Skip to content

Commit 3bf8a9e

Browse files
committed
handle in call likes as well
1 parent 3ff0139 commit 3bf8a9e

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use PHPUnit\Framework\MockObject\Stub;
7+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass;
8+
9+
final class InlineUsedInCalls extends TestCase
10+
{
11+
private Stub $someStub;
12+
13+
protected function setUp(): void
14+
{
15+
$this->someStub = $this->createStub(NotRelevantClass::class);
16+
}
17+
18+
public function testAnother()
19+
{
20+
$anotherObject = new NotRelevantClass($this->someStub);
21+
22+
$this->runThis($this->someStub);
23+
}
24+
25+
private function runThis($object)
26+
{
27+
}
28+
}
29+
30+
?>
31+
-----
32+
<?php
33+
34+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Fixture;
35+
36+
use PHPUnit\Framework\TestCase;
37+
use PHPUnit\Framework\MockObject\Stub;
38+
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass;
39+
40+
final class InlineUsedInCalls extends TestCase
41+
{
42+
protected function setUp(): void
43+
{
44+
}
45+
46+
public function testAnother()
47+
{
48+
$anotherObject = new NotRelevantClass($this->createStub(\Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass::class));
49+
50+
$this->runThis($this->createStub(\Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass::class));
51+
}
52+
53+
private function runThis($object)
54+
{
55+
}
56+
}
57+
58+
?>

rules/CodeQuality/NodeFinder/PropertyFetchUsageFinder.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Rector\PHPUnit\CodeQuality\NodeFinder;
66

7-
use PhpParser\Node\Expr\New_;
7+
use PhpParser\Node\Expr\CallLike;
88
use PhpParser\Node\Expr\PropertyFetch;
99
use PhpParser\Node\Stmt\Class_;
1010
use Rector\NodeNameResolver\NodeNameResolver;
@@ -21,19 +21,19 @@ public function __construct(
2121
/**
2222
* @return PropertyFetch[]
2323
*/
24-
public function findInNew(Class_ $class, string $propertyName): array
24+
public function findInCallLikes(Class_ $class, string $propertyName): array
2525
{
26-
/** @var New_[] $news */
27-
$news = $this->betterNodeFinder->findInstancesOfScoped($class->getMethods(), New_::class);
26+
/** @var CallLike[] $callLikes */
27+
$callLikes = $this->betterNodeFinder->findInstancesOfScoped($class->getMethods(), CallLike::class);
2828

2929
$propertyFetchesInNewArgs = [];
3030

31-
foreach ($news as $new) {
32-
if ($new->isFirstClassCallable()) {
31+
foreach ($callLikes as $callLike) {
32+
if ($callLike->isFirstClassCallable()) {
3333
continue;
3434
}
3535

36-
foreach ($new->getArgs() as $arg) {
36+
foreach ($callLike->getArgs() as $arg) {
3737
if (! $arg->value instanceof PropertyFetch) {
3838
continue;
3939
}

rules/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function refactor(Node $node): ?Node
128128
continue;
129129
}
130130

131-
$currentPropertyFetchesInNewArgs = $this->propertyFetchUsageFinder->findInNew($node, $propertyName);
131+
$currentPropertyFetchesInNewArgs = $this->propertyFetchUsageFinder->findInCallLikes($node, $propertyName);
132132

133133
// are there more uses than simple passing to a new instance?
134134
$totalPropertyFetches = $this->propertyFetchFinder->findLocalPropertyFetchesByName($node, $propertyName);

0 commit comments

Comments
 (0)