From e5392caa31ef3b7a36e578b55d393368713369b3 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 22 Jun 2024 22:14:17 +0900 Subject: [PATCH] [Symfony 6.1] Add CommandConfigureToAttributeRector --- config/sets/symfony/symfony61.php | 21 ++-- config/sets/symfony/symfony70.php | 9 ++ .../CommandConfigureToAttributeRectorTest.php | 28 +++++ .../Fixture/some_command.php.inc | 24 +++++ .../config/configured_rule.php | 10 ++ .../CommandConfigureToAttributeRector.php | 102 ++++++++++++++++++ 6 files changed, 183 insertions(+), 11 deletions(-) create mode 100644 config/sets/symfony/symfony70.php create mode 100644 rules-tests/Symfony61/Rector/Class_/CommandConfigureToAttributeRector/CommandConfigureToAttributeRectorTest.php create mode 100644 rules-tests/Symfony61/Rector/Class_/CommandConfigureToAttributeRector/Fixture/some_command.php.inc create mode 100644 rules-tests/Symfony61/Rector/Class_/CommandConfigureToAttributeRector/config/configured_rule.php create mode 100644 rules/Symfony61/Rector/Class_/CommandConfigureToAttributeRector.php diff --git a/config/sets/symfony/symfony61.php b/config/sets/symfony/symfony61.php index fd4d0558..84d4ad6b 100644 --- a/config/sets/symfony/symfony61.php +++ b/config/sets/symfony/symfony61.php @@ -4,6 +4,7 @@ use Rector\Config\RectorConfig; use Rector\Renaming\Rector\Name\RenameClassRector; +use Rector\Symfony\Symfony61\Rector\Class_\CommandConfigureToAttributeRector; use Rector\Symfony\Symfony61\Rector\Class_\CommandPropertyToAttributeRector; use Rector\Symfony\Symfony61\Rector\Class_\MagicClosureTwigExtensionToNativeMethodsRector; use Rector\Symfony\Symfony61\Rector\StaticPropertyFetch\ErrorNamesPropertyToConstantRector; @@ -12,20 +13,18 @@ return static function (RectorConfig $rectorConfig): void { $rectorConfig->rules([ + CommandConfigureToAttributeRector::class, CommandPropertyToAttributeRector::class, ErrorNamesPropertyToConstantRector::class, MagicClosureTwigExtensionToNativeMethodsRector::class, ]); - $rectorConfig->ruleWithConfiguration( - RenameClassRector::class, - [ - // @see https://github.com/symfony/symfony/pull/43982 - 'Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface' => 'Symfony\Component\Serializer\Normalizer\DenormalizerInterface', - 'Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface' => 'Symfony\Component\Serializer\Normalizer\NormalizerInterface', - // @see https://github.com/symfony/symfony/pull/45623 - 'Symfony\Component\Validator\Constraints\ExpressionLanguageSyntax' => 'Symfony\Component\Validator\Constraints\ExpressionSyntax', - 'Symfony\Component\Validator\Constraints\ExpressionLanguageSyntaxValidator' => 'Symfony\Component\Validator\Constraints\ExpressionSyntaxValidator', - ], - ); + $rectorConfig->ruleWithConfiguration(RenameClassRector::class, [ + // @see https://github.com/symfony/symfony/pull/43982 + 'Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface' => 'Symfony\Component\Serializer\Normalizer\DenormalizerInterface', + 'Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface' => 'Symfony\Component\Serializer\Normalizer\NormalizerInterface', + // @see https://github.com/symfony/symfony/pull/45623 + 'Symfony\Component\Validator\Constraints\ExpressionLanguageSyntax' => 'Symfony\Component\Validator\Constraints\ExpressionSyntax', + 'Symfony\Component\Validator\Constraints\ExpressionLanguageSyntaxValidator' => 'Symfony\Component\Validator\Constraints\ExpressionSyntaxValidator', + ]); }; diff --git a/config/sets/symfony/symfony70.php b/config/sets/symfony/symfony70.php new file mode 100644 index 00000000..2fa17639 --- /dev/null +++ b/config/sets/symfony/symfony70.php @@ -0,0 +1,9 @@ +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/Symfony61/Rector/Class_/CommandConfigureToAttributeRector/Fixture/some_command.php.inc b/rules-tests/Symfony61/Rector/Class_/CommandConfigureToAttributeRector/Fixture/some_command.php.inc new file mode 100644 index 00000000..6b8c3d56 --- /dev/null +++ b/rules-tests/Symfony61/Rector/Class_/CommandConfigureToAttributeRector/Fixture/some_command.php.inc @@ -0,0 +1,24 @@ +setName('sunshine'); + } +} + +?> +----- + diff --git a/rules-tests/Symfony61/Rector/Class_/CommandConfigureToAttributeRector/config/configured_rule.php b/rules-tests/Symfony61/Rector/Class_/CommandConfigureToAttributeRector/config/configured_rule.php new file mode 100644 index 00000000..d4f82bb3 --- /dev/null +++ b/rules-tests/Symfony61/Rector/Class_/CommandConfigureToAttributeRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(CommandConfigureToAttributeRector::class); +}; diff --git a/rules/Symfony61/Rector/Class_/CommandConfigureToAttributeRector.php b/rules/Symfony61/Rector/Class_/CommandConfigureToAttributeRector.php new file mode 100644 index 00000000..28cc52be --- /dev/null +++ b/rules/Symfony61/Rector/Class_/CommandConfigureToAttributeRector.php @@ -0,0 +1,102 @@ +setName('sunshine'); + } +} +CODE_SAMPLE + , <<<'CODE_SAMPLE' +use Symfony\Component\Console\Attribute\AsCommand; +use Symfony\Component\Console\Command\Command; + +#[AsCommand('sunshine')] +final class SunshineCommand extends Command +{ +} +CODE_SAMPLE), + + ] + ); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [Class_::class]; + } + + /** + * @param Class_ $node + */ + public function refactor(Node $node): ?Node + { + if (! $this->isObjectType($node, new ObjectType('Symfony\\Component\\Console\\Command\\Command'))) { + return null; + } + + if (! $this->reflectionProvider->hasClass(SymfonyAnnotation::AS_COMMAND)) { + return null; + } + + // @todo move configure() to attribute + } +}