From b74feacfbcc05be974ccd67b4d0d8cb1c67eb9e2 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 11 Jul 2025 15:06:26 +0700 Subject: [PATCH 1/4] [DowngradePhp84] Add DowngradeArrayFindRector --- config/set/downgrade-php84.php | 2 + .../Fixture/fixture.php.inc | 2 +- .../DowngradeArrayAnyRectorTest.php | 28 +++++ .../Fixture/fixture.php.inc | 33 +++++ .../Fixture/with_key.php.inc | 33 +++++ .../config/configured_rule.php | 10 ++ .../Expression/DowngradeArrayAnyRector.php | 2 +- .../Expression/DowngradeArrayFindRector.php | 116 ++++++++++++++++++ 8 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/DowngradeArrayAnyRectorTest.php create mode 100644 rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/Fixture/fixture.php.inc create mode 100644 rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/Fixture/with_key.php.inc create mode 100644 rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/config/configured_rule.php create mode 100644 rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php diff --git a/config/set/downgrade-php84.php b/config/set/downgrade-php84.php index 2ccd7bd3..1182f5e8 100644 --- a/config/set/downgrade-php84.php +++ b/config/set/downgrade-php84.php @@ -5,6 +5,7 @@ use Rector\Config\RectorConfig; use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayAllRector; use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayAnyRector; +use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayFindRector; use Rector\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector; use Rector\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector; use Rector\ValueObject\PhpVersion; @@ -16,5 +17,6 @@ DowngradeRoundingModeEnumRector::class, DowngradeArrayAllRector::class, DowngradeArrayAnyRector::class, + DowngradeArrayFindRector::class, ]); }; diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/Fixture/fixture.php.inc b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/Fixture/fixture.php.inc index 696b05ac..e4152b57 100644 --- a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/Fixture/fixture.php.inc +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/Fixture/fixture.php.inc @@ -6,7 +6,7 @@ class Fixture { public function run(array $animals) { - $found = array_all($animals, fn($animal) => str_starts_with($animal, 'c')); + $found = array_any($animals, fn($animal) => str_starts_with($animal, 'c')); } } diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/DowngradeArrayAnyRectorTest.php b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/DowngradeArrayAnyRectorTest.php new file mode 100644 index 00000000..84c0a801 --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/DowngradeArrayAnyRectorTest.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/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/Fixture/fixture.php.inc b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..0c5c84df --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/Fixture/fixture.php.inc @@ -0,0 +1,33 @@ + str_starts_with($animal, 'c')); + } +} + +?> +----- + diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/Fixture/with_key.php.inc b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/Fixture/with_key.php.inc new file mode 100644 index 00000000..cf4415fc --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/Fixture/with_key.php.inc @@ -0,0 +1,33 @@ + str_starts_with($animal, 'c') && $key > 0); + } +} + +?> +----- + $animal) { + if (str_starts_with($animal, 'c') && $key > 0) { + $found = $animal; + break; + } + } + } +} + +?> diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/config/configured_rule.php b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/config/configured_rule.php new file mode 100644 index 00000000..d8814cd3 --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(DowngradeArrayFindRector::class); +}; diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector.php index 72580568..ad2a90b1 100644 --- a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector.php +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector.php @@ -69,7 +69,7 @@ public function refactor(Node $node): ?array return null; } - if (! $this->isName($node->expr->expr, 'array_all')) { + if (! $this->isName($node->expr->expr, 'array_any')) { return null; } diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php new file mode 100644 index 00000000..a9eae2c7 --- /dev/null +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php @@ -0,0 +1,116 @@ + str_starts_with($animal, 'c')); +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +$found = null; +foreach ($animals as $animal) { + if (str_starts_with($animal, 'c')) { + $found = $animal; + break; + } +} +CODE_SAMPLE + ), + ] + ); + } + + /** + * @param Expression $node + * @return Stmt[]|null + */ + public function refactor(Node $node): ?array + { + if (! $node->expr instanceof Assign) { + return null; + } + + if (! $node->expr->expr instanceof FuncCall) { + return null; + } + + if (! $this->isName($node->expr->expr, 'array_find')) { + return null; + } + + if ($node->expr->expr->isFirstClassCallable()) { + return null; + } + + $args = $node->expr->expr->getArgs(); + if (count($args) !== 2) { + return null; + } + + if (! $args[1]->value instanceof ArrowFunction) { + return null; + } + + $valueCond = $args[1]->value->expr; + $if = new If_($valueCond, [ + 'stmts' => [ + new Expression(new Assign($node->expr->var, $args[1]->value->params[1]->var)), + new Break_(), + ], + ]); + + return [ + // init + new Expression(new Assign($node->expr->var, new ConstFetch(new Name('null')))), + + // foreach loop + new Foreach_( + $args[0]->value, + $args[1]->value->params[0]->var, + isset($args[1]->value->params[1]->var) + ? [ + 'keyVar' => $args[1]->value->params[1]->var, + 'stmts' => [$if], + ] + : [ + 'stmts' => [$if], + ], + ), + ]; + } +} From cb2d7b69cdfc077ed04e49c1759d4966089a497e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 11 Jul 2025 15:08:18 +0700 Subject: [PATCH 2/4] [DowngradePhp84] Add DowngradeArrayFindRector --- .../Expression/DowngradeArrayAnyRector/Fixture/with_key.php.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/Fixture/with_key.php.inc b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/Fixture/with_key.php.inc index fb36533d..bc1d448b 100644 --- a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/Fixture/with_key.php.inc +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/Fixture/with_key.php.inc @@ -6,7 +6,7 @@ class WithKey { public function run(array $animals) { - $found = array_all($animals, fn($animal, $key) => str_starts_with($animal, 'c') && $key > 0); + $found = array_any($animals, fn($animal, $key) => str_starts_with($animal, 'c') && $key > 0); } } From 0d448d5f6c7ab69ed91f2a52cbff234a7b02692c Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 11 Jul 2025 15:12:45 +0700 Subject: [PATCH 3/4] [DowngradePhp84] Add DowngradeArrayFindRector --- ...adeArrayAnyRectorTest.php => DowngradeArrayFindRectorTest.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/{DowngradeArrayAnyRectorTest.php => DowngradeArrayFindRectorTest.php} (100%) diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/DowngradeArrayAnyRectorTest.php b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/DowngradeArrayFindRectorTest.php similarity index 100% rename from rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/DowngradeArrayAnyRectorTest.php rename to rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/DowngradeArrayFindRectorTest.php From dc16add66e8a7e0102143d1a461beefec914c29c Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 11 Jul 2025 15:13:45 +0700 Subject: [PATCH 4/4] [DowngradePhp84] Add DowngradeArrayFindRector --- .../Rector/Expression/DowngradeArrayFindRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php index a9eae2c7..9c2177ea 100644 --- a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php @@ -89,7 +89,7 @@ public function refactor(Node $node): ?array $valueCond = $args[1]->value->expr; $if = new If_($valueCond, [ 'stmts' => [ - new Expression(new Assign($node->expr->var, $args[1]->value->params[1]->var)), + new Expression(new Assign($node->expr->var, $args[1]->value->params[0]->var)), new Break_(), ], ]);