Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Source\ClassWithDependency;

final class PutInArrayArgument extends TestCase
{
public function test()
{
$mock = $this->createMock(\stdClass::class);

$someObject = new ClassWithDependency([$mock]);
$this->assertSame([$mock], $someObject->getDependency());
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Source\ClassWithDependency;

final class PutInArrayArgument extends TestCase
{
public function test()
{
$mock = $this->createStub(\stdClass::class);

$someObject = new ClassWithDependency([$mock]);
$this->assertSame([$mock], $someObject->getDependency());
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Source\ClassWithDependency;

final class SkipArrayAssignedToVariable extends TestCase
{
public function test()
{
$a = $this->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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Source\ClassWithDependency;

final class SkipAssignedAlias extends TestCase
{
public function test()
{
$a = $this->createMock(\stdClass::class);
$alias = $a;
$alias->expects($this->once())->method('someMethod')->willReturn('someValue');

$someObject = new ClassWithDependency($a);
$this->assertSame($a, $someObject->getDependency());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Source\ClassWithDependency;

final class SkipClosureByReference extends TestCase
{
public function test()
{
$a = $this->createMock(\stdClass::class);
$configure = function () use (&$a) {
$a->expects($this->once())->method('someMethod');
};
$configure();

$someObject = new ClassWithDependency($a);
$this->assertSame($a, $someObject->getDependency());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Source\ClassWithDependency;

final class SkipClosureByValue extends TestCase
{
public function test()
{
$a = $this->createMock(\stdClass::class);
$configure = function () use ($a) {
$a->expects($this->once())->method('someMethod');
};
$configure();

$someObject = new ClassWithDependency($a);
$this->assertSame($a, $someObject->getDependency());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\Source\ClassWithDependency;

final class SkipForeachAlias extends TestCase
{
public function test()
{
$a = $this->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());
}
}
71 changes: 71 additions & 0 deletions rules/CodeQuality/NodeAnalyser/MockObjectExprDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Loading