From eb90489c56d82c03940e22f943d8674c2a7d4134 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 8 Aug 2025 21:34:31 +0700 Subject: [PATCH] [DowngradePhp84] Apply on return on DowngradeArrayFindKeyRector --- .../Fixture/on_return.php.inc | 34 +++++++++++++++ .../DowngradeArrayFindKeyRector.php | 42 ++++++++++++++----- 2 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindKeyRector/Fixture/on_return.php.inc diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindKeyRector/Fixture/on_return.php.inc b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindKeyRector/Fixture/on_return.php.inc new file mode 100644 index 00000000..29e2a5b8 --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayFindKeyRector/Fixture/on_return.php.inc @@ -0,0 +1,34 @@ + str_starts_with($animal, 'c')); + } +} + +?> +----- + $animal) { + if (str_starts_with($animal, 'c')) { + $found = $idx; + break; + } + } + return $found; + } +} + +?> diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindKeyRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindKeyRector.php index ff08f559..41251574 100644 --- a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindKeyRector.php +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindKeyRector.php @@ -16,6 +16,7 @@ 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; @@ -36,7 +37,7 @@ public function __construct( public function getNodeTypes(): array { - return [Expression::class]; + return [Expression::class, Return_::class]; } public function getRuleDefinition(): RuleDefinition @@ -64,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->expr->expr instanceof FuncCall) { + if ($node instanceof Expression && ! $node->expr instanceof Assign) { return null; } - if (! $this->isName($node->expr->expr, 'array_find_key')) { + $expr = $node instanceof Expression && $node->expr instanceof Assign + ? $node->expr->expr + : $node->expr; + + if (! $expr instanceof FuncCall) { + return null; + } + + if (! $this->isName($expr, 'array_find_key')) { 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; } @@ -94,19 +103,24 @@ 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)); + $keyVar = $args[1]->value->params[1]->var ?? new Variable($this->variableNaming->createCountedValueName( 'idx', - ScopeFetcher::fetch($node) + $scope )); $valueCond = $args[1]->value->expr; $if = new If_($valueCond, [ - 'stmts' => [new Expression(new Assign($node->expr->var, $keyVar)), new Break_()], + 'stmts' => [new Expression(new Assign($variable, $keyVar)), 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_( @@ -118,5 +132,11 @@ public function refactor(Node $node): ?array ] ), ]; + + if ($node instanceof Return_) { + $result[] = new Return_($variable); + } + + return $result; } }