|
| 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\PropertyFetch; |
| 9 | +use PhpParser\Node\Expr\Variable; |
| 10 | +use PhpParser\Node\Identifier; |
| 11 | +use PhpParser\Node\Stmt\Class_; |
| 12 | +use PhpParser\Node\VarLikeIdentifier; |
| 13 | +use Rector\PHPUnit\CodeQuality\NodeAnalyser\MockObjectPropertyDetector; |
| 14 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 15 | +use Rector\Rector\AbstractRector; |
| 16 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 17 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 18 | + |
| 19 | +/** |
| 20 | + * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SuffixMockObjectPropertyRector\SuffixMockObjectPropertyRectorTest |
| 21 | + */ |
| 22 | +final class SuffixMockObjectPropertyRector extends AbstractRector |
| 23 | +{ |
| 24 | + public function __construct( |
| 25 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 26 | + private readonly MockObjectPropertyDetector $mockObjectPropertyDetector |
| 27 | + ) { |
| 28 | + } |
| 29 | + |
| 30 | + public function getRuleDefinition(): RuleDefinition |
| 31 | + { |
| 32 | + return new RuleDefinition( |
| 33 | + 'Make mock objet properties names suffix with "Mock" to clearly separate from real objects later on', |
| 34 | + [ |
| 35 | + new CodeSample( |
| 36 | + <<<'CODE_SAMPLE' |
| 37 | +use PHPUnit\Framework\TestCase; |
| 38 | +use PHPUnit\Framework\MockObject\MockObject; |
| 39 | +
|
| 40 | +final class MockingEntity extends TestCase |
| 41 | +{ |
| 42 | + private MockObject $simpleObject; |
| 43 | +
|
| 44 | + protected function setUp(): void |
| 45 | + { |
| 46 | + $this->simpleObject = $this->createMock(SimpleObject::class); |
| 47 | + } |
| 48 | +
|
| 49 | + public function test() |
| 50 | + { |
| 51 | + $this->simpleObject->method('someMethod')->willReturn('someValue'); |
| 52 | + } |
| 53 | +} |
| 54 | +CODE_SAMPLE |
| 55 | + , |
| 56 | + <<<'CODE_SAMPLE' |
| 57 | +use PHPUnit\Framework\TestCase; |
| 58 | +use PHPUnit\Framework\MockObject\MockObject; |
| 59 | +
|
| 60 | +final class MockingEntity extends TestCase |
| 61 | +{ |
| 62 | + private MockObject $simpleObjectMock; |
| 63 | +
|
| 64 | + protected function setUp(): void |
| 65 | + { |
| 66 | + $this->simpleObjectMock = $this->createMock(SimpleObject::class); |
| 67 | + } |
| 68 | +
|
| 69 | + public function test() |
| 70 | + { |
| 71 | + $this->simpleObjectMock->method('someMethod')->willReturn('someValue'); |
| 72 | + } |
| 73 | +} |
| 74 | +CODE_SAMPLE |
| 75 | + ), |
| 76 | + ] |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return array<class-string<Node>> |
| 82 | + */ |
| 83 | + public function getNodeTypes(): array |
| 84 | + { |
| 85 | + return [Class_::class]; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @param Class_ $node |
| 90 | + */ |
| 91 | + public function refactor(Node $node): ?Class_ |
| 92 | + { |
| 93 | + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + $hasChanged = false; |
| 98 | + |
| 99 | + foreach ($node->getProperties() as $property) { |
| 100 | + if (! $this->mockObjectPropertyDetector->detect($property)) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + $propertyName = $this->getName($property); |
| 105 | + if (str_ends_with($propertyName, 'Mock')) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + $newPropertyName = $propertyName . 'Mock'; |
| 110 | + $property->props[0]->name = new VarLikeIdentifier($newPropertyName); |
| 111 | + |
| 112 | + $this->renamePropertyUsagesInClass($node, $propertyName, $newPropertyName); |
| 113 | + $hasChanged = true; |
| 114 | + } |
| 115 | + |
| 116 | + if (! $hasChanged) { |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + return $node; |
| 121 | + } |
| 122 | + |
| 123 | + private function renamePropertyUsagesInClass(Class_ $class, string $oldPropertyName, string $newPropertyName): void |
| 124 | + { |
| 125 | + $this->traverseNodesWithCallable($class, function (Node $node) use ( |
| 126 | + $oldPropertyName, |
| 127 | + $newPropertyName |
| 128 | + ): ?Node { |
| 129 | + if (! $node instanceof PropertyFetch) { |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + // is local property? |
| 134 | + if (! $node->var instanceof Variable && ! $this->isName($node->var, 'this')) { |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | + if (! $this->isName($node->name, $oldPropertyName)) { |
| 139 | + return null; |
| 140 | + } |
| 141 | + |
| 142 | + $node->name = new Identifier($newPropertyName); |
| 143 | + return $node; |
| 144 | + }); |
| 145 | + } |
| 146 | +} |
0 commit comments