From a81a96b2cdafaae9ffe6cbc8df89e67d65ff1095 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 11 Jul 2025 13:56:39 +0700 Subject: [PATCH 1/7] [DowngradePhp84] Add DowngradeArrayAllRector --- .../DowngradeArrayAllRectorTest.php | 28 ++++ .../Fixture/fixture.php.inc | 33 +++++ .../config/configured_rule.php | 10 ++ .../Expression/DowngradeArrayAllRector.php | 121 ++++++++++++++++++ 4 files changed, 192 insertions(+) create mode 100644 rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/DowngradeArrayAllRectorTest.php create mode 100644 rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/fixture.php.inc create mode 100644 rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/config/configured_rule.php create mode 100644 rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/DowngradeArrayAllRectorTest.php b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/DowngradeArrayAllRectorTest.php new file mode 100644 index 00000000..10c3a076 --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/DowngradeArrayAllRectorTest.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/DowngradeArrayAllRector/Fixture/fixture.php.inc b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..6eeae69d --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/fixture.php.inc @@ -0,0 +1,33 @@ + str_starts_with($animal, 'c')); + } +} + +?> +----- + diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/config/configured_rule.php b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/config/configured_rule.php new file mode 100644 index 00000000..00fe21e0 --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(DowngradeArrayAllRector::class); +}; diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php new file mode 100644 index 00000000..4cd4a67d --- /dev/null +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php @@ -0,0 +1,121 @@ + str_starts_with($animal, 'c')); +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +$found = true; +foreach ($animals as $animal) { + if (!str_starts_with($animal, 'c')) { + $found = false; + break; + } +} +CODE_SAMPLE + ), + ] + ); + } + + /** + * @param Expression $node + */ + 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; + } + + $if = new If_($args[1]->value->expr, [ + 'stmts' => [ + new Expression(new Assign( + $node->expr->var, + new ConstFetch(new Name('false')) + )), + new Node\Stmt\Break_(), + ], + ]); + + return [ + // init + new Expression(new Assign( + $node->expr->var, + new ConstFetch(new Name('true')) + )), + + // 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 2ac3e9f284ce5b0bdc4e9c63d2da9aee4586ae44 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 11 Jul 2025 13:56:55 +0700 Subject: [PATCH 2/7] [DowngradePhp84] Add DowngradeArrayAllRector --- config/set/downgrade-php84.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/set/downgrade-php84.php b/config/set/downgrade-php84.php index d3d914cc..4f943dea 100644 --- a/config/set/downgrade-php84.php +++ b/config/set/downgrade-php84.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Rector\Config\RectorConfig; +use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayAllRector; use Rector\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector; use Rector\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector; use Rector\ValueObject\PhpVersion; @@ -12,5 +13,6 @@ $rectorConfig->rules([ DowngradeNewMethodCallWithoutParenthesesRector::class, DowngradeRoundingModeEnumRector::class, + DowngradeArrayAllRector::class, ]); }; From 73a21bcb13c577f9f0890ed9a883b9173e006e11 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 11 Jul 2025 13:57:18 +0700 Subject: [PATCH 3/7] [DowngradePhp84] Add DowngradeArrayAllRector --- rector.php | 4 +-- .../Expression/DowngradeArrayAllRector.php | 26 +++++++------------ 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/rector.php b/rector.php index 2dd42da1..1f6fe9ad 100644 --- a/rector.php +++ b/rector.php @@ -12,12 +12,12 @@ ->withPhpSets(php82: true) ->withSets([PHPUnitSetList::PHPUNIT_100, PHPUnitSetList::PHPUNIT_CODE_QUALITY]) ->withPreparedSets( + deadCode: true, codeQuality: true, codingStyle: true, - deadCode: true, + typeDeclarations: true, privatization: true, naming: true, - typeDeclarations: true, earlyReturn: true, rectorPreset: true )->withPaths([ diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php index 4cd4a67d..4b507464 100644 --- a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php @@ -7,14 +7,10 @@ use PhpParser\Node; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Assign; -use PhpParser\Node\Expr\ClassConstFetch; -use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; -use PhpParser\Node\Expr\MethodCall; -use PhpParser\Node\Identifier; use PhpParser\Node\Name; -use PhpParser\Node\Name\FullyQualified; +use PhpParser\Node\Stmt\Break_; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\If_; @@ -90,20 +86,14 @@ public function refactor(Node $node): ?array $if = new If_($args[1]->value->expr, [ 'stmts' => [ - new Expression(new Assign( - $node->expr->var, - new ConstFetch(new Name('false')) - )), - new Node\Stmt\Break_(), + new Expression(new Assign($node->expr->var, new ConstFetch(new Name('false')))), + new Break_(), ], ]); return [ // init - new Expression(new Assign( - $node->expr->var, - new ConstFetch(new Name('true')) - )), + new Expression(new Assign($node->expr->var, new ConstFetch(new Name('true')))), // foreach loop new Foreach_( @@ -112,10 +102,12 @@ public function refactor(Node $node): ?array isset($args[1]->value->params[1]->var) ? [ 'keyVar' => $args[1]->value->params[1]->var, - 'stmts' => [$if] + 'stmts' => [$if], ] - : ['stmts' => [$if]], - ) + : [ + 'stmts' => [$if], + ], + ), ]; } } From 5305a54b4ef146d13a742e3cb0f24a1ae50788eb Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 11 Jul 2025 14:01:40 +0700 Subject: [PATCH 4/7] key --- .../Fixture/fixture.php.inc | 2 +- .../Fixture/with_key.php.inc | 33 +++++++++++++++++++ .../Expression/DowngradeArrayAllRector.php | 6 +++- 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/with_key.php.inc diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/fixture.php.inc b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/fixture.php.inc index 6eeae69d..b173b551 100644 --- a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/fixture.php.inc +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/fixture.php.inc @@ -22,7 +22,7 @@ class Fixture { $found = true; foreach ($animals as $animal) { - if (str_starts_with($animal, 'c')) { + if (! str_starts_with($animal, 'c')) { $found = false; break; } diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/with_key.php.inc b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/with_key.php.inc new file mode 100644 index 00000000..8869e4a5 --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/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 = false; + break; + } + } + } +} + +?> diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php index 4b507464..939a2085 100644 --- a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php @@ -7,6 +7,7 @@ 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; @@ -14,6 +15,7 @@ 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; @@ -84,7 +86,9 @@ public function refactor(Node $node): ?array return null; } - $if = new If_($args[1]->value->expr, [ + $valueCond = $args[1]->value->expr; + $valueCond->setAttribute(AttributeKey::ORIGINAL_NODE, null); + $if = new If_(new BooleanNot($valueCond), [ 'stmts' => [ new Expression(new Assign($node->expr->var, new ConstFetch(new Name('false')))), new Break_(), From 2696e89de8e1a27d3acd30b300ee6117f2f8fc71 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Fri, 11 Jul 2025 07:02:29 +0000 Subject: [PATCH 5/7] [ci-review] Rector Rectify --- .../DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php | 1 + 1 file changed, 1 insertion(+) diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php index 939a2085..5b8f960c 100644 --- a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php @@ -88,6 +88,7 @@ public function refactor(Node $node): ?array $valueCond = $args[1]->value->expr; $valueCond->setAttribute(AttributeKey::ORIGINAL_NODE, null); + $if = new If_(new BooleanNot($valueCond), [ 'stmts' => [ new Expression(new Assign($node->expr->var, new ConstFetch(new Name('false')))), From 27871b85f84c85825549122fdad777e9bb385854 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 11 Jul 2025 14:03:58 +0700 Subject: [PATCH 6/7] key --- .../Expression/DowngradeArrayAllRector/Fixture/fixture.php.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/fixture.php.inc b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/fixture.php.inc index b173b551..29580082 100644 --- a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/fixture.php.inc +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/fixture.php.inc @@ -22,7 +22,7 @@ class Fixture { $found = true; foreach ($animals as $animal) { - if (! str_starts_with($animal, 'c')) { + if (!str_starts_with($animal, 'c')) { $found = false; break; } From f7c17eef700d16e9060da04e6ed2990756fcc6e8 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 11 Jul 2025 14:05:30 +0700 Subject: [PATCH 7/7] fix --- .../Rector/Expression/DowngradeArrayAllRector.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php index 5b8f960c..2df6cbf3 100644 --- a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php @@ -11,6 +11,7 @@ use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Name; +use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Break_; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Foreach_; @@ -58,6 +59,7 @@ public function getRuleDefinition(): RuleDefinition /** * @param Expression $node + * @return Stmt[]|null */ public function refactor(Node $node): ?array {