Skip to content

Commit abb7e47

Browse files
[DowngradePhp84] Apply on return on DowngradeArrayFindRector (#302)
* [DowngradePhp84] Apply on return on DowngradeArrayFindRector * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <[email protected]>
1 parent 54d85e1 commit abb7e47

File tree

2 files changed

+72
-13
lines changed

2 files changed

+72
-13
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayFindRector\Fixture;
4+
5+
class OnReturn
6+
{
7+
public function run(array $animals)
8+
{
9+
return array_find($animals, fn($animal) => str_starts_with($animal, 'c'));
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayFindRector\Fixture;
18+
19+
class OnReturn
20+
{
21+
public function run(array $animals)
22+
{
23+
$found = null;
24+
foreach ($animals as $animal) {
25+
if (str_starts_with($animal, 'c')) {
26+
$found = $animal;
27+
break;
28+
}
29+
}
30+
return $found;
31+
}
32+
}
33+
34+
?>

rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99
use PhpParser\Node\Expr\Assign;
1010
use PhpParser\Node\Expr\ConstFetch;
1111
use PhpParser\Node\Expr\FuncCall;
12+
use PhpParser\Node\Expr\Variable;
1213
use PhpParser\Node\Name;
1314
use PhpParser\Node\Stmt;
1415
use PhpParser\Node\Stmt\Break_;
1516
use PhpParser\Node\Stmt\Expression;
1617
use PhpParser\Node\Stmt\Foreach_;
1718
use PhpParser\Node\Stmt\If_;
19+
use PhpParser\Node\Stmt\Return_;
20+
use Rector\Naming\Naming\VariableNaming;
21+
use Rector\PHPStan\ScopeFetcher;
1822
use Rector\Rector\AbstractRector;
1923
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2024
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -26,9 +30,14 @@
2630
*/
2731
final class DowngradeArrayFindRector extends AbstractRector
2832
{
33+
public function __construct(
34+
private readonly VariableNaming $variableNaming
35+
) {
36+
}
37+
2938
public function getNodeTypes(): array
3039
{
31-
return [Expression::class];
40+
return [Expression::class, Return_::class];
3241
}
3342

3443
public function getRuleDefinition(): RuleDefinition
@@ -56,28 +65,36 @@ public function getRuleDefinition(): RuleDefinition
5665
}
5766

5867
/**
59-
* @param Expression $node
68+
* @param Expression|Return_ $node
6069
* @return Stmt[]|null
6170
*/
6271
public function refactor(Node $node): ?array
6372
{
64-
if (! $node->expr instanceof Assign) {
73+
if ($node instanceof Return_ && ! $node->expr instanceof FuncCall) {
74+
return null;
75+
}
76+
77+
if ($node instanceof Expression && ! $node->expr instanceof Assign) {
6578
return null;
6679
}
6780

68-
if (! $node->expr->expr instanceof FuncCall) {
81+
$expr = $node instanceof Expression && $node->expr instanceof Assign
82+
? $node->expr->expr
83+
: $node->expr;
84+
85+
if (! $expr instanceof FuncCall) {
6986
return null;
7087
}
7188

72-
if (! $this->isName($node->expr->expr, 'array_find')) {
89+
if (! $this->isName($expr, 'array_find')) {
7390
return null;
7491
}
7592

76-
if ($node->expr->expr->isFirstClassCallable()) {
93+
if ($expr->isFirstClassCallable()) {
7794
return null;
7895
}
7996

80-
$args = $node->expr->expr->getArgs();
97+
$args = $expr->getArgs();
8198
if (count($args) !== 2) {
8299
return null;
83100
}
@@ -86,17 +103,19 @@ public function refactor(Node $node): ?array
86103
return null;
87104
}
88105

106+
$scope = ScopeFetcher::fetch($node);
107+
$variable = $node instanceof Expression && $node->expr instanceof Assign
108+
? $node->expr->var
109+
: new Variable($this->variableNaming->createCountedValueName('found', $scope));
110+
89111
$valueCond = $args[1]->value->expr;
90112
$if = new If_($valueCond, [
91-
'stmts' => [
92-
new Expression(new Assign($node->expr->var, $args[1]->value->params[0]->var)),
93-
new Break_(),
94-
],
113+
'stmts' => [new Expression(new Assign($variable, $args[1]->value->params[0]->var)), new Break_()],
95114
]);
96115

97-
return [
116+
$result = [
98117
// init
99-
new Expression(new Assign($node->expr->var, new ConstFetch(new Name('null')))),
118+
new Expression(new Assign($variable, new ConstFetch(new Name('null')))),
100119

101120
// foreach loop
102121
new Foreach_(
@@ -112,5 +131,11 @@ public function refactor(Node $node): ?array
112131
],
113132
),
114133
];
134+
135+
if ($node instanceof Return_) {
136+
$result[] = new Return_($variable);
137+
}
138+
139+
return $result;
115140
}
116141
}

0 commit comments

Comments
 (0)