Skip to content

Commit 40a6044

Browse files
authored
skip property fetch in CreateStubOverCreateMockArgRector (#635)
1 parent ba38a2c commit 40a6044

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\CallLike\CreateStubOverCreateMockArgRector\Fixture;
4+
5+
use PHPUnit\Framework\MockObject\MockObject;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class SkipPropertyFetch extends TestCase
9+
{
10+
private MockObject $item;
11+
12+
public function testThat()
13+
{
14+
$this->item = $this->createMock(\stdClass::class);
15+
}
16+
17+
public function testUsedMock()
18+
{
19+
$this->item->expects($this->atLeastOnce())->method('someMethod')->willReturn(123);
20+
}
21+
}

rules/PHPUnit120/Rector/CallLike/CreateStubOverCreateMockArgRector.php

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

55
namespace Rector\PHPUnit\PHPUnit120\Rector\CallLike;
66

7+
use PhpParser\Node\Expr\PropertyFetch;
78
use PhpParser\Node;
89
use PhpParser\Node\ArrayItem;
910
use PhpParser\Node\Expr;
@@ -14,9 +15,7 @@
1415
use PhpParser\Node\Identifier;
1516
use PhpParser\Node\Stmt\ClassMethod;
1617
use PhpParser\Node\Stmt\Expression;
17-
use Rector\PHPStan\ScopeFetcher;
1818
use Rector\PHPUnit\CodeQuality\NodeAnalyser\MockObjectExprDetector;
19-
use Rector\PHPUnit\Enum\PHPUnitClassName;
2019
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
2120
use Rector\Rector\AbstractRector;
2221
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -90,13 +89,7 @@ public function getNodeTypes(): array
9089
*/
9190
public function refactor(Node $node): MethodCall|StaticCall|New_|ArrayItem|ClassMethod|null
9291
{
93-
$scope = ScopeFetcher::fetch($node);
94-
if (! $scope->isInClass()) {
95-
return null;
96-
}
97-
98-
$classReflection = $scope->getClassReflection();
99-
if (! $classReflection->is(PHPUnitClassName::TEST_CASE)) {
92+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
10093
return null;
10194
}
10295

@@ -165,6 +158,12 @@ private function refactorClassMethod(ClassMethod $classMethod): ?ClassMethod
165158
}
166159

167160
$assign = $stmt->expr;
161+
162+
// handled in another rule
163+
if ($assign->var instanceof PropertyFetch) {
164+
continue;
165+
}
166+
168167
$createMockMethodCall = $this->matchCreateMockMethodCall($assign->expr);
169168

170169
if (! $createMockMethodCall instanceof MethodCall) {

0 commit comments

Comments
 (0)