diff --git a/rules-tests/PHPUnit120/Rector/Class_/PropertyCreateMockToCreateStubRector/Fixture/skip_property_used_in_closure.php.inc b/rules-tests/PHPUnit120/Rector/Class_/PropertyCreateMockToCreateStubRector/Fixture/skip_property_used_in_closure.php.inc new file mode 100644 index 000000000..749efdbcb --- /dev/null +++ b/rules-tests/PHPUnit120/Rector/Class_/PropertyCreateMockToCreateStubRector/Fixture/skip_property_used_in_closure.php.inc @@ -0,0 +1,23 @@ +someMock = $this->createMock(\stdClass::class); + } + + public function testThis() + { + $builder = $this->createMock(\stdClass::class); + $builder->method('addEventListener')->willReturnCallback(function () { + $this->someMock->expects($this->once())->method('something'); + }); + } +} diff --git a/rules/CodeQuality/NodeAnalyser/MockObjectExprDetector.php b/rules/CodeQuality/NodeAnalyser/MockObjectExprDetector.php index 2ffc902ab..2a11faf83 100644 --- a/rules/CodeQuality/NodeAnalyser/MockObjectExprDetector.php +++ b/rules/CodeQuality/NodeAnalyser/MockObjectExprDetector.php @@ -141,9 +141,10 @@ public function isPropertyUsedForMocking(Class_ $class, string $propertyName): b return true; } - // find out, how many are used in call likes as args + // find out, how many are used in call likes as args; + // not scoped on purpose, as expects() can be nested in closures, e.g. willReturnCallback() /** @var array $methodCalls */ - $methodCalls = $this->betterNodeFinder->findInstancesOfScoped($class->getMethods(), [MethodCall::class]); + $methodCalls = $this->betterNodeFinder->findInstancesOf($class->getMethods(), [MethodCall::class]); foreach ($methodCalls as $methodCall) { if (! $methodCall->var instanceof PropertyFetch) {