diff --git a/rules/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector.php b/rules/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector.php index 825e3497..fe9fb827 100644 --- a/rules/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector.php +++ b/rules/TypedCollections/Rector/ClassMethod/RemoveNullFromNullableCollectionTypeRector.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Name; use PhpParser\Node\NullableType; +use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Property; use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; @@ -49,7 +50,7 @@ public function getRuleDefinition(): RuleDefinition final class SomeClass { - private $items; + private ?Collection $items = null; public function setItems(?Collection $items): void { @@ -63,7 +64,7 @@ public function setItems(?Collection $items): void final class SomeClass { - private $items; + private Collection $items; public function setItems(Collection $items): void { @@ -79,19 +80,38 @@ public function setItems(Collection $items): void public function getNodeTypes(): array { - return [ClassMethod::class, Property::class]; + return [Class_::class]; } /** - * @param ClassMethod|Property $node + * @param Class_ $node */ - public function refactor(Node $node): ClassMethod|Property|null + public function refactor(Node $node): ?Class_ { - if ($node instanceof Property) { - return $this->refactorProperty($node); + // nullable might be on purpose, e.g. via data provider + if ($this->testsNodeAnalyzer->isInTestClass($node)) { + return null; + } + + $hasChanged = false; + + foreach ($node->getMethods() as $classMethod) { + if ($this->refactorClassMethod($classMethod) instanceof ClassMethod) { + $hasChanged = true; + } + } + + foreach ($node->getProperties() as $property) { + if ($this->refactorProperty($property) instanceof Property) { + $hasChanged = true; + } } - return $this->refactorClassMethod($node); + if ($hasChanged) { + return $node; + } + + return null; } private function refactorClassMethod(ClassMethod $classMethod): null|ClassMethod @@ -100,11 +120,6 @@ private function refactorClassMethod(ClassMethod $classMethod): null|ClassMethod return null; } - // nullable might be on purpose, e.g. via data provider - if ($this->testsNodeAnalyzer->isInTestClass($classMethod)) { - return null; - } - $hasChanged = false; foreach ($classMethod->params as $param) {