diff --git a/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/skip_merged_property.php.inc b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/skip_merged_property.php.inc index 7459ac70cd1..080229a29e5 100644 --- a/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/skip_merged_property.php.inc +++ b/rules-tests/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector/Fixture/skip_merged_property.php.inc @@ -48,4 +48,3 @@ final class SkipMergedProperty } ?> - diff --git a/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php b/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php index 85fc180eb23..f50720408f0 100644 --- a/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php +++ b/rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php @@ -6,6 +6,7 @@ use PhpParser\Node; use PhpParser\Node\Expr; +use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; use PhpParser\Node\NullableType; @@ -84,12 +85,12 @@ public function getRuleDefinition(): RuleDefinition <<<'CODE_SAMPLE' class SomeClass { - public float $someVariable; + public float $price; public function __construct( - float $someVariable = 0.0 + float $price = 0.0 ) { - $this->someVariable = $someVariable; + $this->price = $price; } } CODE_SAMPLE @@ -98,7 +99,7 @@ public function __construct( class SomeClass { public function __construct( - public float $someVariable = 0.0 + public float $price = 0.0 ) { } } @@ -215,6 +216,22 @@ public function refactor(Node $node): ?Node $param, $paramTagValueNode ); + + // update variable to property fetch references + $this->traverseNodesWithCallable((array) $constructClassMethod->stmts, function (Node $node) use ( + $promotionCandidate, + $propertyName + ): ?PropertyFetch { + if (! $node instanceof Variable) { + return null; + } + + if (! $this->isName($node, $promotionCandidate->getParamName())) { + return null; + } + + return new PropertyFetch(new Variable('this'), $propertyName); + }); } return $node; diff --git a/rules/Php80/ValueObject/PropertyPromotionCandidate.php b/rules/Php80/ValueObject/PropertyPromotionCandidate.php index e39d58d86ac..d1b1a00452b 100644 --- a/rules/Php80/ValueObject/PropertyPromotionCandidate.php +++ b/rules/Php80/ValueObject/PropertyPromotionCandidate.php @@ -4,9 +4,11 @@ namespace Rector\Php80\ValueObject; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Param; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Property; +use Rector\Core\Exception\ShouldNotHappenException; use Rector\NodeTypeResolver\Node\AttributeKey; final class PropertyPromotionCandidate @@ -28,6 +30,21 @@ public function getParam(): Param return $this->param; } + public function getParamName(): string + { + $paramVar = $this->param->var; + + if (! $paramVar instanceof Variable) { + throw new ShouldNotHappenException(); + } + + if (! is_string($paramVar->name)) { + throw new ShouldNotHappenException(); + } + + return $paramVar->name; + } + public function getStmtPosition(): int { return $this->expression->getAttribute(AttributeKey::STMT_KEY); diff --git a/tests/Issues/IssueConstructorPromotionRename/Fixture/fixture.php.inc b/tests/Issues/IssueConstructorPromotionRename/Fixture/fixture.php.inc index 8ae49d3e943..b164942e5b1 100644 --- a/tests/Issues/IssueConstructorPromotionRename/Fixture/fixture.php.inc +++ b/tests/Issues/IssueConstructorPromotionRename/Fixture/fixture.php.inc @@ -32,8 +32,8 @@ final class Fixture { public function __construct(private PromotedPropertyObject $promotedPropertyObject) { - dump($promotedPropertyObject); - dump($promotedPropertyObject->someCall('Y-m-d')); + dump($this->promotedPropertyObject); + dump($this->promotedPropertyObject->someCall('Y-m-d')); } }