From 5f887b6a27b00207534fcbac85050f9d79e4ed48 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 22 Oct 2026 22:00:26 +0200 Subject: [PATCH] [Transform] Deprecate StringToClassConstantRector as niche, never used and needs context --- phpstan.neon | 1 - .../Fixture/fixture.php.inc | 33 ----------------- .../skip_not_found_in_enum_case.php.inc | 8 ---- .../StringToClassConstantRectorTest.php | 28 -------------- .../config/configured_rule.php | 17 --------- .../String_/StringToClassConstantRector.php | 37 ++++--------------- .../ValueObject/StringToClassConstant.php | 3 ++ 7 files changed, 11 insertions(+), 116 deletions(-) delete mode 100644 rules-tests/Transform/Rector/String_/StringToClassConstantRector/Fixture/fixture.php.inc delete mode 100644 rules-tests/Transform/Rector/String_/StringToClassConstantRector/Fixture/skip_not_found_in_enum_case.php.inc delete mode 100644 rules-tests/Transform/Rector/String_/StringToClassConstantRector/StringToClassConstantRectorTest.php delete mode 100644 rules-tests/Transform/Rector/String_/StringToClassConstantRector/config/configured_rule.php diff --git a/phpstan.neon b/phpstan.neon index 1e5082b3d5a..7536b84150e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -236,7 +236,6 @@ parameters: identifier: rector.registerRector paths: - rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php - - rules/Transform/Rector/String_/StringToClassConstantRector.php # lack of generic array in nikic/php-parser - diff --git a/rules-tests/Transform/Rector/String_/StringToClassConstantRector/Fixture/fixture.php.inc b/rules-tests/Transform/Rector/String_/StringToClassConstantRector/Fixture/fixture.php.inc deleted file mode 100644 index 2929e3ef12b..00000000000 --- a/rules-tests/Transform/Rector/String_/StringToClassConstantRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,33 +0,0 @@ - 'compile', - 'compiler.to_class' => 'compile' - ]; - } -} - -?> ------ - 'compile', - \Yet\AnotherClass::class => 'compile' - ]; - } -} - -?> diff --git a/rules-tests/Transform/Rector/String_/StringToClassConstantRector/Fixture/skip_not_found_in_enum_case.php.inc b/rules-tests/Transform/Rector/String_/StringToClassConstantRector/Fixture/skip_not_found_in_enum_case.php.inc deleted file mode 100644 index 5101b0fed1a..00000000000 --- a/rules-tests/Transform/Rector/String_/StringToClassConstantRector/Fixture/skip_not_found_in_enum_case.php.inc +++ /dev/null @@ -1,8 +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/String_/StringToClassConstantRector/config/configured_rule.php b/rules-tests/Transform/Rector/String_/StringToClassConstantRector/config/configured_rule.php deleted file mode 100644 index ee63c98a74f..00000000000 --- a/rules-tests/Transform/Rector/String_/StringToClassConstantRector/config/configured_rule.php +++ /dev/null @@ -1,17 +0,0 @@ -ruleWithConfiguration(StringToClassConstantRector::class, [ - - new StringToClassConstant('compiler.post_dump', 'Yet\AnotherClass', 'CONSTANT'), - new StringToClassConstant('compiler.to_class', 'Yet\AnotherClass', 'class'), - - ]); -}; diff --git a/rules/Transform/Rector/String_/StringToClassConstantRector.php b/rules/Transform/Rector/String_/StringToClassConstantRector.php index 36e37782cc1..e18b0c817dc 100644 --- a/rules/Transform/Rector/String_/StringToClassConstantRector.php +++ b/rules/Transform/Rector/String_/StringToClassConstantRector.php @@ -6,29 +6,19 @@ use PhpParser\Node; use PhpParser\Node\Scalar\String_; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; use Rector\Contract\Rector\ConfigurableRectorInterface; -use Rector\PhpParser\Node\Value\ValueResolver; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\ConfiguredCodeSample; use Rector\RuleDoc\RuleDefinition; use Rector\Transform\ValueObject\StringToClassConstant; -use Webmozart\Assert\Assert; /** - * @see \Rector\Tests\Transform\Rector\String_\StringToClassConstantRector\StringToClassConstantRectorTest + * @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 StringToClassConstantRector extends AbstractRector implements ConfigurableRectorInterface +final class StringToClassConstantRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface { - /** - * @var StringToClassConstant[] - */ - private array $stringsToClassConstants = []; - - public function __construct( - private readonly ValueResolver $valueResolver - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Change strings to specific constants', [ @@ -71,18 +61,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - foreach ($this->stringsToClassConstants as $stringToClassConstant) { - if (! $this->valueResolver->isValue($node, $stringToClassConstant->getString())) { - continue; - } - - return $this->nodeFactory->createClassConstFetch( - $stringToClassConstant->getClass(), - $stringToClassConstant->getConstant() - ); - } - - 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 + )); } /** @@ -90,8 +72,5 @@ public function refactor(Node $node): ?Node */ public function configure(array $configuration): void { - Assert::allIsAOf($configuration, StringToClassConstant::class); - - $this->stringsToClassConstants = $configuration; } } diff --git a/rules/Transform/ValueObject/StringToClassConstant.php b/rules/Transform/ValueObject/StringToClassConstant.php index 012d6855fb5..dc8de619ed1 100644 --- a/rules/Transform/ValueObject/StringToClassConstant.php +++ b/rules/Transform/ValueObject/StringToClassConstant.php @@ -6,6 +6,9 @@ use Rector\Validation\RectorAssert; +/** + * @api used in deprecated StringToClassConstantRector configs + */ final readonly class StringToClassConstant { public function __construct(