Skip to content

Commit 54d85e1

Browse files
authored
[DowngradePhp84] Apply on return on DowngradeArrayFindKeyRector (#301)
1 parent 76cf96b commit 54d85e1

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
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\DowngradeArrayFindKeyRector\Fixture;
4+
5+
class OnReturn
6+
{
7+
public function run(array $animals)
8+
{
9+
return array_find_key($animals, fn($animal) => str_starts_with($animal, 'c'));
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayFindKeyRector\Fixture;
18+
19+
class OnReturn
20+
{
21+
public function run(array $animals)
22+
{
23+
$found = null;
24+
foreach ($animals as $idx => $animal) {
25+
if (str_starts_with($animal, 'c')) {
26+
$found = $idx;
27+
break;
28+
}
29+
}
30+
return $found;
31+
}
32+
}
33+
34+
?>

rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindKeyRector.php

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PhpParser\Node\Stmt\Expression;
1717
use PhpParser\Node\Stmt\Foreach_;
1818
use PhpParser\Node\Stmt\If_;
19+
use PhpParser\Node\Stmt\Return_;
1920
use Rector\Naming\Naming\VariableNaming;
2021
use Rector\PHPStan\ScopeFetcher;
2122
use Rector\Rector\AbstractRector;
@@ -36,7 +37,7 @@ public function __construct(
3637

3738
public function getNodeTypes(): array
3839
{
39-
return [Expression::class];
40+
return [Expression::class, Return_::class];
4041
}
4142

4243
public function getRuleDefinition(): RuleDefinition
@@ -64,28 +65,36 @@ public function getRuleDefinition(): RuleDefinition
6465
}
6566

6667
/**
67-
* @param Expression $node
68+
* @param Expression|Return_ $node
6869
* @return Stmt[]|null
6970
*/
7071
public function refactor(Node $node): ?array
7172
{
72-
if (! $node->expr instanceof Assign) {
73+
if ($node instanceof Return_ && ! $node->expr instanceof FuncCall) {
7374
return null;
7475
}
7576

76-
if (! $node->expr->expr instanceof FuncCall) {
77+
if ($node instanceof Expression && ! $node->expr instanceof Assign) {
7778
return null;
7879
}
7980

80-
if (! $this->isName($node->expr->expr, 'array_find_key')) {
81+
$expr = $node instanceof Expression && $node->expr instanceof Assign
82+
? $node->expr->expr
83+
: $node->expr;
84+
85+
if (! $expr instanceof FuncCall) {
86+
return null;
87+
}
88+
89+
if (! $this->isName($expr, 'array_find_key')) {
8190
return null;
8291
}
8392

84-
if ($node->expr->expr->isFirstClassCallable()) {
93+
if ($expr->isFirstClassCallable()) {
8594
return null;
8695
}
8796

88-
$args = $node->expr->expr->getArgs();
97+
$args = $expr->getArgs();
8998
if (count($args) !== 2) {
9099
return null;
91100
}
@@ -94,19 +103,24 @@ public function refactor(Node $node): ?array
94103
return null;
95104
}
96105

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+
97111
$keyVar = $args[1]->value->params[1]->var ?? new Variable($this->variableNaming->createCountedValueName(
98112
'idx',
99-
ScopeFetcher::fetch($node)
113+
$scope
100114
));
101115

102116
$valueCond = $args[1]->value->expr;
103117
$if = new If_($valueCond, [
104-
'stmts' => [new Expression(new Assign($node->expr->var, $keyVar)), new Break_()],
118+
'stmts' => [new Expression(new Assign($variable, $keyVar)), new Break_()],
105119
]);
106120

107-
return [
121+
$result = [
108122
// init
109-
new Expression(new Assign($node->expr->var, new ConstFetch(new Name('null')))),
123+
new Expression(new Assign($variable, new ConstFetch(new Name('null')))),
110124

111125
// foreach loop
112126
new Foreach_(
@@ -118,5 +132,11 @@ public function refactor(Node $node): ?array
118132
]
119133
),
120134
];
135+
136+
if ($node instanceof Return_) {
137+
$result[] = new Return_($variable);
138+
}
139+
140+
return $result;
121141
}
122142
}

0 commit comments

Comments
 (0)