Skip to content

Commit a81a96b

Browse files
committed
[DowngradePhp84] Add DowngradeArrayAllRector
1 parent 4b31180 commit a81a96b

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayAllRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DowngradeArrayAllRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayAllRector\Fixture;
4+
5+
class Fixture
6+
{
7+
public function run(array $animals)
8+
{
9+
$found = 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 Fixture
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+
}
31+
}
32+
33+
?>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayAllRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(DowngradeArrayAllRector::class);
10+
};
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\DowngradePhp84\Rector\Expression;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\ArrowFunction;
9+
use PhpParser\Node\Expr\Assign;
10+
use PhpParser\Node\Expr\ClassConstFetch;
11+
use PhpParser\Node\Expr\Closure;
12+
use PhpParser\Node\Expr\ConstFetch;
13+
use PhpParser\Node\Expr\FuncCall;
14+
use PhpParser\Node\Expr\MethodCall;
15+
use PhpParser\Node\Identifier;
16+
use PhpParser\Node\Name;
17+
use PhpParser\Node\Name\FullyQualified;
18+
use PhpParser\Node\Stmt\Expression;
19+
use PhpParser\Node\Stmt\Foreach_;
20+
use PhpParser\Node\Stmt\If_;
21+
use Rector\Rector\AbstractRector;
22+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
23+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
24+
25+
/**
26+
* @changelog https://php.watch/versions/8.4/array_find-array_find_key-array_any-array_all
27+
*
28+
* @see \Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayAllRector\DowngradeArrayAllRectorTest
29+
*/
30+
final class DowngradeArrayAllRector extends AbstractRector
31+
{
32+
public function getNodeTypes(): array
33+
{
34+
return [Expression::class];
35+
}
36+
37+
public function getRuleDefinition(): RuleDefinition
38+
{
39+
return new RuleDefinition(
40+
'Downgrade array_all() to foreach loop',
41+
[
42+
new CodeSample(
43+
<<<'CODE_SAMPLE'
44+
$found = array_all($animals, fn($animal) => str_starts_with($animal, 'c'));
45+
CODE_SAMPLE
46+
,
47+
<<<'CODE_SAMPLE'
48+
$found = true;
49+
foreach ($animals as $animal) {
50+
if (!str_starts_with($animal, 'c')) {
51+
$found = false;
52+
break;
53+
}
54+
}
55+
CODE_SAMPLE
56+
),
57+
]
58+
);
59+
}
60+
61+
/**
62+
* @param Expression $node
63+
*/
64+
public function refactor(Node $node): ?array
65+
{
66+
if (! $node->expr instanceof Assign) {
67+
return null;
68+
}
69+
70+
if (! $node->expr->expr instanceof FuncCall) {
71+
return null;
72+
}
73+
74+
if (! $this->isName($node->expr->expr, 'array_all')) {
75+
return null;
76+
}
77+
78+
if ($node->expr->expr->isFirstClassCallable()) {
79+
return null;
80+
}
81+
82+
$args = $node->expr->expr->getArgs();
83+
if (count($args) !== 2) {
84+
return null;
85+
}
86+
87+
if (! $args[1]->value instanceof ArrowFunction) {
88+
return null;
89+
}
90+
91+
$if = new If_($args[1]->value->expr, [
92+
'stmts' => [
93+
new Expression(new Assign(
94+
$node->expr->var,
95+
new ConstFetch(new Name('false'))
96+
)),
97+
new Node\Stmt\Break_(),
98+
],
99+
]);
100+
101+
return [
102+
// init
103+
new Expression(new Assign(
104+
$node->expr->var,
105+
new ConstFetch(new Name('true'))
106+
)),
107+
108+
// foreach loop
109+
new Foreach_(
110+
$args[0]->value,
111+
$args[1]->value->params[0]->var,
112+
isset($args[1]->value->params[1]->var)
113+
? [
114+
'keyVar' => $args[1]->value->params[1]->var,
115+
'stmts' => [$if]
116+
]
117+
: ['stmts' => [$if]],
118+
)
119+
];
120+
}
121+
}

0 commit comments

Comments
 (0)