Skip to content

Commit 40da2f0

Browse files
[DowngradePhp84] Apply on return on DowngradeArrayAllRector (#299)
* [DowngradePhp84] Apply on return on DowngradeArrayAllRector * [DowngradePhp84] Apply on return on DowngradeArrayAllRector * [ci-review] Rector Rectify * ensure pass assign --------- Co-authored-by: GitHub Action <[email protected]>
1 parent 3ca042a commit 40da2f0

File tree

3 files changed

+74
-14
lines changed

3 files changed

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

rules/DowngradePhp74/Rector/ArrowFunction/ArrowFunctionToAnonymousFunctionRector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpParser\Node\Expr\Closure;
1313
use PhpParser\Node\Expr\Throw_;
1414
use PhpParser\Node\Expr\Variable;
15+
use PhpParser\Node\Param;
1516
use PhpParser\Node\Stmt\Expression;
1617
use PhpParser\Node\Stmt\Return_;
1718
use PHPStan\Analyser\Scope;
@@ -101,7 +102,7 @@ public function refactor(Node $node): Closure
101102
$isAlsoParam = in_array(
102103
$node->expr->expr->name,
103104
array_map(
104-
static fn ($param) => $param->var instanceof Variable ? $param->var->name : null,
105+
static fn (Param $param) => $param->var instanceof Variable ? $param->var->name : null,
105106
$node->params
106107
)
107108
);

rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010
use PhpParser\Node\Expr\BooleanNot;
1111
use PhpParser\Node\Expr\ConstFetch;
1212
use PhpParser\Node\Expr\FuncCall;
13+
use PhpParser\Node\Expr\Variable;
1314
use PhpParser\Node\Name;
1415
use PhpParser\Node\Stmt;
1516
use PhpParser\Node\Stmt\Break_;
1617
use PhpParser\Node\Stmt\Expression;
1718
use PhpParser\Node\Stmt\Foreach_;
1819
use PhpParser\Node\Stmt\If_;
20+
use PhpParser\Node\Stmt\Return_;
21+
use Rector\Naming\Naming\VariableNaming;
22+
use Rector\PHPStan\ScopeFetcher;
1923
use Rector\Rector\AbstractRector;
2024
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2125
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -27,9 +31,14 @@
2731
*/
2832
final class DowngradeArrayAllRector extends AbstractRector
2933
{
34+
public function __construct(
35+
private readonly VariableNaming $variableNaming
36+
) {
37+
}
38+
3039
public function getNodeTypes(): array
3140
{
32-
return [Expression::class];
41+
return [Expression::class, Return_::class];
3342
}
3443

3544
public function getRuleDefinition(): RuleDefinition
@@ -57,28 +66,36 @@ public function getRuleDefinition(): RuleDefinition
5766
}
5867

5968
/**
60-
* @param Expression $node
69+
* @param Expression|Return_ $node
6170
* @return Stmt[]|null
6271
*/
6372
public function refactor(Node $node): ?array
6473
{
65-
if (! $node->expr instanceof Assign) {
74+
if ($node instanceof Return_ && ! $node->expr instanceof FuncCall) {
75+
return null;
76+
}
77+
78+
if ($node instanceof Expression && ! $node->expr instanceof Assign) {
6679
return null;
6780
}
6881

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

73-
if (! $this->isName($node->expr->expr, 'array_all')) {
90+
if (! $this->isName($expr, 'array_all')) {
7491
return null;
7592
}
7693

77-
if ($node->expr->expr->isFirstClassCallable()) {
94+
if ($expr->isFirstClassCallable()) {
7895
return null;
7996
}
8097

81-
$args = $node->expr->expr->getArgs();
98+
$args = $expr->getArgs();
8299
if (count($args) !== 2) {
83100
return null;
84101
}
@@ -87,17 +104,19 @@ public function refactor(Node $node): ?array
87104
return null;
88105
}
89106

107+
$scope = ScopeFetcher::fetch($node);
108+
$variable = $node instanceof Expression && $node->expr instanceof Assign
109+
? $node->expr->var
110+
: new Variable($this->variableNaming->createCountedValueName('found', $scope));
111+
90112
$valueCond = $args[1]->value->expr;
91113
$if = new If_(new BooleanNot($valueCond), [
92-
'stmts' => [
93-
new Expression(new Assign($node->expr->var, new ConstFetch(new Name('false')))),
94-
new Break_(),
95-
],
114+
'stmts' => [new Expression(new Assign($variable, new ConstFetch(new Name('false')))), new Break_()],
96115
]);
97116

98-
return [
117+
$result = [
99118
// init
100-
new Expression(new Assign($node->expr->var, new ConstFetch(new Name('true')))),
119+
new Expression(new Assign($variable, new ConstFetch(new Name('true')))),
101120

102121
// foreach loop
103122
new Foreach_(
@@ -113,5 +132,11 @@ public function refactor(Node $node): ?array
113132
],
114133
),
115134
];
135+
136+
if ($node instanceof Return_) {
137+
$result[] = new Return_($variable);
138+
}
139+
140+
return $result;
116141
}
117142
}

0 commit comments

Comments
 (0)