Skip to content

Commit c371a23

Browse files
committed
[DowngradePhp84] Apply on return on DowngradeArrayAnyRector
1 parent 40da2f0 commit c371a23

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

rules/DowngradePhp84/Rector/Expression/DowngradeArrayAnyRector.php

Lines changed: 38 additions & 10 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 DowngradeArrayAnyRector 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_any')) {
89+
if (! $this->isName($expr, 'array_any')) {
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,22 @@ 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, [
91113
'stmts' => [
92-
new Expression(new Assign($node->expr->var, new ConstFetch(new Name('true')))),
114+
new Expression(new Assign($variable, new ConstFetch(new Name('true')))),
93115
new Break_(),
94116
],
95117
]);
96118

97-
return [
119+
$result = [
98120
// init
99-
new Expression(new Assign($node->expr->var, new ConstFetch(new Name('false')))),
121+
new Expression(new Assign($variable, new ConstFetch(new Name('false')))),
100122

101123
// foreach loop
102124
new Foreach_(
@@ -112,5 +134,11 @@ public function refactor(Node $node): ?array
112134
],
113135
),
114136
];
137+
138+
if ($node instanceof Return_) {
139+
$result[] = new Return_($variable);
140+
}
141+
142+
return $result;
115143
}
116144
}

0 commit comments

Comments
 (0)