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/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); } } diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/DowngradeArrayFindRectorTest.php b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/DowngradeArrayFindRectorTest.php new file mode 100644 index 00000000..84c0a801 --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/DowngradeArrayFindRectorTest.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..9c2177ea --- /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[0]->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], + ], + ), + ]; + } +}