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