From c568e2e7c4155fe0a42194f1bd09e804f64f75e3 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 5 Feb 2024 03:40:39 +0700 Subject: [PATCH] [TypeDeclaration] Skip assign in construct with method call property same name on RestoreDefaultNullToNullableTypePropertyRector --- ...ith_method_call_property_same_name.php.inc | 19 +++++++++++++++++++ .../ConstructorAssignDetector.php | 11 +++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 rules-tests/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector/Fixture/skip_assign_with_method_call_property_same_name.php.inc diff --git a/rules-tests/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector/Fixture/skip_assign_with_method_call_property_same_name.php.inc b/rules-tests/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector/Fixture/skip_assign_with_method_call_property_same_name.php.inc new file mode 100644 index 00000000000..42920ba9e72 --- /dev/null +++ b/rules-tests/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector/Fixture/skip_assign_with_method_call_property_same_name.php.inc @@ -0,0 +1,19 @@ +bookmarkFeature = $relation->getRelationParameters()->bookmarkFeature; + } +} \ No newline at end of file diff --git a/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php b/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php index e8be42b3546..13862a4d9d6 100644 --- a/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php +++ b/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php @@ -21,6 +21,7 @@ use Rector\NodeAnalyzer\PropertyFetchAnalyzer; use Rector\NodeTypeResolver\NodeTypeResolver; use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; +use Rector\PhpParser\Comparing\NodeComparator; use Rector\TypeDeclaration\Matcher\PropertyAssignMatcher; use Rector\TypeDeclaration\NodeAnalyzer\AutowiredClassMethodOrPropertyAnalyzer; use Rector\ValueObject\MethodName; @@ -38,6 +39,7 @@ public function __construct( private SimpleCallableNodeTraverser $simpleCallableNodeTraverser, private AutowiredClassMethodOrPropertyAnalyzer $autowiredClassMethodOrPropertyAnalyzer, private PropertyFetchAnalyzer $propertyFetchAnalyzer, + private NodeComparator $nodeComparator ) { } @@ -192,7 +194,8 @@ private function matchInitializeClassMethod(ClassLike $classLike): array private function isPropertyUsedInAssign(Assign $assign, string $propertyName): bool { $nodeFinder = new NodeFinder(); - return (bool) $nodeFinder->findFirst($assign->expr, static function (Node $node) use ($propertyName): ?bool { + $var = $assign->var; + return (bool) $nodeFinder->findFirst($assign->expr, function (Node $node) use ($propertyName, $var): ?bool { if (! $node instanceof PropertyFetch) { return null; } @@ -201,7 +204,11 @@ private function isPropertyUsedInAssign(Assign $assign, string $propertyName): b return null; } - return $node->name->toString() === $propertyName; + if ($node->name->toString() !== $propertyName) { + return null; + } + + return $this->nodeComparator->areNodesEqual($node, $var); }); } }