From 8ebbf4fe68ea7ed615a86ee78b4a6f44d6b1ad3f Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 22 Oct 2026 12:53:26 +0200 Subject: [PATCH] [Transform] Deprecate NewToStaticCallRector as too niche and requiring careful context --- .../Fixture/fixture.php.inc | 31 ------------ .../Fixture/skip_another_class.php.inc | 15 ------ .../NewToStaticCallRectorTest.php | 28 ----------- .../Source/FromNewClass.php | 10 ---- .../Source/IntoStaticClass.php | 14 ------ .../config/configured_rule.php | 17 ------- .../Rector/New_/NewToStaticCallRector.php | 47 ++++--------------- .../Transform/ValueObject/NewToStaticCall.php | 37 --------------- 8 files changed, 10 insertions(+), 189 deletions(-) delete mode 100644 rules-tests/Transform/Rector/New_/NewToStaticCallRector/Fixture/fixture.php.inc delete mode 100644 rules-tests/Transform/Rector/New_/NewToStaticCallRector/Fixture/skip_another_class.php.inc delete mode 100644 rules-tests/Transform/Rector/New_/NewToStaticCallRector/NewToStaticCallRectorTest.php delete mode 100644 rules-tests/Transform/Rector/New_/NewToStaticCallRector/Source/FromNewClass.php delete mode 100644 rules-tests/Transform/Rector/New_/NewToStaticCallRector/Source/IntoStaticClass.php delete mode 100644 rules-tests/Transform/Rector/New_/NewToStaticCallRector/config/configured_rule.php delete mode 100644 rules/Transform/ValueObject/NewToStaticCall.php diff --git a/rules-tests/Transform/Rector/New_/NewToStaticCallRector/Fixture/fixture.php.inc b/rules-tests/Transform/Rector/New_/NewToStaticCallRector/Fixture/fixture.php.inc deleted file mode 100644 index 81301d714e3..00000000000 --- a/rules-tests/Transform/Rector/New_/NewToStaticCallRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - diff --git a/rules-tests/Transform/Rector/New_/NewToStaticCallRector/Fixture/skip_another_class.php.inc b/rules-tests/Transform/Rector/New_/NewToStaticCallRector/Fixture/skip_another_class.php.inc deleted file mode 100644 index c5ef1df0a2d..00000000000 --- a/rules-tests/Transform/Rector/New_/NewToStaticCallRector/Fixture/skip_another_class.php.inc +++ /dev/null @@ -1,15 +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/Transform/Rector/New_/NewToStaticCallRector/Source/FromNewClass.php b/rules-tests/Transform/Rector/New_/NewToStaticCallRector/Source/FromNewClass.php deleted file mode 100644 index ccff2a362df..00000000000 --- a/rules-tests/Transform/Rector/New_/NewToStaticCallRector/Source/FromNewClass.php +++ /dev/null @@ -1,10 +0,0 @@ -ruleWithConfiguration( - NewToStaticCallRector::class, - [new NewToStaticCall(FromNewClass::class, IntoStaticClass::class, 'run')] - ); -}; diff --git a/rules/Transform/Rector/New_/NewToStaticCallRector.php b/rules/Transform/Rector/New_/NewToStaticCallRector.php index d5444feb149..5511bdd8fe3 100644 --- a/rules/Transform/Rector/New_/NewToStaticCallRector.php +++ b/rules/Transform/Rector/New_/NewToStaticCallRector.php @@ -6,27 +6,21 @@ use PhpParser\Node; use PhpParser\Node\Expr\New_; -use Rector\Contract\Rector\ConfigurableRectorInterface; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; -use Rector\RuleDoc\CodeSample\ConfiguredCodeSample; +use Rector\RuleDoc\CodeSample\CodeSample; use Rector\RuleDoc\RuleDefinition; -use Rector\Transform\ValueObject\NewToStaticCall; -use Webmozart\Assert\Assert; /** - * @see \Rector\Tests\Transform\Rector\New_\NewToStaticCallRector\NewToStaticCallRectorTest + * @deprecated This rule is deprecated, as too niche, never used, and requires careful context to pick the correct static call. Write a custom rule for the exact project case instead. */ -final class NewToStaticCallRector extends AbstractRector implements ConfigurableRectorInterface +final class NewToStaticCallRector extends AbstractRector implements DeprecatedInterface { - /** - * @var NewToStaticCall[] - */ - private array $typeToStaticCalls = []; - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Change new Object to static call', [ - new ConfiguredCodeSample( + new CodeSample( <<<'CODE_SAMPLE' class SomeClass { @@ -46,8 +40,6 @@ public function run() } } CODE_SAMPLE - , - [new NewToStaticCall('Cookie', 'Cookie', 'create')] ), ]); } @@ -65,28 +57,9 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - foreach ($this->typeToStaticCalls as $typeToStaticCall) { - if (! $this->isObjectType($node->class, $typeToStaticCall->getObjectType())) { - continue; - } - - return $this->nodeFactory->createStaticCall( - $typeToStaticCall->getStaticCallClass(), - $typeToStaticCall->getStaticCallMethod(), - $node->args - ); - } - - return null; - } - - /** - * @param mixed[] $configuration - */ - public function configure(array $configuration): void - { - Assert::allIsAOf($configuration, NewToStaticCall::class); - - $this->typeToStaticCalls = $configuration; + throw new ShouldNotHappenException(sprintf( + '"%s" rule is deprecated, as too niche and never used. Write a custom rule for the exact project case instead', + self::class + )); } } diff --git a/rules/Transform/ValueObject/NewToStaticCall.php b/rules/Transform/ValueObject/NewToStaticCall.php deleted file mode 100644 index bac0603487d..00000000000 --- a/rules/Transform/ValueObject/NewToStaticCall.php +++ /dev/null @@ -1,37 +0,0 @@ -type); - } - - public function getStaticCallClass(): string - { - return $this->staticCallClass; - } - - public function getStaticCallMethod(): string - { - return $this->staticCallMethod; - } -}