diff --git a/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php b/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php index d0252853eb2..2e8cc57ad7e 100644 --- a/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php +++ b/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php @@ -14,8 +14,10 @@ use PHPStan\Type\ObjectType; use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer; use Rector\Core\ValueObject\MethodName; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\NodeTypeResolver; use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser; +use Rector\PostRector\Collector\NodesToRemoveCollector; use Rector\TypeDeclaration\Matcher\PropertyAssignMatcher; use Rector\TypeDeclaration\NodeAnalyzer\AutowiredClassMethodOrPropertyAnalyzer; @@ -31,7 +33,8 @@ public function __construct( private readonly PropertyAssignMatcher $propertyAssignMatcher, private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser, private readonly AutowiredClassMethodOrPropertyAnalyzer $autowiredClassMethodOrPropertyAnalyzer, - private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer + private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer, + private readonly NodesToRemoveCollector $nodesToRemoveCollector ) { } @@ -64,6 +67,11 @@ public function isPropertyAssigned(ClassLike $classLike, string $propertyName): return null; } + $parentNode = $assign->getAttribute(AttributeKey::PARENT_NODE); + if ($parentNode instanceof Expression && $this->nodesToRemoveCollector->isNodeRemoved($parentNode)) { + return null; + } + $isAssignedInConstructor = true; return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; diff --git a/tests/Issues/InlineTypedFromConstructorDefaultValue/Fixture/fixture.php.inc b/tests/Issues/InlineTypedFromConstructorDefaultValue/Fixture/fixture.php.inc new file mode 100644 index 00000000000..e667ece6503 --- /dev/null +++ b/tests/Issues/InlineTypedFromConstructorDefaultValue/Fixture/fixture.php.inc @@ -0,0 +1,34 @@ +url = 'https://website.tld'; + } +} + +?> +----- + diff --git a/tests/Issues/InlineTypedFromConstructorDefaultValue/InlineTypedFromConstructorDefaultValueTest.php b/tests/Issues/InlineTypedFromConstructorDefaultValue/InlineTypedFromConstructorDefaultValueTest.php new file mode 100644 index 00000000000..69b30aec578 --- /dev/null +++ b/tests/Issues/InlineTypedFromConstructorDefaultValue/InlineTypedFromConstructorDefaultValueTest.php @@ -0,0 +1,32 @@ +doTestFile($filePath); + } + + /** + * @return Iterator> + */ + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Issues/InlineTypedFromConstructorDefaultValue/config/configured_rule.php b/tests/Issues/InlineTypedFromConstructorDefaultValue/config/configured_rule.php new file mode 100644 index 00000000000..d2f88d523cb --- /dev/null +++ b/tests/Issues/InlineTypedFromConstructorDefaultValue/config/configured_rule.php @@ -0,0 +1,14 @@ +rules([ + InlineConstructorDefaultToPropertyRector::class, + TypedPropertyFromStrictConstructorRector::class, + ]); +};