diff --git a/config/set/downgrade-php83.php b/config/set/downgrade-php83.php index f86f2300..af0d5d88 100644 --- a/config/set/downgrade-php83.php +++ b/config/set/downgrade-php83.php @@ -6,11 +6,13 @@ use Rector\DowngradePhp83\Rector\Class_\DowngradeReadonlyAnonymousClassRector; use Rector\ValueObject\PhpVersion; use Rector\DowngradePhp83\Rector\ClassConst\DowngradeTypedClassConstRector; +use Rector\DowngradePhp83\Rector\ClassConstFetch\DowngradeDynamicClassConstFetchRector; return static function (RectorConfig $rectorConfig): void { $rectorConfig->phpVersion(PhpVersion::PHP_82); $rectorConfig->rules([ DowngradeTypedClassConstRector::class, DowngradeReadonlyAnonymousClassRector::class, + DowngradeDynamicClassConstFetchRector::class, ]); }; diff --git a/rules-tests/DowngradePhp83/Rector/ClassConstFetch/DowngradeDynamicClassConstFetchRector/DowngradeDynamicClassConstFetchRectorTest.php b/rules-tests/DowngradePhp83/Rector/ClassConstFetch/DowngradeDynamicClassConstFetchRector/DowngradeDynamicClassConstFetchRectorTest.php new file mode 100644 index 00000000..c440a848 --- /dev/null +++ b/rules-tests/DowngradePhp83/Rector/ClassConstFetch/DowngradeDynamicClassConstFetchRector/DowngradeDynamicClassConstFetchRectorTest.php @@ -0,0 +1,28 @@ +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/DowngradePhp83/Rector/ClassConstFetch/DowngradeDynamicClassConstFetchRector/Fixture/fixture.php.inc b/rules-tests/DowngradePhp83/Rector/ClassConstFetch/DowngradeDynamicClassConstFetchRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..80635bce --- /dev/null +++ b/rules-tests/DowngradePhp83/Rector/ClassConstFetch/DowngradeDynamicClassConstFetchRector/Fixture/fixture.php.inc @@ -0,0 +1,37 @@ + +----- + diff --git a/rules-tests/DowngradePhp83/Rector/ClassConstFetch/DowngradeDynamicClassConstFetchRector/Fixture/skip_named_class_const_fetch.php.inc b/rules-tests/DowngradePhp83/Rector/ClassConstFetch/DowngradeDynamicClassConstFetchRector/Fixture/skip_named_class_const_fetch.php.inc new file mode 100644 index 00000000..d4ead3be --- /dev/null +++ b/rules-tests/DowngradePhp83/Rector/ClassConstFetch/DowngradeDynamicClassConstFetchRector/Fixture/skip_named_class_const_fetch.php.inc @@ -0,0 +1,15 @@ +rule(DowngradeDynamicClassConstFetchRector::class); +}; diff --git a/rules/DowngradePhp83/Rector/ClassConstFetch/DowngradeDynamicClassConstFetchRector.php b/rules/DowngradePhp83/Rector/ClassConstFetch/DowngradeDynamicClassConstFetchRector.php new file mode 100644 index 00000000..cee77418 --- /dev/null +++ b/rules/DowngradePhp83/Rector/ClassConstFetch/DowngradeDynamicClassConstFetchRector.php @@ -0,0 +1,65 @@ +> + */ + public function getNodeTypes(): array + { + return [ClassConstFetch::class]; + } + + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition( + 'Change dynamic class const fetch Example::{$constName} to constant(Example::class . \'::\' . $constName)', + [ + new CodeSample( + <<<'CODE_SAMPLE' +$value = Example::{$constName}; +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +$value = constant(Example::class . '::' . $constName); +CODE_SAMPLE + ), + ] + ); + } + + /** + * @param ClassConstFetch $node + */ + public function refactor(Node $node): ?Node + { + if ($node->name instanceof Identifier) { + return null; + } + + return $this->nodeFactory->createFuncCall('constant', [ + new Concat( + new Concat(new ClassConstFetch($node->class, new Identifier('class')), new String_('::')), + $node->name + ), + ]); + } +}