From 3bf8a9e93a99304daca819bf1ccfdcf1be4b1e2c Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 3 Feb 2026 19:39:07 +0100 Subject: [PATCH] handle in call likes as well --- .../Fixture/inline_used_in_calls.php.inc | 58 +++++++++++++++++++ .../NodeFinder/PropertyFetchUsageFinder.php | 14 ++--- ...ubPropertyToCreateStubMethodCallRector.php | 2 +- 3 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector/Fixture/inline_used_in_calls.php.inc diff --git a/rules-tests/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector/Fixture/inline_used_in_calls.php.inc b/rules-tests/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector/Fixture/inline_used_in_calls.php.inc new file mode 100644 index 00000000..8d029862 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector/Fixture/inline_used_in_calls.php.inc @@ -0,0 +1,58 @@ +someStub = $this->createStub(NotRelevantClass::class); + } + + public function testAnother() + { + $anotherObject = new NotRelevantClass($this->someStub); + + $this->runThis($this->someStub); + } + + private function runThis($object) + { + } +} + +?> +----- +createStub(\Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass::class)); + + $this->runThis($this->createStub(\Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass::class)); + } + + private function runThis($object) + { + } +} + +?> diff --git a/rules/CodeQuality/NodeFinder/PropertyFetchUsageFinder.php b/rules/CodeQuality/NodeFinder/PropertyFetchUsageFinder.php index fe9d0360..aae14596 100644 --- a/rules/CodeQuality/NodeFinder/PropertyFetchUsageFinder.php +++ b/rules/CodeQuality/NodeFinder/PropertyFetchUsageFinder.php @@ -4,7 +4,7 @@ namespace Rector\PHPUnit\CodeQuality\NodeFinder; -use PhpParser\Node\Expr\New_; +use PhpParser\Node\Expr\CallLike; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Stmt\Class_; use Rector\NodeNameResolver\NodeNameResolver; @@ -21,19 +21,19 @@ public function __construct( /** * @return PropertyFetch[] */ - public function findInNew(Class_ $class, string $propertyName): array + public function findInCallLikes(Class_ $class, string $propertyName): array { - /** @var New_[] $news */ - $news = $this->betterNodeFinder->findInstancesOfScoped($class->getMethods(), New_::class); + /** @var CallLike[] $callLikes */ + $callLikes = $this->betterNodeFinder->findInstancesOfScoped($class->getMethods(), CallLike::class); $propertyFetchesInNewArgs = []; - foreach ($news as $new) { - if ($new->isFirstClassCallable()) { + foreach ($callLikes as $callLike) { + if ($callLike->isFirstClassCallable()) { continue; } - foreach ($new->getArgs() as $arg) { + foreach ($callLike->getArgs() as $arg) { if (! $arg->value instanceof PropertyFetch) { continue; } diff --git a/rules/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector.php b/rules/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector.php index 35114103..513ae684 100644 --- a/rules/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector.php +++ b/rules/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector.php @@ -128,7 +128,7 @@ public function refactor(Node $node): ?Node continue; } - $currentPropertyFetchesInNewArgs = $this->propertyFetchUsageFinder->findInNew($node, $propertyName); + $currentPropertyFetchesInNewArgs = $this->propertyFetchUsageFinder->findInCallLikes($node, $propertyName); // are there more uses than simple passing to a new instance? $totalPropertyFetches = $this->propertyFetchFinder->findLocalPropertyFetchesByName($node, $propertyName);