Skip to content

Commit 4e4d579

Browse files
committed
add fixture
1 parent 80cdb1b commit 4e4d579

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed
Lines changed: 58 additions & 0 deletions
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 HandleArrayItem 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+
23+
public function testArrayItems()
24+
{
25+
$anotherObject = $this->getMockBuilder(NotRelevantClass::class)
26+
->setConstructorArgs([$this->someStub]);
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 HandleArrayItem 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+
51+
public function testArrayItems()
52+
{
53+
$anotherObject = $this->getMockBuilder(NotRelevantClass::class)
54+
->setConstructorArgs([$this->createStub(\Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\InlineStubPropertyToCreateStubMethodCallRector\Source\NotRelevantClass::class)]);
55+
}
56+
}
57+
58+
?>

rules/CodeQuality/NodeFinder/PropertyFetchUsageFinder.php

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

55
namespace Rector\PHPUnit\CodeQuality\NodeFinder;
66

7+
use PhpParser\Node\Expr\Array_;
78
use PhpParser\Node\Expr\CallLike;
89
use PhpParser\Node\Expr\PropertyFetch;
910
use PhpParser\Node\Stmt\Class_;
@@ -48,4 +49,32 @@ public function findInCallLikes(Class_ $class, string $propertyName): array
4849

4950
return $propertyFetchesInNewArgs;
5051
}
52+
53+
/**
54+
* @return PropertyFetch[]
55+
*/
56+
public function findInArrays(Class_ $class, string $propertyName): array
57+
{
58+
/** @var Array_[] $arrays */
59+
$arrays = $this->betterNodeFinder->findInstancesOfScoped($class->getMethods(), Array_::class);
60+
61+
$propertyFetchesInArrays = [];
62+
63+
foreach ($arrays as $array) {
64+
foreach ($array->items as $arrayItem) {
65+
if (! $arrayItem->value instanceof PropertyFetch) {
66+
continue;
67+
}
68+
69+
$propertyFetch = $arrayItem->value;
70+
if (! $this->nodeNameResolver->isName($propertyFetch->name, $propertyName)) {
71+
continue;
72+
}
73+
74+
$propertyFetchesInArrays[] = $propertyFetch;
75+
}
76+
}
77+
78+
return $propertyFetchesInArrays;
79+
}
5180
}

rules/CodeQuality/Rector/Class_/InlineStubPropertyToCreateStubMethodCallRector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,13 @@ public function refactor(Node $node): ?Node
129129
}
130130

131131
$currentPropertyFetchesInNewArgs = $this->propertyFetchUsageFinder->findInCallLikes($node, $propertyName);
132+
$currentPropertyFetchesInArrays = $this->propertyFetchUsageFinder->findInArrays($node, $propertyName);
132133

133134
// are there more uses than simple passing to a new instance?
134135
$totalPropertyFetches = $this->propertyFetchFinder->findLocalPropertyFetchesByName($node, $propertyName);
135-
if ((count($totalPropertyFetches) - 1) !== count($currentPropertyFetchesInNewArgs)) {
136+
if ((count($totalPropertyFetches) - 1) !== (count($currentPropertyFetchesInNewArgs) + count(
137+
$currentPropertyFetchesInArrays
138+
))) {
136139
continue;
137140
}
138141

0 commit comments

Comments
 (0)