|
5 | 5 | namespace Rector\PHPUnit\CodeQuality\Rector\Class_; |
6 | 6 |
|
7 | 7 | use PhpParser\Node; |
8 | | -use PhpParser\Node\Expr\PropertyFetch; |
9 | | -use PhpParser\Node\Expr\Variable; |
10 | | -use PhpParser\Node\Identifier; |
11 | 8 | use PhpParser\Node\Stmt\Class_; |
12 | | -use PhpParser\Node\VarLikeIdentifier; |
13 | | -use Rector\PHPUnit\CodeQuality\NodeAnalyser\MockObjectPropertyDetector; |
14 | | -use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 9 | +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; |
| 10 | +use Rector\Exception\ShouldNotHappenException; |
15 | 11 | use Rector\Rector\AbstractRector; |
16 | 12 | use Rector\RuleDoc\CodeSample\CodeSample; |
17 | 13 | use Rector\RuleDoc\RuleDefinition; |
18 | 14 |
|
19 | 15 | /** |
20 | | - * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SuffixMockObjectPropertyRector\SuffixMockObjectPropertyRectorTest |
| 16 | + * @deprecated This rule is deprecated, as renaming properties can produce invalid variable names and collide with |
| 17 | + * existing ones. Use a coding standard to enforce the "Mock" suffix instead. |
21 | 18 | */ |
22 | | -final class SuffixMockObjectPropertyRector extends AbstractRector |
| 19 | +final class SuffixMockObjectPropertyRector extends AbstractRector implements DeprecatedInterface |
23 | 20 | { |
24 | | - public function __construct( |
25 | | - private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
26 | | - private readonly MockObjectPropertyDetector $mockObjectPropertyDetector |
27 | | - ) { |
28 | | - } |
29 | | - |
30 | 21 | public function getRuleDefinition(): RuleDefinition |
31 | 22 | { |
32 | 23 | return new RuleDefinition( |
@@ -88,59 +79,11 @@ public function getNodeTypes(): array |
88 | 79 | /** |
89 | 80 | * @param Class_ $node |
90 | 81 | */ |
91 | | - public function refactor(Node $node): ?Class_ |
| 82 | + public function refactor(Node $node): ?Node |
92 | 83 | { |
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 | | - }); |
| 84 | + throw new ShouldNotHappenException(sprintf( |
| 85 | + '"%s" is deprecated, as renaming properties can produce invalid variable names. Use a coding standard to enforce the "Mock" suffix instead.', |
| 86 | + self::class, |
| 87 | + )); |
145 | 88 | } |
146 | 89 | } |
0 commit comments