diff --git a/rules-tests/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector/AttributeKeyToClassConstFetchRectorTest.php b/rules-tests/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector/AttributeKeyToClassConstFetchRectorTest.php deleted file mode 100644 index cc6802e5379..00000000000 --- a/rules-tests/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector/AttributeKeyToClassConstFetchRectorTest.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/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector/Fixture/not_replacing_existing_constant.php.inc b/rules-tests/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector/Fixture/not_replacing_existing_constant.php.inc deleted file mode 100644 index 5c7de7610ea..00000000000 --- a/rules-tests/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector/Fixture/not_replacing_existing_constant.php.inc +++ /dev/null @@ -1,15 +0,0 @@ - diff --git a/rules-tests/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector/Fixture/skip_already.php.inc b/rules-tests/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector/Fixture/skip_already.php.inc deleted file mode 100644 index 292b812e69a..00000000000 --- a/rules-tests/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector/Fixture/skip_already.php.inc +++ /dev/null @@ -1,15 +0,0 @@ - diff --git a/rules-tests/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector/Fixture/some_class.php.inc b/rules-tests/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector/Fixture/some_class.php.inc deleted file mode 100644 index 3423936f438..00000000000 --- a/rules-tests/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector/Fixture/some_class.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/rules-tests/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector/Source/Constant.php b/rules-tests/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector/Source/Constant.php deleted file mode 100644 index 83a9cfc9f4a..00000000000 --- a/rules-tests/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector/Source/Constant.php +++ /dev/null @@ -1,10 +0,0 @@ -ruleWithConfiguration(AttributeKeyToClassConstFetchRector::class, [ - new AttributeKeyToClassConstFetch('Doctrine\ORM\Mapping\Column', 'type', 'Doctrine\DBAL\Types\Types', [ - 'string' => 'STRING', - ]), - new AttributeKeyToClassConstFetch('Rector\Tests\Transform\Rector\Attribute\AttributeKeyToClassConstFetchRector\Source\TestAttribute', 'type', 'Rector\Tests\Transform\Rector\Attribute\AttributeKeyToClassConstFetchRector\Source\Constant', [ - 'value' => 'VALUE', - ]), - ]); -}; diff --git a/rules/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector.php b/rules/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector.php index 550d7141eef..553db415aa0 100644 --- a/rules/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector.php +++ b/rules/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector.php @@ -5,41 +5,27 @@ namespace Rector\Transform\Rector\Attribute; use PhpParser\Node; -use PhpParser\Node\Arg; -use PhpParser\Node\AttributeGroup; use PhpParser\Node\Expr\ArrowFunction; -use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\Closure; -use PhpParser\Node\Identifier; use PhpParser\Node\Param; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Interface_; use PhpParser\Node\Stmt\Property; +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\AttributeKeyToClassConstFetch; -use Webmozart\Assert\Assert; /** - * @see \Rector\Tests\Transform\Rector\Attribute\AttributeKeyToClassConstFetchRector\AttributeKeyToClassConstFetchRectorTest + * @deprecated This rule is deprecated, as only used for 2 cases now covered by dedicated rules: ColumnTypeToTypesConstantRector in rector-doctrine and EmailModeToConstantRector in rector-symfony. Write a custom rule for the exact project case instead. */ -final class AttributeKeyToClassConstFetchRector extends AbstractRector implements ConfigurableRectorInterface +final class AttributeKeyToClassConstFetchRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface { - /** - * @var AttributeKeyToClassConstFetch[] - */ - private array $attributeKeysToClassConstFetches = []; - - public function __construct( - private readonly ValueResolver $valueResolver - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Replace key value on specific attribute to class constant', [ @@ -96,24 +82,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node->attrGroups === []) { - return null; - } - - $hasChanged = false; - foreach ($this->attributeKeysToClassConstFetches as $attributeKeyToClassConstFetch) { - foreach ($node->attrGroups as $attrGroup) { - if ($this->processToClassConstFetch($attrGroup, $attributeKeyToClassConstFetch)) { - $hasChanged = true; - } - } - } - - if ($hasChanged) { - return $node; - } - - return null; + throw new ShouldNotHappenException(sprintf( + '"%s" rule is deprecated, as only used for 2 cases now covered by dedicated rules; write a custom rule for the exact project case instead', + self::class + )); } /** @@ -121,64 +93,5 @@ public function refactor(Node $node): ?Node */ public function configure(array $configuration): void { - Assert::allIsAOf($configuration, AttributeKeyToClassConstFetch::class); - - $this->attributeKeysToClassConstFetches = $configuration; - } - - private function processToClassConstFetch( - AttributeGroup $attributeGroup, - AttributeKeyToClassConstFetch $attributeKeyToClassConstFetch - ): bool { - $hasChanged = false; - - foreach ($attributeGroup->attrs as $attribute) { - if (! $this->isName($attribute->name, $attributeKeyToClassConstFetch->getAttributeClass())) { - continue; - } - - foreach ($attribute->args as $arg) { - $argName = $arg->name; - if (! $argName instanceof Identifier) { - continue; - } - - if (! $this->isName($argName, $attributeKeyToClassConstFetch->getAttributeKey())) { - continue; - } - - if ($this->processArg($arg, $attributeKeyToClassConstFetch)) { - $hasChanged = true; - } - } - } - - return $hasChanged; - } - - private function processArg(Arg $arg, AttributeKeyToClassConstFetch $attributeKeyToClassConstFetch): bool - { - $value = $this->valueResolver->getValue($arg->value); - - $constName = $attributeKeyToClassConstFetch->getValuesToConstantsMap()[$value] ?? null; - if ($constName === null) { - return false; - } - - $classConstFetch = $this->nodeFactory->createClassConstFetch( - $attributeKeyToClassConstFetch->getConstantClass(), - $constName - ); - - if ( - $arg->value instanceof ClassConstFetch - && $this->getName($arg->value) === $this->getName($classConstFetch) - ) { - return false; - } - - $arg->value = $classConstFetch; - - return true; } } diff --git a/rules/Transform/ValueObject/AttributeKeyToClassConstFetch.php b/rules/Transform/ValueObject/AttributeKeyToClassConstFetch.php index f611ab31170..65928cb6b8f 100644 --- a/rules/Transform/ValueObject/AttributeKeyToClassConstFetch.php +++ b/rules/Transform/ValueObject/AttributeKeyToClassConstFetch.php @@ -4,6 +4,9 @@ namespace Rector\Transform\ValueObject; +/** + * @api used in deprecated AttributeKeyToClassConstFetchRector configs + */ final readonly class AttributeKeyToClassConstFetch { /**