From 0f2181d772054d211f6a5f4288ceeb91e4b3d9aa Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 22 Oct 2026 21:58:36 +0200 Subject: [PATCH] [Renaming] Deprecate RenameStaticMethodRector as niche, never used and needs context --- bench/rename_static_method_bucket_bench.php | 99 ------------------- phpstan.neon | 1 - .../Fixture/fixture.php.inc | 21 ---- .../Fixture/fixture2.php.inc | 27 ----- .../RenameStaticMethodRectorTest.php | 28 ------ .../Source/FormMacros.php | 10 -- .../config/configured_rule.php | 24 ----- .../StaticCall/RenameStaticMethodRector.php | 50 ++-------- .../ValueObject/RenameStaticMethod.php | 3 + 9 files changed, 11 insertions(+), 252 deletions(-) delete mode 100644 bench/rename_static_method_bucket_bench.php delete mode 100644 rules-tests/Renaming/Rector/StaticCall/RenameStaticMethodRector/Fixture/fixture.php.inc delete mode 100644 rules-tests/Renaming/Rector/StaticCall/RenameStaticMethodRector/Fixture/fixture2.php.inc delete mode 100644 rules-tests/Renaming/Rector/StaticCall/RenameStaticMethodRector/RenameStaticMethodRectorTest.php delete mode 100644 rules-tests/Renaming/Rector/StaticCall/RenameStaticMethodRector/Source/FormMacros.php delete mode 100644 rules-tests/Renaming/Rector/StaticCall/RenameStaticMethodRector/config/configured_rule.php diff --git a/bench/rename_static_method_bucket_bench.php b/bench/rename_static_method_bucket_bench.php deleted file mode 100644 index b3df4e4038c..00000000000 --- a/bench/rename_static_method_bucket_bench.php +++ /dev/null @@ -1,99 +0,0 @@ -create(); -$rectorConfig->boot(); - -/** @var TestingParser $testingParser */ -$testingParser = $rectorConfig->make(TestingParser::class); - -/** @var BetterNodeFinder $betterNodeFinder */ -$betterNodeFinder = $rectorConfig->make(BetterNodeFinder::class); - -$rector = $rectorConfig->make(RenameStaticMethodRector::class); - -$staticMethodRenames = []; -for ($i = 0; $i < RENAMES; ++$i) { - $staticMethodRenames[] = new RenameStaticMethod('Bench\\SomeClass' . $i, 'oldMethod' . $i, 'Bench\\NewClass' . $i, 'newMethod' . $i); -} - -$rector->configure($staticMethodRenames); - -$source = "parseFileToDecoratedNodes($sourceFilePath); - -/** @var StaticCall[] $staticCalls */ -$staticCalls = $betterNodeFinder->findInstanceOf($stmts, StaticCall::class); - -$cpuSeconds = static function (): float { - $usage = getrusage(); - return $usage['ru_utime.tv_sec'] + $usage['ru_utime.tv_usec'] / 1_000_000 - + $usage['ru_stime.tv_sec'] + $usage['ru_stime.tv_usec'] / 1_000_000; -}; - -$run = static function () use ($rector, $staticCalls): void { - foreach ($staticCalls as $staticCall) { - $rector->refactor($staticCall); - } -}; - -// warm up autoloading/reflection caches once, excluded from timing -$run(); - -$wallStart = microtime(true); -$cpuStart = $cpuSeconds(); - -for ($repeat = 0; $repeat < REPEAT; ++$repeat) { - $run(); -} - -$cpu = $cpuSeconds() - $cpuStart; -$wall = microtime(true) - $wallStart; - -@unlink($sourceFilePath); - -$totalCalls = REPEAT * count($staticCalls); - -printf('renames: %d, calls: %d, repeats: %d, refactor() calls: %d%s', RENAMES, count($staticCalls), REPEAT, $totalCalls, PHP_EOL); -printf('CPU: %.4f s (%.3f us/call)%s', $cpu, $cpu / $totalCalls * 1_000_000, PHP_EOL); -printf('wall: %.4f s%s', $wall, PHP_EOL); diff --git a/phpstan.neon b/phpstan.neon index f5aec0053bc..1741ded25c4 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -237,7 +237,6 @@ parameters: paths: - rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php - rules/Removing/Rector/Class_/RemoveTraitUseRector.php - - rules/Renaming/Rector/StaticCall/RenameStaticMethodRector.php - rules/Transform/Rector/String_/StringToClassConstantRector.php # lack of generic array in nikic/php-parser diff --git a/rules-tests/Renaming/Rector/StaticCall/RenameStaticMethodRector/Fixture/fixture.php.inc b/rules-tests/Renaming/Rector/StaticCall/RenameStaticMethodRector/Fixture/fixture.php.inc deleted file mode 100644 index abf7c2f3d3f..00000000000 --- a/rules-tests/Renaming/Rector/StaticCall/RenameStaticMethodRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,21 +0,0 @@ - ------ - diff --git a/rules-tests/Renaming/Rector/StaticCall/RenameStaticMethodRector/Fixture/fixture2.php.inc b/rules-tests/Renaming/Rector/StaticCall/RenameStaticMethodRector/Fixture/fixture2.php.inc deleted file mode 100644 index 7da36e09ee4..00000000000 --- a/rules-tests/Renaming/Rector/StaticCall/RenameStaticMethodRector/Fixture/fixture2.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/rules-tests/Renaming/Rector/StaticCall/RenameStaticMethodRector/RenameStaticMethodRectorTest.php b/rules-tests/Renaming/Rector/StaticCall/RenameStaticMethodRector/RenameStaticMethodRectorTest.php deleted file mode 100644 index 64037383072..00000000000 --- a/rules-tests/Renaming/Rector/StaticCall/RenameStaticMethodRector/RenameStaticMethodRectorTest.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/Renaming/Rector/StaticCall/RenameStaticMethodRector/Source/FormMacros.php b/rules-tests/Renaming/Rector/StaticCall/RenameStaticMethodRector/Source/FormMacros.php deleted file mode 100644 index 5ceaa908ea8..00000000000 --- a/rules-tests/Renaming/Rector/StaticCall/RenameStaticMethodRector/Source/FormMacros.php +++ /dev/null @@ -1,10 +0,0 @@ -ruleWithConfiguration(RenameStaticMethodRector::class, [ - - new RenameStaticMethod(Html::class, 'add', Html::class, 'addHtml'), - new RenameStaticMethod( - FormMacros::class, - 'renderFormBegin', - 'Nette\Bridges\FormsLatte\Runtime', - 'renderFormBegin' - ), - - ]); -}; diff --git a/rules/Renaming/Rector/StaticCall/RenameStaticMethodRector.php b/rules/Renaming/Rector/StaticCall/RenameStaticMethodRector.php index e5b619f1b20..60f9a5fd120 100644 --- a/rules/Renaming/Rector/StaticCall/RenameStaticMethodRector.php +++ b/rules/Renaming/Rector/StaticCall/RenameStaticMethodRector.php @@ -6,25 +6,19 @@ use PhpParser\Node; use PhpParser\Node\Expr\StaticCall; -use PhpParser\Node\Identifier; -use PhpParser\Node\Name\FullyQualified; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; use Rector\Contract\Rector\ConfigurableRectorInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; use Rector\Renaming\ValueObject\RenameStaticMethod; use Rector\RuleDoc\CodeSample\ConfiguredCodeSample; use Rector\RuleDoc\RuleDefinition; -use Webmozart\Assert\Assert; /** - * @see \Rector\Tests\Renaming\Rector\StaticCall\RenameStaticMethodRector\RenameStaticMethodRectorTest + * @deprecated This rule is deprecated, as too niche, never used and needs project context. Write a custom rule for the exact project case instead. */ -final class RenameStaticMethodRector extends AbstractRector implements ConfigurableRectorInterface +final class RenameStaticMethodRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface { - /** - * @var array - */ - private array $staticMethodRenamesByOldMethod = []; - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Turn method names to new ones', [ @@ -49,20 +43,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $methodName = $this->getName($node->name); - if ($methodName === null) { - return null; - } - - foreach ($this->staticMethodRenamesByOldMethod[strtolower($methodName)] ?? [] as $staticMethodRename) { - if (! $this->isObjectType($node->class, $staticMethodRename->getOldObjectType())) { - continue; - } - - return $this->rename($node, $staticMethodRename); - } - - return null; + throw new ShouldNotHappenException(sprintf( + '"%s" rule is deprecated, as too niche and part of no set; write a custom rule for the exact project case instead', + self::class + )); } /** @@ -70,23 +54,5 @@ public function refactor(Node $node): ?Node */ public function configure(array $configuration): void { - Assert::allIsAOf($configuration, RenameStaticMethod::class); - - // bucket by lowercased old method name (method names are case-insensitive) - $this->staticMethodRenamesByOldMethod = []; - foreach ($configuration as $staticMethodRename) { - $this->staticMethodRenamesByOldMethod[strtolower($staticMethodRename->getOldMethod())][] = $staticMethodRename; - } - } - - private function rename(StaticCall $staticCall, RenameStaticMethod $renameStaticMethod): StaticCall - { - $staticCall->name = new Identifier($renameStaticMethod->getNewMethod()); - - if ($renameStaticMethod->hasClassChanged()) { - $staticCall->class = new FullyQualified($renameStaticMethod->getNewClass()); - } - - return $staticCall; } } diff --git a/rules/Renaming/ValueObject/RenameStaticMethod.php b/rules/Renaming/ValueObject/RenameStaticMethod.php index 3df7fb0b12d..7cb3716e65e 100644 --- a/rules/Renaming/ValueObject/RenameStaticMethod.php +++ b/rules/Renaming/ValueObject/RenameStaticMethod.php @@ -7,6 +7,9 @@ use PHPStan\Type\ObjectType; use Rector\Validation\RectorAssert; +/** + * @api used in deprecated RenameStaticMethodRector configs + */ final readonly class RenameStaticMethod { public function __construct(