From c42446531d402f4fd6df294702f968edd5e3ea5f Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 8 Aug 2025 21:39:08 +0700 Subject: [PATCH 1/2] [DowngradePhp84] Apply on return on DowngradeArrayFindRector --- .../Fixture/on_return.php.inc | 34 +++++++++++++ .../Expression/DowngradeArrayFindRector.php | 48 +++++++++++++++---- 2 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/Fixture/on_return.php.inc diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/Fixture/on_return.php.inc b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/Fixture/on_return.php.inc new file mode 100644 index 00000000..72684f20 --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector/Fixture/on_return.php.inc @@ -0,0 +1,34 @@ + str_starts_with($animal, 'c')); + } +} + +?> +----- + diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php index 9c2177ea..0b07356f 100644 --- a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php @@ -9,12 +9,16 @@ use PhpParser\Node\Expr\Assign; 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; @@ -26,9 +30,14 @@ */ final class DowngradeArrayFindRector extends AbstractRector { + public function __construct( + private readonly VariableNaming $variableNaming + ) { + } + public function getNodeTypes(): array { - return [Expression::class]; + return [Expression::class, Return_::class]; } public function getRuleDefinition(): RuleDefinition @@ -56,28 +65,36 @@ 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) { + if ($node instanceof Return_ && ! $node->expr instanceof FuncCall) { + return null; + } + + if ($node instanceof Expression && ! $node->expr instanceof Assign) { return null; } - if (! $node->expr->expr instanceof FuncCall) { + $expr = $node instanceof Expression && $node->expr instanceof Assign + ? $node->expr->expr + : $node->expr; + + if (! $expr instanceof FuncCall) { return null; } - if (! $this->isName($node->expr->expr, 'array_find')) { + if (! $this->isName($expr, 'array_find')) { 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; } @@ -86,17 +103,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_($valueCond, [ 'stmts' => [ - new Expression(new Assign($node->expr->var, $args[1]->value->params[0]->var)), + new Expression(new Assign($variable, $args[1]->value->params[0]->var)), new Break_(), ], ]); - return [ + $result = [ // init - new Expression(new Assign($node->expr->var, new ConstFetch(new Name('null')))), + new Expression(new Assign($variable, new ConstFetch(new Name('null')))), // foreach loop new Foreach_( @@ -112,5 +134,11 @@ public function refactor(Node $node): ?array ], ), ]; + + if ($node instanceof Return_) { + $result[] = new Return_($variable); + } + + return $result; } } From 630d493e7cd88edb5da48ac6303cbbc6d3da2e13 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Fri, 8 Aug 2025 14:39:52 +0000 Subject: [PATCH 2/2] [ci-review] Rector Rectify --- .../Rector/Expression/DowngradeArrayFindRector.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php index 0b07356f..12410c67 100644 --- a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php @@ -110,10 +110,7 @@ public function refactor(Node $node): ?array $valueCond = $args[1]->value->expr; $if = new If_($valueCond, [ - 'stmts' => [ - new Expression(new Assign($variable, $args[1]->value->params[0]->var)), - new Break_(), - ], + 'stmts' => [new Expression(new Assign($variable, $args[1]->value->params[0]->var)), new Break_()], ]); $result = [