Skip to content

Commit 1979cf6

Browse files
committed
[DowngradePhp84] Add DowngradeArrayAnyRector
1 parent 6fae74e commit 1979cf6

File tree

6 files changed

+224
-0
lines changed

6 files changed

+224
-0
lines changed

config/set/downgrade-php84.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Rector\Config\RectorConfig;
66
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayAllRector;
7+
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayAnyRector;
78
use Rector\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector;
89
use Rector\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector;
910
use Rector\ValueObject\PhpVersion;
@@ -14,5 +15,6 @@
1415
DowngradeNewMethodCallWithoutParenthesesRector::class,
1516
DowngradeRoundingModeEnumRector::class,
1617
DowngradeArrayAllRector::class,
18+
DowngradeArrayAnyRector::class,
1719
]);
1820
};
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\DowngradeArrayAnyRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DowngradeArrayAnyRectorTest 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\DowngradeArrayAnyRector\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\DowngradeArrayAnyRector\Fixture;
18+
19+
class Fixture
20+
{
21+
public function run(array $animals)
22+
{
23+
$found = false;
24+
foreach ($animals as $animal) {
25+
if (str_starts_with($animal, 'c')) {
26+
$found = true;
27+
break;
28+
}
29+
}
30+
}
31+
}
32+
33+
?>
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\DowngradeArrayAnyRector\Fixture;
4+
5+
class WithKey
6+
{
7+
public function run(array $animals)
8+
{
9+
$found = array_all($animals, fn($animal, $key) => str_starts_with($animal, 'c') && $key > 0);
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayAnyRector\Fixture;
18+
19+
class WithKey
20+
{
21+
public function run(array $animals)
22+
{
23+
$found = false;
24+
foreach ($animals as $key => $animal) {
25+
if (str_starts_with($animal, 'c') && $key > 0) {
26+
$found = true;
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\DowngradeArrayAnyRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(DowngradeArrayAnyRector::class);
10+
};
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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\BooleanNot;
11+
use PhpParser\Node\Expr\ConstFetch;
12+
use PhpParser\Node\Expr\FuncCall;
13+
use PhpParser\Node\Name;
14+
use PhpParser\Node\Stmt;
15+
use PhpParser\Node\Stmt\Break_;
16+
use PhpParser\Node\Stmt\Expression;
17+
use PhpParser\Node\Stmt\Foreach_;
18+
use PhpParser\Node\Stmt\If_;
19+
use Rector\NodeTypeResolver\Node\AttributeKey;
20+
use Rector\Rector\AbstractRector;
21+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
22+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
23+
24+
/**
25+
* @changelog https://php.watch/versions/8.4/array_find-array_find_key-array_any-array_all
26+
*
27+
* @see \Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayAnyRector\DowngradeArrayAnyRectorTest
28+
*/
29+
final class DowngradeArrayAnyRector extends AbstractRector
30+
{
31+
public function getNodeTypes(): array
32+
{
33+
return [Expression::class];
34+
}
35+
36+
public function getRuleDefinition(): RuleDefinition
37+
{
38+
return new RuleDefinition(
39+
'Downgrade array_any() to foreach loop',
40+
[
41+
new CodeSample(
42+
<<<'CODE_SAMPLE'
43+
$found = array_any($animals, fn($animal) => str_starts_with($animal, 'c'));
44+
CODE_SAMPLE
45+
,
46+
<<<'CODE_SAMPLE'
47+
$found = false;
48+
foreach ($animals as $animal) {
49+
if (str_starts_with($animal, 'c')) {
50+
$found = true;
51+
break;
52+
}
53+
}
54+
CODE_SAMPLE
55+
),
56+
]
57+
);
58+
}
59+
60+
/**
61+
* @param Expression $node
62+
* @return Stmt[]|null
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+
$valueCond = $args[1]->value->expr;
92+
$if = new If_($valueCond, [
93+
'stmts' => [
94+
new Expression(new Assign($node->expr->var, new ConstFetch(new Name('true')))),
95+
new Break_(),
96+
],
97+
]);
98+
99+
return [
100+
// init
101+
new Expression(new Assign($node->expr->var, new ConstFetch(new Name('false')))),
102+
103+
// foreach loop
104+
new Foreach_(
105+
$args[0]->value,
106+
$args[1]->value->params[0]->var,
107+
isset($args[1]->value->params[1]->var)
108+
? [
109+
'keyVar' => $args[1]->value->params[1]->var,
110+
'stmts' => [$if],
111+
]
112+
: [
113+
'stmts' => [$if],
114+
],
115+
),
116+
];
117+
}
118+
}

0 commit comments

Comments
 (0)