diff --git a/rules-tests/PHPUnit120/Rector/Class_/PropertyCreateMockToCreateStubRector/Fixture/skip_on_property_not_exists.php.inc b/rules-tests/PHPUnit120/Rector/Class_/PropertyCreateMockToCreateStubRector/Fixture/skip_on_property_not_exists.php.inc new file mode 100644 index 00000000..521a88de --- /dev/null +++ b/rules-tests/PHPUnit120/Rector/Class_/PropertyCreateMockToCreateStubRector/Fixture/skip_on_property_not_exists.php.inc @@ -0,0 +1,20 @@ +someMock = $this->createMock(\stdClass::class); + } + + public function testThis() + { + $this->assertSame('...', $this->someMock); + } +} + +?> \ No newline at end of file diff --git a/rules/CodeQuality/NodeAnalyser/MockObjectExprDetector.php b/rules/CodeQuality/NodeAnalyser/MockObjectExprDetector.php index 88573763..2ffc902a 100644 --- a/rules/CodeQuality/NodeAnalyser/MockObjectExprDetector.php +++ b/rules/CodeQuality/NodeAnalyser/MockObjectExprDetector.php @@ -11,6 +11,7 @@ use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; +use PhpParser\Node\Stmt\Property; use PHPStan\Reflection\MethodReflection; use PHPStan\Type\ObjectType; use Rector\NodeNameResolver\NodeNameResolver; @@ -132,6 +133,14 @@ public function isUsedForMocking(Expr $expr, ClassMethod $classMethod): bool public function isPropertyUsedForMocking(Class_ $class, string $propertyName): bool { + $property = $class->getProperty($propertyName); + + // possibly dynamic property on purpose + // mark as used + if (! $property instanceof Property) { + return true; + } + // find out, how many are used in call likes as args /** @var array $methodCalls */ $methodCalls = $this->betterNodeFinder->findInstancesOfScoped($class->getMethods(), [MethodCall::class]);