From 1979cf6fb02d6f0b38dd462976eda803d3898f07 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 11 Jul 2025 14:56:05 +0700 Subject: [PATCH 1/2] [DowngradePhp84] Add DowngradeArrayAnyRector --- config/set/downgrade-php84.php | 2 + .../DowngradeArrayAnyRectorTest.php | 28 +++++ .../Fixture/fixture.php.inc | 33 +++++ .../Fixture/with_key.php.inc | 33 +++++ .../config/configured_rule.php | 10 ++ .../Expression/DowngradeArrayAnyRector.php | 118 ++++++++++++++++++ 6 files changed, 224 insertions(+) create mode 100644 rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/DowngradeArrayAnyRectorTest.php create mode 100644 rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/Fixture/fixture.php.inc create mode 100644 rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/Fixture/with_key.php.inc create mode 100644 rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/config/configured_rule.php create mode 100644 rules/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector.php diff --git a/config/set/downgrade-php84.php b/config/set/downgrade-php84.php index 4f943dea..2ccd7bd3 100644 --- a/config/set/downgrade-php84.php +++ b/config/set/downgrade-php84.php @@ -4,6 +4,7 @@ use Rector\Config\RectorConfig; use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayAllRector; +use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayAnyRector; use Rector\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector; use Rector\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector; use Rector\ValueObject\PhpVersion; @@ -14,5 +15,6 @@ DowngradeNewMethodCallWithoutParenthesesRector::class, DowngradeRoundingModeEnumRector::class, DowngradeArrayAllRector::class, + DowngradeArrayAnyRector::class, ]); }; diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/DowngradeArrayAnyRectorTest.php b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/DowngradeArrayAnyRectorTest.php new file mode 100644 index 00000000..bd9fa8b8 --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/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/DowngradeArrayAnyRector/Fixture/fixture.php.inc b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..696b05ac --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/Fixture/fixture.php.inc @@ -0,0 +1,33 @@ + 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 new file mode 100644 index 00000000..fb36533d --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/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 = true; + break; + } + } + } +} + +?> diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/config/configured_rule.php b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/config/configured_rule.php new file mode 100644 index 00000000..e631c9a2 --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(DowngradeArrayAnyRector::class); +}; diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector.php new file mode 100644 index 00000000..4794d6bd --- /dev/null +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector.php @@ -0,0 +1,118 @@ + str_starts_with($animal, 'c')); +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +$found = false; +foreach ($animals as $animal) { + if (str_starts_with($animal, 'c')) { + $found = true; + 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_all')) { + 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, new ConstFetch(new Name('true')))), + new Break_(), + ], + ]); + + return [ + // init + new Expression(new Assign($node->expr->var, new ConstFetch(new Name('false')))), + + // 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 2783aaae8d56e6051055750d2cf78f91ae397927 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Fri, 11 Jul 2025 07:57:25 +0000 Subject: [PATCH 2/2] [ci-review] Rector Rectify --- .../Rector/Expression/DowngradeArrayAnyRector.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector.php index 4794d6bd..72580568 100644 --- a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector.php +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector.php @@ -7,7 +7,6 @@ use PhpParser\Node; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Assign; -use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Name; @@ -16,7 +15,6 @@ use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\If_; -use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;