Skip to content

Commit 1dc79fb

Browse files
committed
[Deprecate] SuffixMockObjectPropertyRector as it can produce invalid variable names
1 parent a680469 commit 1dc79fb

4 files changed

Lines changed: 10 additions & 153 deletions

File tree

‎rules-tests/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector/Fixture/some_test.php.inc‎

Lines changed: 0 additions & 49 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector/SuffixMockObjectPropertyRectorTest.php‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector/config/configured_rule.php‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎rules/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector.php‎

Lines changed: 10 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,19 @@
55
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;
66

77
use PhpParser\Node;
8-
use PhpParser\Node\Expr\PropertyFetch;
9-
use PhpParser\Node\Expr\Variable;
10-
use PhpParser\Node\Identifier;
118
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;
1511
use Rector\Rector\AbstractRector;
1612
use Rector\RuleDoc\CodeSample\CodeSample;
1713
use Rector\RuleDoc\RuleDefinition;
1814

1915
/**
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.
2118
*/
22-
final class SuffixMockObjectPropertyRector extends AbstractRector
19+
final class SuffixMockObjectPropertyRector extends AbstractRector implements DeprecatedInterface
2320
{
24-
public function __construct(
25-
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
26-
private readonly MockObjectPropertyDetector $mockObjectPropertyDetector
27-
) {
28-
}
29-
3021
public function getRuleDefinition(): RuleDefinition
3122
{
3223
return new RuleDefinition(
@@ -88,59 +79,11 @@ public function getNodeTypes(): array
8879
/**
8980
* @param Class_ $node
9081
*/
91-
public function refactor(Node $node): ?Class_
82+
public function refactor(Node $node): ?Node
9283
{
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+
));
14588
}
14689
}

0 commit comments

Comments
 (0)