From 2af405c68ac19a887034b7105c5ebb354932bb9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Brala?= Date: Fri, 8 Mar 2024 18:44:07 +0100 Subject: [PATCH] New rector (9.1): ClassConstantToClassConstantRector to rename class constants to a new class. (#282) * ConstantToClassConstantRector serves d8 and d9, it should be in the generic space * New rector (9.1): ClassConstantToClassConstantRector to rename class constants to a new class. * 0.19 changes --------- Co-authored-by: Ken Rickard --- config/drupal-8/drupal-8.5-deprecations.php | 4 +- config/drupal-8/drupal-8.7-deprecations.php | 4 +- config/drupal-9/drupal-9.1-deprecations.php | 24 ++++ config/drupal-9/drupal-9.3-deprecations.php | 4 +- docs/rules_overview.md | 34 +++--- .../ClassConstantToClassConstantRector.php | 112 ++++++++++++++++++ .../ConstantToClassConstantRector.php | 8 +- ...ssConstantToClassConstantConfiguration.php | 49 ++++++++ .../ConstantToClassConfiguration.php | 2 +- .../ConstantToClassConstantRectorTest.php | 35 ++++++ .../config/configured_rule.php | 31 +++++ .../fixture/fixture.php.inc | 19 +++ .../ConstantToClassConstantRectorTest.php | 2 +- .../config/configured_rule.php | 4 +- .../fixture/fixture.php.inc | 0 15 files changed, 301 insertions(+), 31 deletions(-) create mode 100644 src/Rector/Deprecation/ClassConstantToClassConstantRector.php rename src/{Drupal8 => }/Rector/Deprecation/ConstantToClassConstantRector.php (90%) create mode 100644 src/Rector/ValueObject/ClassConstantToClassConstantConfiguration.php rename src/{Drupal8 => }/Rector/ValueObject/ConstantToClassConfiguration.php (94%) create mode 100644 tests/src/Rector/Deprecation/ClassConstantToClassConstantRector/ConstantToClassConstantRectorTest.php create mode 100644 tests/src/Rector/Deprecation/ClassConstantToClassConstantRector/config/configured_rule.php create mode 100644 tests/src/Rector/Deprecation/ClassConstantToClassConstantRector/fixture/fixture.php.inc rename tests/src/{Drupal8 => }/Rector/Deprecation/ConstantToClassConstantRector/ConstantToClassConstantRectorTest.php (90%) rename tests/src/{Drupal8 => }/Rector/Deprecation/ConstantToClassConstantRector/config/configured_rule.php (83%) rename tests/src/{Drupal8 => }/Rector/Deprecation/ConstantToClassConstantRector/fixture/fixture.php.inc (100%) diff --git a/config/drupal-8/drupal-8.5-deprecations.php b/config/drupal-8/drupal-8.5-deprecations.php index 64067b13..f0430179 100644 --- a/config/drupal-8/drupal-8.5-deprecations.php +++ b/config/drupal-8/drupal-8.5-deprecations.php @@ -2,9 +2,9 @@ declare(strict_types=1); -use DrupalRector\Drupal8\Rector\Deprecation\ConstantToClassConstantRector; use DrupalRector\Drupal8\Rector\Deprecation\DrupalSetMessageRector; -use DrupalRector\Drupal8\Rector\ValueObject\ConstantToClassConfiguration; +use DrupalRector\Rector\Deprecation\ConstantToClassConstantRector; +use DrupalRector\Rector\ValueObject\ConstantToClassConfiguration; use DrupalRector\Services\AddCommentService; use Rector\Config\RectorConfig; diff --git a/config/drupal-8/drupal-8.7-deprecations.php b/config/drupal-8/drupal-8.7-deprecations.php index 678e8b30..e21f5eda 100644 --- a/config/drupal-8/drupal-8.7-deprecations.php +++ b/config/drupal-8/drupal-8.7-deprecations.php @@ -2,9 +2,9 @@ declare(strict_types=1); -use DrupalRector\Drupal8\Rector\Deprecation\ConstantToClassConstantRector; -use DrupalRector\Drupal8\Rector\ValueObject\ConstantToClassConfiguration; +use DrupalRector\Rector\Deprecation\ConstantToClassConstantRector; use DrupalRector\Rector\Deprecation\FunctionToServiceRector; +use DrupalRector\Rector\ValueObject\ConstantToClassConfiguration; use DrupalRector\Rector\ValueObject\FunctionToServiceConfiguration; use DrupalRector\Services\AddCommentService; use Rector\Config\RectorConfig; diff --git a/config/drupal-9/drupal-9.1-deprecations.php b/config/drupal-9/drupal-9.1-deprecations.php index a2f5125c..536fdb49 100644 --- a/config/drupal-9/drupal-9.1-deprecations.php +++ b/config/drupal-9/drupal-9.1-deprecations.php @@ -15,6 +15,8 @@ use DrupalRector\Drupal9\Rector\Deprecation\UiHelperTraitDrupalPostFormRector; use DrupalRector\Drupal9\Rector\Deprecation\UserPasswordRector; use DrupalRector\Drupal9\Rector\ValueObject\AssertLegacyTraitConfiguration; +use DrupalRector\Rector\Deprecation\ClassConstantToClassConstantRector; +use DrupalRector\Rector\ValueObject\ClassConstantToClassConstantConfiguration; use DrupalRector\Services\AddCommentService; use Rector\Config\RectorConfig; use Rector\PHPUnit\Set\PHPUnitSetList; @@ -107,4 +109,26 @@ 'toNumber' ), ]); + + // Change record: https://www.drupal.org/node/3151009 (only constants are supported) + $rectorConfig->ruleWithConfiguration(ClassConstantToClassConstantRector::class, [ + new ClassConstantToClassConstantConfiguration( + 'Symfony\Cmf\Component\Routing\RouteObjectInterface', + 'ROUTE_NAME', + 'Drupal\Core\Routing\RouteObjectInterface', + 'ROUTE_NAME', + ), + new ClassConstantToClassConstantConfiguration( + 'Symfony\Cmf\Component\Routing\RouteObjectInterface', + 'ROUTE_OBJECT', + 'Drupal\Core\Routing\RouteObjectInterface', + 'ROUTE_OBJECT', + ), + new ClassConstantToClassConstantConfiguration( + 'Symfony\Cmf\Component\Routing\RouteObjectInterface', + 'CONTROLLER_NAME', + 'Drupal\Core\Routing\RouteObjectInterface', + 'CONTROLLER_NAME', + ), + ]); }; diff --git a/config/drupal-9/drupal-9.3-deprecations.php b/config/drupal-9/drupal-9.3-deprecations.php index 6b6166a3..f93b1445 100644 --- a/config/drupal-9/drupal-9.3-deprecations.php +++ b/config/drupal-9/drupal-9.3-deprecations.php @@ -2,8 +2,6 @@ declare(strict_types=1); -use DrupalRector\Drupal8\Rector\Deprecation\ConstantToClassConstantRector; -use DrupalRector\Drupal8\Rector\ValueObject\ConstantToClassConfiguration; use DrupalRector\Drupal9\Rector\Deprecation\ExtensionPathRector; use DrupalRector\Drupal9\Rector\Deprecation\FileBuildUriRector; use DrupalRector\Drupal9\Rector\Deprecation\FunctionToEntityTypeStorageMethod; @@ -15,8 +13,10 @@ use DrupalRector\Drupal9\Rector\ValueObject\ExtensionPathConfiguration; use DrupalRector\Drupal9\Rector\ValueObject\FunctionToEntityTypeStorageConfiguration; use DrupalRector\Drupal9\Rector\ValueObject\FunctionToFirstArgMethodConfiguration; +use DrupalRector\Rector\Deprecation\ConstantToClassConstantRector; use DrupalRector\Rector\Deprecation\FunctionToServiceRector; use DrupalRector\Rector\Deprecation\FunctionToStaticRector; +use DrupalRector\Rector\ValueObject\ConstantToClassConfiguration; use DrupalRector\Rector\ValueObject\FunctionToServiceConfiguration; use DrupalRector\Rector\ValueObject\FunctionToStaticConfiguration; use DrupalRector\Services\AddCommentService; diff --git a/docs/rules_overview.md b/docs/rules_overview.md index c6d108cd..1a80073f 100644 --- a/docs/rules_overview.md +++ b/docs/rules_overview.md @@ -6,11 +6,11 @@ - [Drupal10](#drupal10) (2) -- [Drupal8](#drupal8) (19) +- [Drupal8](#drupal8) (18) - [Drupal9](#drupal9) (26) -- [DrupalRector](#drupalrector) (5) +- [DrupalRector](#drupalrector) (6)
@@ -56,21 +56,6 @@ Fixes deprecated watchdog_exception('update', `$exception)` calls ## Drupal8 -### ConstantToClassConstantRector - -Fixes deprecated contant use - -:wrench: **configure it!** - -- class: [`DrupalRector\Drupal8\Rector\Deprecation\ConstantToClassConstantRector`](../src/Drupal8/Rector/Deprecation/ConstantToClassConstantRector.php) - -```diff --$result = file_unmanaged_copy($source, $destination, DEPRECATED_CONSTANT); -+$result = file_unmanaged_copy($source, $destination, \Drupal\MyClass::CONSTANT); -``` - -
- ### DBRector Fixes deprecated `db_delete()` calls @@ -858,6 +843,21 @@ Fixes deprecated `user_password()` calls ## DrupalRector +### ConstantToClassConstantRector + +Fixes deprecated contant use, used in Drupal 8 and 9 deprecations + +:wrench: **configure it!** + +- class: [`DrupalRector\Rector\Deprecation\ConstantToClassConstantRector`](../src/Rector/Deprecation/ConstantToClassConstantRector.php) + +```diff +-$result = file_unmanaged_copy($source, $destination, DEPRECATED_CONSTANT); ++$result = file_unmanaged_copy($source, $destination, \Drupal\MyClass::CONSTANT); +``` + +
+ ### DeprecationHelperRemoveRector Remove DeprecationHelper calls for versions before configured minimum requirement diff --git a/src/Rector/Deprecation/ClassConstantToClassConstantRector.php b/src/Rector/Deprecation/ClassConstantToClassConstantRector.php new file mode 100644 index 00000000..de78184c --- /dev/null +++ b/src/Rector/Deprecation/ClassConstantToClassConstantRector.php @@ -0,0 +1,112 @@ +constantToClassRenames = $configuration; + } + + /** + * {@inheritdoc} + */ + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition('Fixes deprecated class contant use, used in Drupal 9.1 deprecations', [ + new ConfiguredCodeSample( + <<<'CODE_BEFORE' +$value = Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_NAME; +$value2 = Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT; +$value3 = Symfony\Cmf\Component\Routing\RouteObjectInterface::CONTROLLER_NAME; +CODE_BEFORE + , + <<<'CODE_AFTER' +$value = \Drupal\Core\Routing\RouteObjectInterface::ROUTE_NAME; +$value2 = \Drupal\Core\Routing\RouteObjectInterface::ROUTE_OBJECT; +$value3 = \Drupal\Core\Routing\RouteObjectInterface::CONTROLLER_NAME; +CODE_AFTER + , + [ + new ClassConstantToClassConstantConfiguration( + 'Symfony\Cmf\Component\Routing\RouteObjectInterface', + 'ROUTE_NAME', + 'Drupal\Core\Routing\RouteObjectInterface', + 'ROUTE_NAME', + ), + new ClassConstantToClassConstantConfiguration( + 'Symfony\Cmf\Component\Routing\RouteObjectInterface', + 'ROUTE_OBJECT', + 'Drupal\Core\Routing\RouteObjectInterface', + 'ROUTE_OBJECT', + ), + new ClassConstantToClassConstantConfiguration( + 'Symfony\Cmf\Component\Routing\RouteObjectInterface', + 'CONTROLLER_NAME', + 'Drupal\Core\Routing\RouteObjectInterface', + 'CONTROLLER_NAME', + ), + ] + ), + ]); + } + + /** + * {@inheritdoc} + */ + public function getNodeTypes(): array + { + return [ + Node\Expr\ClassConstFetch::class, + ]; + } + + /** + * {@inheritdoc} + */ + public function refactor(Node $node): ?Node + { + assert($node instanceof Node\Expr\ClassConstFetch); + + foreach ($this->constantToClassRenames as $constantToClassRename) { + if ($this->getName($node->name) === $constantToClassRename->getDeprecated() && $this->getName($node->class) === $constantToClassRename->getDeprecatedClass()) { + // We add a fully qualified class name and the parameters in `rector.php` adds the use statement. + $fully_qualified_class = new Node\Name\FullyQualified($constantToClassRename->getClass()); + + $name = new Node\Identifier($constantToClassRename->getConstant()); + + return new Node\Expr\ClassConstFetch($fully_qualified_class, $name); + } + } + + return null; + } +} diff --git a/src/Drupal8/Rector/Deprecation/ConstantToClassConstantRector.php b/src/Rector/Deprecation/ConstantToClassConstantRector.php similarity index 90% rename from src/Drupal8/Rector/Deprecation/ConstantToClassConstantRector.php rename to src/Rector/Deprecation/ConstantToClassConstantRector.php index 3ee74eb7..cb1cc7ae 100644 --- a/src/Drupal8/Rector/Deprecation/ConstantToClassConstantRector.php +++ b/src/Rector/Deprecation/ConstantToClassConstantRector.php @@ -2,9 +2,9 @@ declare(strict_types=1); -namespace DrupalRector\Drupal8\Rector\Deprecation; +namespace DrupalRector\Rector\Deprecation; -use DrupalRector\Drupal8\Rector\ValueObject\ConstantToClassConfiguration; +use DrupalRector\Rector\ValueObject\ConstantToClassConfiguration; use PhpParser\Node; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Rector\AbstractRector; @@ -20,7 +20,7 @@ class ConstantToClassConstantRector extends AbstractRector implements ConfigurableRectorInterface { /** - * @var \DrupalRector\Drupal8\Rector\ValueObject\ConstantToClassConfiguration[] + * @var ConstantToClassConfiguration[] */ private array $constantToClassRenames; @@ -40,7 +40,7 @@ public function configure(array $configuration): void */ public function getRuleDefinition(): RuleDefinition { - return new RuleDefinition('Fixes deprecated contant use', [ + return new RuleDefinition('Fixes deprecated contant use, used in Drupal 8 and 9 deprecations', [ new ConfiguredCodeSample( <<<'CODE_BEFORE' $result = file_unmanaged_copy($source, $destination, DEPRECATED_CONSTANT); diff --git a/src/Rector/ValueObject/ClassConstantToClassConstantConfiguration.php b/src/Rector/ValueObject/ClassConstantToClassConstantConfiguration.php new file mode 100644 index 00000000..24a3d574 --- /dev/null +++ b/src/Rector/ValueObject/ClassConstantToClassConstantConfiguration.php @@ -0,0 +1,49 @@ +deprecatedClass = $deprecatedClass; + $this->deprecated = $deprecated; + $this->class = $class; + $this->constant = $constant; + + RectorAssert::className($deprecatedClass); + RectorAssert::className($class); + RectorAssert::constantName($deprecated); + RectorAssert::constantName($constant); + } + + public function getDeprecated(): string + { + return $this->deprecated; + } + + public function getClass(): string + { + return $this->class; + } + + public function getConstant(): string + { + return $this->constant; + } + + public function getDeprecatedClass(): string + { + return $this->deprecatedClass; + } +} diff --git a/src/Drupal8/Rector/ValueObject/ConstantToClassConfiguration.php b/src/Rector/ValueObject/ConstantToClassConfiguration.php similarity index 94% rename from src/Drupal8/Rector/ValueObject/ConstantToClassConfiguration.php rename to src/Rector/ValueObject/ConstantToClassConfiguration.php index b8bfe544..98a5c16d 100644 --- a/src/Drupal8/Rector/ValueObject/ConstantToClassConfiguration.php +++ b/src/Rector/ValueObject/ConstantToClassConfiguration.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace DrupalRector\Drupal8\Rector\ValueObject; +namespace DrupalRector\Rector\ValueObject; use Rector\Validation\RectorAssert; diff --git a/tests/src/Rector/Deprecation/ClassConstantToClassConstantRector/ConstantToClassConstantRectorTest.php b/tests/src/Rector/Deprecation/ClassConstantToClassConstantRector/ConstantToClassConstantRectorTest.php new file mode 100644 index 00000000..2c7181c7 --- /dev/null +++ b/tests/src/Rector/Deprecation/ClassConstantToClassConstantRector/ConstantToClassConstantRectorTest.php @@ -0,0 +1,35 @@ +doTestFile($filePath); + } + + /** + * @return Iterator<> + */ + public static function provideData(): \Iterator + { + return self::yieldFilesFromDirectory(__DIR__.'/fixture'); + } + + public function provideConfigFilePath(): string + { + // must be implemented + return __DIR__.'/config/configured_rule.php'; + } +} diff --git a/tests/src/Rector/Deprecation/ClassConstantToClassConstantRector/config/configured_rule.php b/tests/src/Rector/Deprecation/ClassConstantToClassConstantRector/config/configured_rule.php new file mode 100644 index 00000000..4d1352cb --- /dev/null +++ b/tests/src/Rector/Deprecation/ClassConstantToClassConstantRector/config/configured_rule.php @@ -0,0 +1,31 @@ + +----- + diff --git a/tests/src/Drupal8/Rector/Deprecation/ConstantToClassConstantRector/ConstantToClassConstantRectorTest.php b/tests/src/Rector/Deprecation/ConstantToClassConstantRector/ConstantToClassConstantRectorTest.php similarity index 90% rename from tests/src/Drupal8/Rector/Deprecation/ConstantToClassConstantRector/ConstantToClassConstantRectorTest.php rename to tests/src/Rector/Deprecation/ConstantToClassConstantRector/ConstantToClassConstantRectorTest.php index e15349ad..b7698b1e 100644 --- a/tests/src/Drupal8/Rector/Deprecation/ConstantToClassConstantRector/ConstantToClassConstantRectorTest.php +++ b/tests/src/Rector/Deprecation/ConstantToClassConstantRector/ConstantToClassConstantRectorTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Drupal8\Rector\Deprecation\ConstantToClassConstantRector; +namespace DrupalRector\Rector\Deprecation\ConstantToClassConstantRector; use Iterator; use Rector\Testing\PHPUnit\AbstractRectorTestCase; diff --git a/tests/src/Drupal8/Rector/Deprecation/ConstantToClassConstantRector/config/configured_rule.php b/tests/src/Rector/Deprecation/ConstantToClassConstantRector/config/configured_rule.php similarity index 83% rename from tests/src/Drupal8/Rector/Deprecation/ConstantToClassConstantRector/config/configured_rule.php rename to tests/src/Rector/Deprecation/ConstantToClassConstantRector/config/configured_rule.php index aeec0664..fe541df3 100644 --- a/tests/src/Drupal8/Rector/Deprecation/ConstantToClassConstantRector/config/configured_rule.php +++ b/tests/src/Rector/Deprecation/ConstantToClassConstantRector/config/configured_rule.php @@ -2,8 +2,8 @@ declare(strict_types=1); -use DrupalRector\Drupal8\Rector\Deprecation\ConstantToClassConstantRector; -use DrupalRector\Drupal8\Rector\ValueObject\ConstantToClassConfiguration; +use DrupalRector\Rector\Deprecation\ConstantToClassConstantRector; +use DrupalRector\Rector\ValueObject\ConstantToClassConfiguration; use DrupalRector\Tests\Rector\Deprecation\DeprecationBase; use Rector\Config\RectorConfig; diff --git a/tests/src/Drupal8/Rector/Deprecation/ConstantToClassConstantRector/fixture/fixture.php.inc b/tests/src/Rector/Deprecation/ConstantToClassConstantRector/fixture/fixture.php.inc similarity index 100% rename from tests/src/Drupal8/Rector/Deprecation/ConstantToClassConstantRector/fixture/fixture.php.inc rename to tests/src/Rector/Deprecation/ConstantToClassConstantRector/fixture/fixture.php.inc