|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\MethodCall; |
| 9 | +use PhpParser\Node\Stmt\ClassMethod; |
| 10 | +use PhpParser\Node\Stmt\Expression; |
| 11 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 12 | +use Rector\Rector\AbstractRector; |
| 13 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 14 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 15 | + |
| 16 | +/** |
| 17 | + * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\RemoveStandaloneCreateMockRector\RemoveStandaloneCreateMockRectorTest |
| 18 | + */ |
| 19 | +final class RemoveStandaloneCreateMockRector extends AbstractRector |
| 20 | +{ |
| 21 | + public function __construct( |
| 22 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 23 | + ) { |
| 24 | + } |
| 25 | + |
| 26 | + public function getRuleDefinition(): RuleDefinition |
| 27 | + { |
| 28 | + return new RuleDefinition( |
| 29 | + 'Remove standalone method calls with no effect', |
| 30 | + [ |
| 31 | + new CodeSample( |
| 32 | + <<<'CODE_SAMPLE' |
| 33 | +use PHPUnit\Framework\TestCase; |
| 34 | +
|
| 35 | +final class SomeTest extends TestCase |
| 36 | +{ |
| 37 | + public function test() |
| 38 | + { |
| 39 | + $this->createMock('SomeClass'); |
| 40 | + } |
| 41 | +} |
| 42 | +CODE_SAMPLE |
| 43 | + |
| 44 | + , |
| 45 | + <<<'CODE_SAMPLE' |
| 46 | +use PHPUnit\Framework\TestCase; |
| 47 | +
|
| 48 | +final class SomeTest extends TestCase |
| 49 | +{ |
| 50 | + public function test() |
| 51 | + { |
| 52 | + } |
| 53 | +} |
| 54 | +CODE_SAMPLE |
| 55 | + ), |
| 56 | + ] |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return array<class-string<Node>> |
| 62 | + */ |
| 63 | + public function getNodeTypes(): array |
| 64 | + { |
| 65 | + return [ClassMethod::class]; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param ClassMethod $node |
| 70 | + */ |
| 71 | + public function refactor(Node $node): ?Node |
| 72 | + { |
| 73 | + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + $hasChanged = false; |
| 78 | + foreach ((array) $node->stmts as $key => $stmt) { |
| 79 | + if (! $stmt instanceof Expression) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + if (! $stmt->expr instanceof MethodCall) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + $topmostCall = $this->resolveTopmostCall($stmt->expr); |
| 88 | + if (! $this->isName($topmostCall->name, 'createMock')) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + unset($node->stmts[$key]); |
| 93 | + $hasChanged = true; |
| 94 | + } |
| 95 | + |
| 96 | + if ($hasChanged) { |
| 97 | + return $node; |
| 98 | + } |
| 99 | + |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + private function resolveTopmostCall(MethodCall $methodCall): MethodCall |
| 104 | + { |
| 105 | + $currentMethodCall = $methodCall; |
| 106 | + while ($currentMethodCall->var instanceof MethodCall) { |
| 107 | + $currentMethodCall = $currentMethodCall->var; |
| 108 | + } |
| 109 | + |
| 110 | + return $currentMethodCall; |
| 111 | + } |
| 112 | +} |
0 commit comments