diff --git a/rules-tests/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector/Fixture/handle_array_item.php.inc b/rules-tests/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector/Fixture/handle_array_item.php.inc new file mode 100644 index 00000000..44c7789b --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector/Fixture/handle_array_item.php.inc @@ -0,0 +1,58 @@ +someStub = $this->createStub(NotRelevantClass::class); + } + + public function testAnother() + { + $anotherObject = new NotRelevantClass($this->someStub); + } + + public function testArrayItems() + { + $anotherObject = $this->getMockBuilder(NotRelevantClass::class) + ->setConstructorArgs([$this->someStub]); + } +} + +?> +----- +createStub(\Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass::class)); + } + + public function testArrayItems() + { + $anotherObject = $this->getMockBuilder(NotRelevantClass::class) + ->setConstructorArgs([$this->createStub(\Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass::class)]); + } +} + +?> diff --git a/rules/CodeQuality/NodeFinder/PropertyFetchUsageFinder.php b/rules/CodeQuality/NodeFinder/PropertyFetchUsageFinder.php index aae14596..b84a114a 100644 --- a/rules/CodeQuality/NodeFinder/PropertyFetchUsageFinder.php +++ b/rules/CodeQuality/NodeFinder/PropertyFetchUsageFinder.php @@ -4,6 +4,7 @@ namespace Rector\PHPUnit\CodeQuality\NodeFinder; +use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\CallLike; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Stmt\Class_; @@ -48,4 +49,32 @@ public function findInCallLikes(Class_ $class, string $propertyName): array return $propertyFetchesInNewArgs; } + + /** + * @return PropertyFetch[] + */ + public function findInArrays(Class_ $class, string $propertyName): array + { + /** @var Array_[] $arrays */ + $arrays = $this->betterNodeFinder->findInstancesOfScoped($class->getMethods(), Array_::class); + + $propertyFetchesInArrays = []; + + foreach ($arrays as $array) { + foreach ($array->items as $arrayItem) { + if (! $arrayItem->value instanceof PropertyFetch) { + continue; + } + + $propertyFetch = $arrayItem->value; + if (! $this->nodeNameResolver->isName($propertyFetch->name, $propertyName)) { + continue; + } + + $propertyFetchesInArrays[] = $propertyFetch; + } + } + + return $propertyFetchesInArrays; + } } diff --git a/rules/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector.php b/rules/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector.php index 513ae684..d8245d58 100644 --- a/rules/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector.php +++ b/rules/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector.php @@ -129,10 +129,13 @@ public function refactor(Node $node): ?Node } $currentPropertyFetchesInNewArgs = $this->propertyFetchUsageFinder->findInCallLikes($node, $propertyName); + $currentPropertyFetchesInArrays = $this->propertyFetchUsageFinder->findInArrays($node, $propertyName); // are there more uses than simple passing to a new instance? $totalPropertyFetches = $this->propertyFetchFinder->findLocalPropertyFetchesByName($node, $propertyName); - if ((count($totalPropertyFetches) - 1) !== count($currentPropertyFetchesInNewArgs)) { + if ((count($totalPropertyFetches) - 1) !== (count($currentPropertyFetchesInNewArgs) + count( + $currentPropertyFetchesInArrays + ))) { continue; }