From 41033a60171eb13fbf9b02449027d7d3801a9613 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 14 May 2025 02:51:21 +0700 Subject: [PATCH 1/2] [DowngradePhp85] Add DowngradeArrayFirstLastRector --- config/set/downgrade-php85.php | 14 ++++ config/set/level/down-to-php83.php | 3 +- config/set/level/down-to-php84.php | 10 +++ .../DowngradeArrayFirstLastRectorTest.php | 28 +++++++ .../Fixture/fixture.php.inc | 33 +++++++++ .../Fixture/skip_different_func_call.php.inc | 15 ++++ .../config/configured_rule.php | 10 +++ .../Array_/DowngradeArraySpreadRector.php | 2 +- .../DowngradeArrayFirstLastRector.php | 73 +++++++++++++++++++ src/Set/ValueObject/DowngradeLevelSetList.php | 5 ++ src/Set/ValueObject/DowngradeSetList.php | 5 ++ 11 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 config/set/downgrade-php85.php create mode 100644 config/set/level/down-to-php84.php create mode 100644 rules-tests/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector/DowngradeArrayFirstLastRectorTest.php create mode 100644 rules-tests/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector/Fixture/fixture.php.inc create mode 100644 rules-tests/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector/Fixture/skip_different_func_call.php.inc create mode 100644 rules-tests/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector/config/configured_rule.php create mode 100644 rules/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector.php diff --git a/config/set/downgrade-php85.php b/config/set/downgrade-php85.php new file mode 100644 index 00000000..4f3b09a0 --- /dev/null +++ b/config/set/downgrade-php85.php @@ -0,0 +1,14 @@ +phpVersion(PhpVersion::PHP_84); + $rectorConfig->rules([ + DowngradeArrayFirstLastRector::class, + ]); +}; diff --git a/config/set/level/down-to-php83.php b/config/set/level/down-to-php83.php index e70c5d20..164f68b8 100644 --- a/config/set/level/down-to-php83.php +++ b/config/set/level/down-to-php83.php @@ -3,8 +3,9 @@ declare(strict_types=1); use Rector\Config\RectorConfig; +use Rector\Set\ValueObject\DowngradeLevelSetList; use Rector\Set\ValueObject\DowngradeSetList; return static function (RectorConfig $rectorConfig): void { - $rectorConfig->sets([DowngradeSetList::PHP_84]); + $rectorConfig->sets([DowngradeLevelSetList::DOWN_TO_PHP_84, DowngradeSetList::PHP_84]); }; diff --git a/config/set/level/down-to-php84.php b/config/set/level/down-to-php84.php new file mode 100644 index 00000000..f8ffa0ca --- /dev/null +++ b/config/set/level/down-to-php84.php @@ -0,0 +1,10 @@ +sets([DowngradeSetList::PHP_85]); +}; diff --git a/rules-tests/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector/DowngradeArrayFirstLastRectorTest.php b/rules-tests/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector/DowngradeArrayFirstLastRectorTest.php new file mode 100644 index 00000000..206b8b39 --- /dev/null +++ b/rules-tests/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector/DowngradeArrayFirstLastRectorTest.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/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector/Fixture/fixture.php.inc b/rules-tests/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..d713e7d7 --- /dev/null +++ b/rules-tests/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector/Fixture/fixture.php.inc @@ -0,0 +1,33 @@ + +----- + diff --git a/rules-tests/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector/Fixture/skip_different_func_call.php.inc b/rules-tests/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector/Fixture/skip_different_func_call.php.inc new file mode 100644 index 00000000..c8d296c3 --- /dev/null +++ b/rules-tests/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector/Fixture/skip_different_func_call.php.inc @@ -0,0 +1,15 @@ + diff --git a/rules-tests/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector/config/configured_rule.php b/rules-tests/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector/config/configured_rule.php new file mode 100644 index 00000000..535994c9 --- /dev/null +++ b/rules-tests/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(DowngradeArrayFirstLastRector::class); +}; diff --git a/rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php b/rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php index a4814419..db2e4382 100644 --- a/rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php +++ b/rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php @@ -174,7 +174,7 @@ private function refactorArrayConstValue(Array_ $array): ?Array_ /** * @var ArrayItem $item - * @var ClassConstFetch $value + * @var ClassConstFetch * @var Identifier $name */ $value = $item->value; diff --git a/rules/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector.php b/rules/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector.php new file mode 100644 index 00000000..949a4e36 --- /dev/null +++ b/rules/DowngradePhp85/Rector/FuncCall/DowngradeArrayFirstLastRector.php @@ -0,0 +1,73 @@ +isNames($node, ['array_first', 'array_last'])) { + return null; + } + + if ($node->isFirstClassCallable()) { + return null; + } + + $args = $node->getArgs(); + + if (count($args) !== 1) { + return null; + } + + $functionName = $this->isName($node, 'array_first') + ? 'array_key_first' + : 'array_key_last'; + + return new ArrayDimFetch( + $args[0]->value, + $this->nodeFactory->createFuncCall($functionName, [$args[0]->value]) + ); + } +} diff --git a/src/Set/ValueObject/DowngradeLevelSetList.php b/src/Set/ValueObject/DowngradeLevelSetList.php index 297ba42f..71f0418d 100644 --- a/src/Set/ValueObject/DowngradeLevelSetList.php +++ b/src/Set/ValueObject/DowngradeLevelSetList.php @@ -10,6 +10,11 @@ */ final class DowngradeLevelSetList { + /** + * @var string + */ + public const DOWN_TO_PHP_84 = __DIR__ . '/../../../config/set/level/down-to-php84.php'; + /** * @var string */ diff --git a/src/Set/ValueObject/DowngradeSetList.php b/src/Set/ValueObject/DowngradeSetList.php index 424fcfaa..6bd5cc40 100644 --- a/src/Set/ValueObject/DowngradeSetList.php +++ b/src/Set/ValueObject/DowngradeSetList.php @@ -49,4 +49,9 @@ final class DowngradeSetList * @var string */ public const PHP_84 = __DIR__ . '/../../../config/set/downgrade-php84.php'; + + /** + * @var string + */ + public const PHP_85 = __DIR__ . '/../../../config/set/downgrade-php85.php'; } From 82dd60358c92be5bd217f0617fd0ad7c65a02041 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 14 May 2025 02:55:55 +0700 Subject: [PATCH 2/2] fix --- .../Rector/Array_/DowngradeArraySpreadRector.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php b/rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php index db2e4382..88c2adf0 100644 --- a/rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php +++ b/rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php @@ -172,15 +172,11 @@ private function refactorArrayConstValue(Array_ $array): ?Array_ continue; } - /** - * @var ArrayItem $item - * @var ClassConstFetch - * @var Identifier $name - */ $value = $item->value; - /** @var Identifier $name */ + /** @var ClassConstFetch $value */ $name = $value->name; + /** @var Identifier $name */ $classLike = $this->astResolver->resolveClassFromName($type->getClassName()); if (! $classLike instanceof ClassLike) { continue;