8000 Incorrect behavior of RemoveEmptyClassMethodRector, RemoveUnusedConstructorParamRector · Issue #9163 · rectorphp/rector · GitHub
[go: up one dir, main page]

Skip to content
Incorrect behavior of RemoveEmptyClassMethodRector, RemoveUnusedConstructorParamRector #9163
@andrew-demb

Description

@andrew-demb

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

From PHP PoV these two classes are equal - both have a public property name (https://3v4l.org/lcTgW). But rector thinks that public function __construct(readonly string $name) { } it is not a constructor with a promoted property, but with a regular parameter.

See https://getrector.com/demo/7f5423c8-123a-4ca0-b2c4-88c3e90ae2c7

<?php

final class SomeClassFoo
{
    public function __construct(public readonly string $name) { }
}

final class SomeClassBar
{
    public function __construct(readonly string $name) { }
}

$a = new SomeClassFoo('foo');
$b = new SomeClassBar('bar');

var_dump($a, $b);

Responsible rules

  • RemoveEmptyClassMethodRector

  • RemoveUnusedConstructorParamRector

  • ReadOnlyClassRector

Expected Behavior

 <?php
 
-final class SomeClassFoo
+final readonly class SomeClassFoo
 {
-    public function __construct(public readonly string $name) { }
+    public function __construct(public string $name) { }
 }
 
-final class SomeClassBar
-{
-    public function __construct(readonly string $name) { }
+final readonly class SomeClassBar
+{
+    public function __construct(string $name) { }
 }
 
$a = new SomeClassFoo('foo');
$b = new SomeClassBar('bar');

var_dump($a, $b);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0