Skip to content

Commit e9bfa56

Browse files
committed
skip property fetch in CreateStubOverCreateMockArgRector
1 parent ba38a2c commit e9bfa56

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-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: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
use PhpParser\Node\Identifier;
1515
use PhpParser\Node\Stmt\ClassMethod;
1616
use PhpParser\Node\Stmt\Expression;
17-
use Rector\PHPStan\ScopeFetcher;
1817
use Rector\PHPUnit\CodeQuality\NodeAnalyser\MockObjectExprDetector;
19-
use Rector\PHPUnit\Enum\PHPUnitClassName;
2018
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
2119
use Rector\Rector\AbstractRector;
2220
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -90,13 +88,7 @@ public function getNodeTypes(): array
9088
*/
9189
public function refactor(Node $node): MethodCall|StaticCall|New_|ArrayItem|ClassMethod|null
9290
{
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)) {
91+
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
10092
return null;
10193
}
10294

@@ -165,6 +157,12 @@ private function refactorClassMethod(ClassMethod $classMethod): ?ClassMethod
165157
}
166158

167159
$assign = $stmt->expr;
160+
161+
// handled in another rule
162+
if ($assign->var instanceof Expr\PropertyFetch) {
163+
continue;
164+
}
165+
168166
$createMockMethodCall = $this->matchCreateMockMethodCall($assign->expr);
169167

170168
if (! $createMockMethodCall instanceof MethodCall) {

0 commit comments

Comments
 (0)