From 1088f72ef7e738ceb9dce35e3c87099c2396b9d0 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 8 Aug 2025 21:12:32 +0700 Subject: [PATCH 1/4] [DowngradePhp84] Apply on return on DowngradeArrayAllRector --- .../Expression/DowngradeArrayAllRector.php | 45 ++++++++++++++----- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php index 74dc4ccc..7187a734 100644 --- a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php @@ -10,12 +10,16 @@ use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Name; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Break_; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\If_; +use PhpParser\Node\Stmt\Return_; +use Rector\Naming\Naming\VariableNaming; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -27,9 +31,15 @@ */ final class DowngradeArrayAllRector extends AbstractRector { + public function __construct( + private VariableNaming $variableNaming + ) + { + } + public function getNodeTypes(): array { - return [Expression::class]; + return [Expression::class, Return_::class]; } public function getRuleDefinition(): RuleDefinition @@ -57,28 +67,28 @@ public function getRuleDefinition(): RuleDefinition } /** - * @param Expression $node + * @param Expression|Return_ $node * @return Stmt[]|null */ public function refactor(Node $node): ?array { - if (! $node->expr instanceof Assign) { - return null; - } + $expr = $node instanceof Expression && $node->expr instanceof Assign + ? $node->expr->expr + : $node->expr; - if (! $node->expr->expr instanceof FuncCall) { + if (! $expr instanceof FuncCall) { return null; } - if (! $this->isName($node->expr->expr, 'array_all')) { + if (! $this->isName($expr, 'array_all')) { return null; } - if ($node->expr->expr->isFirstClassCallable()) { + if ($expr->isFirstClassCallable()) { return null; } - $args = $node->expr->expr->getArgs(); + $args = $expr->getArgs(); if (count($args) !== 2) { return null; } @@ -87,17 +97,22 @@ public function refactor(Node $node): ?array return null; } + $scope = ScopeFetcher::fetch($node); + $variable = $node instanceof Expression && $node->expr instanceof Assign + ? $node->expr->var + : new Variable($this->variableNaming->createCountedValueName('found', $scope)); + $valueCond = $args[1]->value->expr; $if = new If_(new BooleanNot($valueCond), [ 'stmts' => [ - new Expression(new Assign($node->expr->var, new ConstFetch(new Name('false')))), + new Expression(new Assign($variable, new ConstFetch(new Name('false')))), new Break_(), ], ]); - return [ + $result = [ // init - new Expression(new Assign($node->expr->var, new ConstFetch(new Name('true')))), + new Expression(new Assign($variable, new ConstFetch(new Name('true')))), // foreach loop new Foreach_( @@ -113,5 +128,11 @@ public function refactor(Node $node): ?array ], ), ]; + + if ($node instanceof Return_) { + $result[] = new Return_($variable); + } + + return $result; } } From b89656b56f418e4633a1a88c304991da467efed4 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 8 Aug 2025 21:12:36 +0700 Subject: [PATCH 2/4] [DowngradePhp84] Apply on return on DowngradeArrayAllRector --- .../Fixture/on_return.php.inc | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/on_return.php.inc diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/on_return.php.inc b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/on_return.php.inc new file mode 100644 index 00000000..21dd6ff4 --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/on_return.php.inc @@ -0,0 +1,34 @@ + str_starts_with($animal, 'c')); + } +} + +?> +----- + From f376d3c5cff4afc17093efd3d3d6f04482f713f0 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Fri, 8 Aug 2025 14:14:50 +0000 Subject: [PATCH 3/4] [ci-review] Rector Rectify --- .../ArrowFunctionToAnonymousFunctionRector.php | 3 ++- .../Rector/Expression/DowngradeArrayAllRector.php | 10 +++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/rules/DowngradePhp74/Rector/ArrowFunction/ArrowFunctionToAnonymousFunctionRector.php b/rules/DowngradePhp74/Rector/ArrowFunction/ArrowFunctionToAnonymousFunctionRector.php index a2d471a8..4f6c99de 100644 --- a/rules/DowngradePhp74/Rector/ArrowFunction/ArrowFunctionToAnonymousFunctionRector.php +++ b/rules/DowngradePhp74/Rector/ArrowFunction/ArrowFunctionToAnonymousFunctionRector.php @@ -12,6 +12,7 @@ use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\Throw_; use PhpParser\Node\Expr\Variable; +use PhpParser\Node\Param; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Return_; use PHPStan\Analyser\Scope; @@ -101,7 +102,7 @@ public function refactor(Node $node): Closure $isAlsoParam = in_array( $node->expr->expr->name, array_map( - static fn ($param) => $param->var instanceof Variable ? $param->var->name : null, + static fn (Param $param) => $param->var instanceof Variable ? $param->var->name : null, $node->params ) ); diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php index 7187a734..5e3233e7 100644 --- a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php @@ -32,9 +32,8 @@ final class DowngradeArrayAllRector extends AbstractRector { public function __construct( - private VariableNaming $variableNaming - ) - { + private readonly VariableNaming $variableNaming + ) { } public function getNodeTypes(): array @@ -104,10 +103,7 @@ public function refactor(Node $node): ?array $valueCond = $args[1]->value->expr; $if = new If_(new BooleanNot($valueCond), [ - 'stmts' => [ - new Expression(new Assign($variable, new ConstFetch(new Name('false')))), - new Break_(), - ], + 'stmts' => [new Expression(new Assign($variable, new ConstFetch(new Name('false')))), new Break_()], ]); $result = [ From 73e8fef84acefd0e12a39945bc39ea2f70791e34 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 8 Aug 2025 21:18:44 +0700 Subject: [PATCH 4/4] ensure pass assign --- .../Rector/Expression/DowngradeArrayAllRector.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php index 5e3233e7..d3c3166d 100644 --- a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php @@ -71,6 +71,14 @@ public function getRuleDefinition(): RuleDefinition */ public function refactor(Node $node): ?array { + if ($node instanceof Return_ && ! $node->expr instanceof FuncCall) { + return null; + } + + if ($node instanceof Expression && ! $node->expr instanceof Assign) { + return null; + } + $expr = $node instanceof Expression && $node->expr instanceof Assign ? $node->expr->expr : $node->expr;