From d042abc370f72fac172d31dc9a030fa3ab26dc49 Mon Sep 17 00:00:00 2001 From: Fabien Villepinte Date: Tue, 9 Nov 2021 07:42:21 +0000 Subject: [PATCH 1/2] [Downgrade PHP 5.5] Add DowngradeClassConstantToStringRector --- config/set/downgrade-php55.php | 16 ++++ packages/Set/ValueObject/DowngradeSetList.php | 5 + ...wngradeClassConstantToStringRectorTest.php | 33 +++++++ .../Fixture/another_class.php.inc | 43 +++++++++ .../Fixture/some_class.php.inc | 23 +++++ .../config/configured_rule.php | 12 +++ .../DowngradeClassConstantToStringRector.php | 95 +++++++++++++++++++ 7 files changed, 227 insertions(+) create mode 100644 config/set/downgrade-php55.php create mode 100644 rules-tests/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector/DowngradeClassConstantToStringRectorTest.php create mode 100644 rules-tests/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector/Fixture/another_class.php.inc create mode 100644 rules-tests/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector/Fixture/some_class.php.inc create mode 100644 rules-tests/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector/config/configured_rule.php create mode 100644 rules/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector.php diff --git a/config/set/downgrade-php55.php b/config/set/downgrade-php55.php new file mode 100644 index 00000000000..9701ae4aee9 --- /dev/null +++ b/config/set/downgrade-php55.php @@ -0,0 +1,16 @@ +parameters(); + $parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_54); + + $services = $containerConfigurator->services(); + $services->set(DowngradeClassConstantToStringRector::class); +}; diff --git a/packages/Set/ValueObject/DowngradeSetList.php b/packages/Set/ValueObject/DowngradeSetList.php index 44af8182f3d..b256cf8f31c 100644 --- a/packages/Set/ValueObject/DowngradeSetList.php +++ b/packages/Set/ValueObject/DowngradeSetList.php @@ -18,6 +18,11 @@ final class DowngradeSetList implements SetListInterface */ public const PHP_54 = __DIR__ . '/../../../config/set/downgrade-php54.php'; + /** + * @var string + */ + public const PHP_55 = __DIR__ . '/../../../config/set/downgrade-php55.php'; + /** * @var string */ diff --git a/rules-tests/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector/DowngradeClassConstantToStringRectorTest.php b/rules-tests/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector/DowngradeClassConstantToStringRectorTest.php new file mode 100644 index 00000000000..716cc66e136 --- /dev/null +++ b/rules-tests/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector/DowngradeClassConstantToStringRectorTest.php @@ -0,0 +1,33 @@ +doTestFileInfo($fileInfo); + } + + /** + * @return Iterator + */ + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/rules-tests/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector/Fixture/another_class.php.inc b/rules-tests/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector/Fixture/another_class.php.inc new file mode 100644 index 00000000000..b5233727cc2 --- /dev/null +++ b/rules-tests/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector/Fixture/another_class.php.inc @@ -0,0 +1,43 @@ + +----- + diff --git a/rules-tests/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector/Fixture/some_class.php.inc b/rules-tests/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector/Fixture/some_class.php.inc new file mode 100644 index 00000000000..bf993db6a70 --- /dev/null +++ b/rules-tests/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector/Fixture/some_class.php.inc @@ -0,0 +1,23 @@ + +----- + diff --git a/rules-tests/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector/config/configured_rule.php b/rules-tests/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector/config/configured_rule.php new file mode 100644 index 00000000000..71d05d74d79 --- /dev/null +++ b/rules-tests/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector/config/configured_rule.php @@ -0,0 +1,12 @@ +services(); + $services->set(DowngradeClassConstantToStringRector::class); +}; diff --git a/rules/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector.php b/rules/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector.php new file mode 100644 index 00000000000..fec4658b811 --- /dev/null +++ b/rules/DowngradePhp55/Rector/ClassConstFetch/DowngradeClassConstantToStringRector.php @@ -0,0 +1,95 @@ +::class constant by string class names', + [ + new CodeSample( + <<<'CODE_SAMPLE' +class AnotherClass +{ +} +class SomeClass +{ + public function run() + { + return \AnotherClass::class; + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +class AnotherClass +{ +} +class SomeClass +{ + public function run() + { + return 'AnotherClass'; + } +} +CODE_SAMPLE + ), + ] + ); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [ClassConstFetch::class]; + } + + /** + * @param ClassConstFetch $node + */ + public function refactor(Node $node): ?Node + { + if ( + ! $node->name instanceof Identifier + || strtolower($node->name->name) !== 'class' + || ! $node->class instanceof Name + ) { + return null; + } + + $className = $node->class->toString(); + + $func = match (strtolower($className)) { + 'self' => 'get_class', + 'static' => 'get_called_class', + 'parent' => 'get_parent_class', + default => null, + }; + + if ($func !== null) { + return $this->nodeFactory->createFuncCall($func); + } + + return new String_($className); + } +} From 69608494bd4c4151cb52cd8a74e61f7359236386 Mon Sep 17 00:00:00 2001 From: Fabien Villepinte Date: Tue, 9 Nov 2021 09:40:59 +0100 Subject: [PATCH 2/2] Complete DowngradeLevelSetList down to 5.3 --- config/set/level/down-to-php53.php | 12 ++++++++++++ config/set/level/down-to-php54.php | 12 ++++++++++++ config/set/level/down-to-php55.php | 12 ++++++++++++ .../Set/ValueObject/DowngradeLevelSetList.php | 15 +++++++++++++++ 4 files changed, 51 insertions(+) create mode 100644 config/set/level/down-to-php53.php create mode 100644 config/set/level/down-to-php54.php create mode 100644 config/set/level/down-to-php55.php diff --git a/config/set/level/down-to-php53.php b/config/set/level/down-to-php53.php new file mode 100644 index 00000000000..f255db845f5 --- /dev/null +++ b/config/set/level/down-to-php53.php @@ -0,0 +1,12 @@ +import(DowngradeLevelSetList::DOWN_TO_PHP_54); + $containerConfigurator->import(DowngradeSetList::PHP_54); +}; diff --git a/config/set/level/down-to-php54.php b/config/set/level/down-to-php54.php new file mode 100644 index 00000000000..10894e23d6d --- /dev/null +++ b/config/set/level/down-to-php54.php @@ -0,0 +1,12 @@ +import(DowngradeLevelSetList::DOWN_TO_PHP_55); + $containerConfigurator->import(DowngradeSetList::PHP_55); +}; diff --git a/config/set/level/down-to-php55.php b/config/set/level/down-to-php55.php new file mode 100644 index 00000000000..22b82063353 --- /dev/null +++ b/config/set/level/down-to-php55.php @@ -0,0 +1,12 @@ +import(DowngradeLevelSetList::DOWN_TO_PHP_56); + $containerConfigurator->import(DowngradeSetList::PHP_56); +}; diff --git a/packages/Set/ValueObject/DowngradeLevelSetList.php b/packages/Set/ValueObject/DowngradeLevelSetList.php index 27f0338e75a..13ecfd34a35 100644 --- a/packages/Set/ValueObject/DowngradeLevelSetList.php +++ b/packages/Set/ValueObject/DowngradeLevelSetList.php @@ -42,4 +42,19 @@ final class DowngradeLevelSetList implements SetListInterface * @var string */ public const DOWN_TO_PHP_56 = __DIR__ . '/../../../config/set/level/down-to-php56.php'; + + /** + * @var string + */ + public const DOWN_TO_PHP_55 = __DIR__ . '/../../../config/set/level/down-to-php55.php'; + + /** + * @var string + */ + public const DOWN_TO_PHP_54 = __DIR__ . '/../../../config/set/level/down-to-php54.php'; + + /** + * @var string + */ + public const DOWN_TO_PHP_53 = __DIR__ . '/../../../config/set/level/down-to-php53.php'; }