From 1dc79fb481ee4caa1cc86923acc3b390a3f0af55 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 11 Oct 2026 00:28:39 +0200 Subject: [PATCH] [Deprecate] SuffixMockObjectPropertyRector as it can produce invalid variable names --- .../Fixture/some_test.php.inc | 49 ------------ .../SuffixMockObjectPropertyRectorTest.php | 28 ------- .../config/configured_rule.php | 9 --- .../Class_/SuffixMockObjectPropertyRector.php | 77 +++---------------- 4 files changed, 10 insertions(+), 153 deletions(-) delete mode 100644 rules-tests/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector/Fixture/some_test.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector/SuffixMockObjectPropertyRectorTest.php delete mode 100644 rules-tests/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector/config/configured_rule.php diff --git a/rules-tests/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector/Fixture/some_test.php.inc b/rules-tests/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector/Fixture/some_test.php.inc deleted file mode 100644 index 6f69b64b..00000000 --- a/rules-tests/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector/Fixture/some_test.php.inc +++ /dev/null @@ -1,49 +0,0 @@ -someProperty = $this->createMock(\stdClass::class); - } - - public function testMocks(): void - { - $this->someProperty->expects($this->once())->method('someMethod')->willReturn('someValue'); - } -} - -?> ------ -somePropertyMock = $this->createMock(\stdClass::class); - } - - public function testMocks(): void - { - $this->somePropertyMock->expects($this->once())->method('someMethod')->willReturn('someValue'); - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector/SuffixMockObjectPropertyRectorTest.php b/rules-tests/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector/SuffixMockObjectPropertyRectorTest.php deleted file mode 100644 index 5bdbb913..00000000 --- a/rules-tests/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector/SuffixMockObjectPropertyRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector/config/configured_rule.php deleted file mode 100644 index e7a296d0..00000000 --- a/rules-tests/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector/config/configured_rule.php +++ /dev/null @@ -1,9 +0,0 @@ -withRules([SuffixMockObjectPropertyRector::class]); diff --git a/rules/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector.php b/rules/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector.php index 68644073..56ecbe8f 100644 --- a/rules/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector.php +++ b/rules/CodeQuality/Rector/Class_/SuffixMockObjectPropertyRector.php @@ -5,28 +5,19 @@ namespace Rector\PHPUnit\CodeQuality\Rector\Class_; use PhpParser\Node; -use PhpParser\Node\Expr\PropertyFetch; -use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\Class_; -use PhpParser\Node\VarLikeIdentifier; -use Rector\PHPUnit\CodeQuality\NodeAnalyser\MockObjectPropertyDetector; -use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; use Rector\RuleDoc\RuleDefinition; /** - * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SuffixMockObjectPropertyRector\SuffixMockObjectPropertyRectorTest + * @deprecated This rule is deprecated, as renaming properties can produce invalid variable names and collide with + * existing ones. Use a coding standard to enforce the "Mock" suffix instead. */ -final class SuffixMockObjectPropertyRector extends AbstractRector +final class SuffixMockObjectPropertyRector extends AbstractRector implements DeprecatedInterface { - public function __construct( - private readonly TestsNodeAnalyzer $testsNodeAnalyzer, - private readonly MockObjectPropertyDetector $mockObjectPropertyDetector - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( @@ -88,59 +79,11 @@ public function getNodeTypes(): array /** * @param Class_ $node */ - public function refactor(Node $node): ?Class_ + public function refactor(Node $node): ?Node { - if (! $this->testsNodeAnalyzer->isInTestClass($node)) { - return null; - } - - $hasChanged = false; - - foreach ($node->getProperties() as $property) { - if (! $this->mockObjectPropertyDetector->detect($property)) { - continue; - } - - $propertyName = $this->getName($property); - if (str_ends_with($propertyName, 'Mock')) { - continue; - } - - $newPropertyName = $propertyName . 'Mock'; - $property->props[0]->name = new VarLikeIdentifier($newPropertyName); - - $this->renamePropertyUsagesInClass($node, $propertyName, $newPropertyName); - $hasChanged = true; - } - - if (! $hasChanged) { - return null; - } - - return $node; - } - - private function renamePropertyUsagesInClass(Class_ $class, string $oldPropertyName, string $newPropertyName): void - { - $this->traverseNodesWithCallable($class, function (Node $node) use ( - $oldPropertyName, - $newPropertyName - ): ?Node { - if (! $node instanceof PropertyFetch) { - return null; - } - - // is local property? - if (! $node->var instanceof Variable && ! $this->isName($node->var, 'this')) { - return null; - } - - if (! $this->isName($node->name, $oldPropertyName)) { - return null; - } - - $node->name = new Identifier($newPropertyName); - return $node; - }); + throw new ShouldNotHappenException(sprintf( + '"%s" is deprecated, as renaming properties can produce invalid variable names. Use a coding standard to enforce the "Mock" suffix instead.', + self::class, + )); } }