diff --git a/rules-tests/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector/Fixture/put_in_array_argument.php.inc b/rules-tests/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector/Fixture/put_in_array_argument.php.inc new file mode 100644 index 00000000..81350259 --- /dev/null +++ b/rules-tests/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector/Fixture/put_in_array_argument.php.inc @@ -0,0 +1,43 @@ +createMock(\stdClass::class); + + $someObject = new ClassWithDependency([$mock]); + $this->assertSame([$mock], $someObject->getDependency()); + } +} + +?> +----- +createStub(\stdClass::class); + + $someObject = new ClassWithDependency([$mock]); + $this->assertSame([$mock], $someObject->getDependency()); + } +} + +?> diff --git a/rules-tests/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector/Fixture/skip_array_assigned_to_variable.php.inc b/rules-tests/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector/Fixture/skip_array_assigned_to_variable.php.inc new file mode 100644 index 00000000..806d6dfd --- /dev/null +++ b/rules-tests/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector/Fixture/skip_array_assigned_to_variable.php.inc @@ -0,0 +1,23 @@ +createMock(\stdClass::class); + $mocks = [$a]; + foreach ($mocks as $mock) { + $mock->expects($this->once())->method('someMethod')->willReturn('someValue'); + } + + $someObject = new ClassWithDependency($a); + $this->assertSame($a, $someObject->getDependency()); + } +} diff --git a/rules-tests/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector/Fixture/skip_assigned_alias.php.inc b/rules-tests/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector/Fixture/skip_assigned_alias.php.inc new file mode 100644 index 00000000..a47e3869 --- /dev/null +++ b/rules-tests/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector/Fixture/skip_assigned_alias.php.inc @@ -0,0 +1,21 @@ +createMock(\stdClass::class); + $alias = $a; + $alias->expects($this->once())->method('someMethod')->willReturn('someValue'); + + $someObject = new ClassWithDependency($a); + $this->assertSame($a, $someObject->getDependency()); + } +} diff --git a/rules-tests/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector/Fixture/skip_closure_by_reference.php.inc b/rules-tests/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector/Fixture/skip_closure_by_reference.php.inc new file mode 100644 index 00000000..27b38b47 --- /dev/null +++ b/rules-tests/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector/Fixture/skip_closure_by_reference.php.inc @@ -0,0 +1,23 @@ +createMock(\stdClass::class); + $configure = function () use (&$a) { + $a->expects($this->once())->method('someMethod'); + }; + $configure(); + + $someObject = new ClassWithDependency($a); + $this->assertSame($a, $someObject->getDependency()); + } +} diff --git a/rules-tests/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector/Fixture/skip_closure_by_value.php.inc b/rules-tests/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector/Fixture/skip_closure_by_value.php.inc new file mode 100644 index 00000000..9c33f1cd --- /dev/null +++ b/rules-tests/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector/Fixture/skip_closure_by_value.php.inc @@ -0,0 +1,23 @@ +createMock(\stdClass::class); + $configure = function () use ($a) { + $a->expects($this->once())->method('someMethod'); + }; + $configure(); + + $someObject = new ClassWithDependency($a); + $this->assertSame($a, $someObject->getDependency()); + } +} diff --git a/rules-tests/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector/Fixture/skip_foreach_alias.php.inc b/rules-tests/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector/Fixture/skip_foreach_alias.php.inc new file mode 100644 index 00000000..434160ee --- /dev/null +++ b/rules-tests/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector/Fixture/skip_foreach_alias.php.inc @@ -0,0 +1,23 @@ +createMock(\stdClass::class); + $b = $this->createMock(\stdClass::class); + foreach ([$a, $b] as $mock) { + $mock->expects($this->once())->method('someMethod')->willReturn('someValue'); + } + + $someObject = new ClassWithDependency($a); + $this->assertSame($a, $someObject->getDependency()); + } +} diff --git a/rules/CodeQuality/NodeAnalyser/MockObjectExprDetector.php b/rules/CodeQuality/NodeAnalyser/MockObjectExprDetector.php index 543d6656..8fcf7d3e 100644 --- a/rules/CodeQuality/NodeAnalyser/MockObjectExprDetector.php +++ b/rules/CodeQuality/NodeAnalyser/MockObjectExprDetector.php @@ -4,13 +4,19 @@ namespace Rector\PHPUnit\CodeQuality\NodeAnalyser; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr; +use PhpParser\Node\Expr\Array_; +use PhpParser\Node\Expr\Assign; +use PhpParser\Node\Expr\AssignRef; +use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; +use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\Property; use PHPStan\Reflection\MethodReflection; use PHPStan\Type\ObjectType; @@ -143,6 +149,71 @@ public function isUsedForMocking(Expr $expr, ClassMethod $classMethod): bool return false; } + /** + * The variable is put to an array, assigned to another variable or captured by a closure, e.g. + * foreach ([$a, $b] as $mock), $alias = $a or $mocks = [$a]; it can be mocked through the alias, so be safe + */ + public function isEscapingToAlias(Expr $expr, ClassMethod $classMethod): bool + { + if (! $expr instanceof Variable) { + return false; + } + + $variableName = $this->nodeNameResolver->getName($expr); + + // to be safe + if ($variableName === null) { + return true; + } + + // not scoped on purpose, as the alias can be created in a closure + $nodes = $this->betterNodeFinder->findInstancesOf( + (array) $classMethod->stmts, + [Assign::class, AssignRef::class, Foreach_::class, Closure::class] + ); + + foreach ($nodes as $node) { + if ($node instanceof Foreach_) { + if ($this->isVariableInArray($node->expr, $variableName)) { + return true; + } + + continue; + } + + if ($node instanceof Assign || $node instanceof AssignRef) { + // e.g. $alias = $a, $mocks = [$a, $b]; arrays passed directly as arguments are not aliases + if ($this->isVariableNamed($node->expr, $variableName) || $this->isVariableInArray($node->expr, $variableName)) { + return true; + } + + continue; + } + + foreach ($node->uses as $closureUse) { + if ($this->nodeNameResolver->isName($closureUse->var, $variableName)) { + return true; + } + } + } + + return false; + } + + private function isVariableNamed(Expr $expr, string $variableName): bool + { + return $expr instanceof Variable && $this->nodeNameResolver->isName($expr, $variableName); + } + + private function isVariableInArray(Expr $expr, string $variableName): bool + { + if (! $expr instanceof Array_) { + return false; + } + + return array_any($expr->items, fn (ArrayItem $item): bool => $this->isVariableNamed($item->value, $variableName)); + } + public function isPropertyUsedForMocking(Class_ $class, string $propertyName): bool { $property = $class->getProperty($propertyName); diff --git a/rules/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector.php b/rules/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector.php index 3c12b868..4eefd0b5 100644 --- a/rules/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector.php +++ b/rules/PHPUnit120/Rector/ClassMethod/ExpressionCreateMockToCreateStubRector.php @@ -123,6 +123,11 @@ public function refactor(Node $node): ?ClassMethod continue; } + // the mock can be configured through an alias, e.g. foreach ([$a, $b] as $mock) + if ($this->mockObjectExprDetector->isEscapingToAlias($assign->var, $node)) { + continue; + } + /** @var MethodCall $methodCall */ $methodCall = $assign->expr; $methodCall->name = new Identifier('createStub');