|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types=1); |
| 4 | +namespace Rector\PHPUnit\PHPUnit120\Rector\ClassMethod; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Arg; |
| 8 | +use PhpParser\Node\Expr\Assign; |
| 9 | +use PhpParser\Node\Expr\CallLike; |
| 10 | +use PhpParser\Node\Expr\MethodCall; |
| 11 | +use PhpParser\Node\Expr\New_; |
| 12 | +use PhpParser\Node\Expr\StaticCall; |
| 13 | +use PhpParser\Node\Expr\Variable; |
| 14 | +use PhpParser\Node\Identifier; |
| 15 | +use PhpParser\Node\Stmt\ClassMethod; |
| 16 | +use PhpParser\Node\Stmt\Expression; |
| 17 | +use Rector\PhpParser\Node\BetterNodeFinder; |
| 18 | +use Rector\PHPUnit\CodeQuality\NodeAnalyser\AssignedMocksCollector; |
| 19 | +use Rector\PHPUnit\CodeQuality\NodeFinder\VariableFinder; |
| 20 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 21 | +use Rector\Rector\AbstractRector; |
| 22 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 23 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 24 | +/** |
| 25 | + * @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector\ExpressionCreateMockToCreateStubRectorTest |
| 26 | + */ |
| 27 | +final class ExpressionCreateMockToCreateStubRector extends AbstractRector |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @readonly |
| 31 | + */ |
| 32 | + private AssignedMocksCollector $assignedMocksCollector; |
| 33 | + /** |
| 34 | + * @readonly |
| 35 | + */ |
| 36 | + private TestsNodeAnalyzer $testsNodeAnalyzer; |
| 37 | + /** |
| 38 | + * @readonly |
| 39 | + */ |
| 40 | + private VariableFinder $variableFinder; |
| 41 | + /** |
| 42 | + * @readonly |
| 43 | + */ |
| 44 | + private BetterNodeFinder $betterNodeFinder; |
| 45 | + public function __construct(AssignedMocksCollector $assignedMocksCollector, TestsNodeAnalyzer $testsNodeAnalyzer, VariableFinder $variableFinder, BetterNodeFinder $betterNodeFinder) |
| 46 | + { |
| 47 | + $this->assignedMocksCollector = $assignedMocksCollector; |
| 48 | + $this->testsNodeAnalyzer = $testsNodeAnalyzer; |
| 49 | + $this->variableFinder = $variableFinder; |
| 50 | + $this->betterNodeFinder = $betterNodeFinder; |
| 51 | + } |
| 52 | + public function getRuleDefinition(): RuleDefinition |
| 53 | + { |
| 54 | + return new RuleDefinition('Replace createMock() assigned to variable that is only used as arg with no expectations, to createStub()', [new CodeSample(<<<'CODE_SAMPLE' |
| 55 | +use PHPUnit\Framework\TestCase; |
| 56 | +
|
| 57 | +final class SomeTest extends TestCase |
| 58 | +{ |
| 59 | + public function test(): void |
| 60 | + { |
| 61 | + $mock = $this->createMock(SomeClass::class); |
| 62 | +
|
| 63 | + $someObject = new SomeClass($mock); |
| 64 | + $this->assertSame($mock, $someObject->getDependency()); |
| 65 | + } |
| 66 | +} |
| 67 | +CODE_SAMPLE |
| 68 | +, <<<'CODE_SAMPLE' |
| 69 | +use PHPUnit\Framework\TestCase; |
| 70 | +
|
| 71 | +final class SomeTest extends TestCase |
| 72 | +{ |
| 73 | + public function test(): void |
| 74 | + { |
| 75 | + $mock = $this->createStub(SomeClass::class); |
| 76 | +
|
| 77 | + $someObject = new SomeClass($mock); |
| 78 | + $this->assertSame($mock, $someObject->getDependency()); |
| 79 | + } |
| 80 | +} |
| 81 | +CODE_SAMPLE |
| 82 | +)]); |
| 83 | + } |
| 84 | + public function getNodeTypes(): array |
| 85 | + { |
| 86 | + return [ClassMethod::class]; |
| 87 | + } |
| 88 | + /** |
| 89 | + * @param ClassMethod $node |
| 90 | + */ |
| 91 | + public function refactor(Node $node): ?ClassMethod |
| 92 | + { |
| 93 | + if (!$this->testsNodeAnalyzer->isTestClassMethod($node)) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + if ($node->stmts === null || count($node->stmts) < 2) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + $hasChanged = \false; |
| 100 | + foreach ($node->stmts as $stmt) { |
| 101 | + if (!$stmt instanceof Expression) { |
| 102 | + continue; |
| 103 | + } |
| 104 | + if (!$stmt->expr instanceof Assign) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + $typeArg = $this->assignedMocksCollector->matchCreateMockArgAssignedToVariable($stmt->expr); |
| 108 | + if (!$typeArg instanceof Arg) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + /** @var Assign $assign */ |
| 112 | + $assign = $stmt->expr; |
| 113 | + if (!$assign->var instanceof Variable) { |
| 114 | + continue; |
| 115 | + } |
| 116 | + $assignedVariable = $assign->var; |
| 117 | + $variableName = $this->getName($assignedVariable); |
| 118 | + if ($variableName === null) { |
| 119 | + continue; |
| 120 | + } |
| 121 | + // find variable usages outside call like and inside it |
| 122 | + $usedVariables = $this->variableFinder->find($node, $variableName); |
| 123 | + // used variable in calls |
| 124 | + /** @var array<StaticCall|MethodCall|New_> $callLikes */ |
| 125 | + $callLikes = $this->betterNodeFinder->findInstancesOfScoped($node->stmts, [CallLike::class]); |
| 126 | + $callLikeUsedVariables = $this->collectVariableInCallLikeArg($callLikes, $variableName); |
| 127 | + if (count($usedVariables) - 1 !== count($callLikeUsedVariables)) { |
| 128 | + continue; |
| 129 | + } |
| 130 | + // here we can flip the createMock() to createStub() |
| 131 | + if (!$assign->expr instanceof MethodCall) { |
| 132 | + continue; |
| 133 | + } |
| 134 | + $methodCall = $assign->expr; |
| 135 | + $methodCall->name = new Identifier('createStub'); |
| 136 | + $hasChanged = \true; |
| 137 | + } |
| 138 | + if ($hasChanged) { |
| 139 | + return $node; |
| 140 | + } |
| 141 | + return null; |
| 142 | + } |
| 143 | + /** |
| 144 | + * @param CallLike[] $callLikes |
| 145 | + * @return Variable[] |
| 146 | + */ |
| 147 | + private function collectVariableInCallLikeArg(array $callLikes, string $variableName): array |
| 148 | + { |
| 149 | + $callLikeUsedVariables = []; |
| 150 | + foreach ($callLikes as $callLike) { |
| 151 | + if ($callLike->isFirstClassCallable()) { |
| 152 | + continue; |
| 153 | + } |
| 154 | + foreach ($callLike->getArgs() as $arg) { |
| 155 | + if (!$arg->value instanceof Variable) { |
| 156 | + continue; |
| 157 | + } |
| 158 | + if (!$this->isName($arg->value, $variableName)) { |
| 159 | + continue; |
| 160 | + } |
| 161 | + $callLikeUsedVariables[] = $arg->value; |
| 162 | + } |
| 163 | + } |
| 164 | + return $callLikeUsedVariables; |
| 165 | + } |
| 166 | +} |
0 commit comments