|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\CodeQuality\Rector\Class_; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\Assign; |
| 9 | +use PhpParser\Node\Expr\MethodCall; |
| 10 | +use PhpParser\Node\Expr\PropertyFetch; |
| 11 | +use PhpParser\Node\Stmt\Class_; |
| 12 | +use PhpParser\Node\Stmt\ClassMethod; |
| 13 | +use PhpParser\Node\Stmt\Expression; |
| 14 | +use PhpParser\Node\Stmt\Property; |
| 15 | +use Rector\PHPUnit\CodeQuality\NodeAnalyser\MockObjectExprDetector; |
| 16 | +use Rector\PHPUnit\CodeQuality\NodeAnalyser\MockObjectPropertyDetector; |
| 17 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 18 | +use Rector\Rector\AbstractRector; |
| 19 | +use Rector\ValueObject\MethodName; |
| 20 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 21 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 22 | + |
| 23 | +/** |
| 24 | + * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveNeverUsedMockPropertyRector\RemoveNeverUsedMockPropertyRectorTest |
| 25 | + */ |
| 26 | +final class RemoveNeverUsedMockPropertyRector extends AbstractRector |
| 27 | +{ |
| 28 | + public function __construct( |
| 29 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 30 | + private readonly MockObjectPropertyDetector $mockObjectPropertyDetector, |
| 31 | + private readonly MockObjectExprDetector $mockObjectExprDetector, |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + public function getRuleDefinition(): RuleDefinition |
| 36 | + { |
| 37 | + return new RuleDefinition( |
| 38 | + 'Remove never used property mock, only to set expectations', |
| 39 | + [ |
| 40 | + new CodeSample( |
| 41 | + <<<'CODE_SAMPLE' |
| 42 | +use PHPUnit\Framework\TestCase; |
| 43 | +
|
| 44 | +final class SomeTest extends TestCase |
| 45 | +{ |
| 46 | + private $mockProperty = null; |
| 47 | +
|
| 48 | + protected function setUp(): void |
| 49 | + { |
| 50 | + $this->mockProperty = $this->createMock(SomeClass::class); |
| 51 | + $this->mockProperty->expects($this->once()) |
| 52 | + ->method('someMethod') |
| 53 | + ->willReturn('someValue'); |
| 54 | + } |
| 55 | +} |
| 56 | +CODE_SAMPLE |
| 57 | + |
| 58 | + , |
| 59 | + <<<'CODE_SAMPLE' |
| 60 | +use PHPUnit\Framework\TestCase; |
| 61 | +
|
| 62 | +final class SomeTest extends TestCase |
| 63 | +{ |
| 64 | + protected function setUp(): void |
| 65 | + { |
| 66 | + } |
| 67 | +} |
| 68 | +CODE_SAMPLE |
| 69 | + ), |
| 70 | + ] |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @return array<class-string<Node>> |
| 76 | + */ |
| 77 | + public function getNodeTypes(): array |
| 78 | + { |
| 79 | + return [Class_::class]; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param Class_ $node |
| 84 | + */ |
| 85 | + public function refactor(Node $node): ?Node |
| 86 | + { |
| 87 | + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + $setUpClassMethod = $node->getMethod(MethodName::SET_UP); |
| 92 | + if (! $setUpClassMethod instanceof ClassMethod) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + $propertyNamesToCreateMockMethodCalls = $this->mockObjectPropertyDetector->collectFromClassMethod( |
| 97 | + $setUpClassMethod |
| 98 | + ); |
| 99 | + if ($propertyNamesToCreateMockMethodCalls === []) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + $propertyNamesToRemove = []; |
| 104 | + foreach (array_keys($propertyNamesToCreateMockMethodCalls) as $propertyName) { |
| 105 | + if ($this->mockObjectExprDetector->isPropertyMockObjectPassedAsArgument($node, $propertyName)) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + $propertyNamesToRemove[] = $propertyName; |
| 110 | + } |
| 111 | + |
| 112 | + if ($propertyNamesToRemove === []) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + // remove never used mock properties |
| 117 | + foreach ($propertyNamesToRemove as $propertyNameToRemove) { |
| 118 | + // 1. remove property |
| 119 | + $this->removePropertyFromClass($node, $propertyNameToRemove); |
| 120 | + |
| 121 | + // 2. remove assign from setUp() |
| 122 | + $this->removeMockPropertyFromSetUpMethod($setUpClassMethod, $propertyNameToRemove); |
| 123 | + |
| 124 | + // 3. remove expression method calls on this property |
| 125 | + foreach ($node->getMethods() as $classMethod) { |
| 126 | + foreach ((array) $classMethod->stmts as $key => $classMethodStmt) { |
| 127 | + if (! $classMethodStmt instanceof Expression) { |
| 128 | + continue; |
| 129 | + } |
| 130 | + |
| 131 | + if (! $classMethodStmt->expr instanceof MethodCall) { |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + $methodCall = $classMethodStmt->expr; |
| 136 | + $currentMethodCall = $methodCall; |
| 137 | + while ($currentMethodCall->var instanceof MethodCall) { |
| 138 | + $currentMethodCall = $currentMethodCall->var; |
| 139 | + } |
| 140 | + |
| 141 | + if (! $currentMethodCall->var instanceof PropertyFetch) { |
| 142 | + continue; |
| 143 | + } |
| 144 | + |
| 145 | + $propertyFetch = $currentMethodCall->var; |
| 146 | + if (! $this->isName($propertyFetch->name, $propertyNameToRemove)) { |
| 147 | + continue; |
| 148 | + } |
| 149 | + |
| 150 | + unset($classMethod->stmts[$key]); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return $node; |
| 156 | + } |
| 157 | + |
| 158 | + private function removeMockPropertyFromSetUpMethod(ClassMethod $setUpClassMethod, string $propertyName): void |
| 159 | + { |
| 160 | + foreach ((array) $setUpClassMethod->stmts as $key => $classStmt) { |
| 161 | + if (! $classStmt instanceof Expression) { |
| 162 | + continue; |
| 163 | + } |
| 164 | + |
| 165 | + if (! $classStmt->expr instanceof Assign) { |
| 166 | + continue; |
| 167 | + } |
| 168 | + |
| 169 | + $assign = $classStmt->expr; |
| 170 | + if (! $assign->var instanceof PropertyFetch) { |
| 171 | + continue; |
| 172 | + } |
| 173 | + |
| 174 | + $assignedPropertyFetch = $assign->var; |
| 175 | + if (! $this->isName($assignedPropertyFetch->name, $propertyName)) { |
| 176 | + continue; |
| 177 | + } |
| 178 | + |
| 179 | + unset($setUpClassMethod->stmts[$key]); |
| 180 | + return; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private function removePropertyFromClass(Class_ $class, string $propertyNameToRemove): void |
| 185 | + { |
| 186 | + foreach ($class->stmts as $key => $stmt) { |
| 187 | + if (! $stmt instanceof Property) { |
| 188 | + continue; |
| 189 | + } |
| 190 | + |
| 191 | + if (! $this->isName($stmt, $propertyNameToRemove)) { |
| 192 | + continue; |
| 193 | + } |
| 194 | + |
| 195 | + unset($class->stmts[$key]); |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments