Skip to content

Commit 80ea220

Browse files
[DowngradePhp84] Add DowngradeArrayAnyRector (#291)
* [DowngradePhp84] Add DowngradeArrayAnyRector * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <[email protected]>
1 parent 6fae74e commit 80ea220

File tree

6 files changed

+222
-0
lines changed

6 files changed

+222
-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: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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\ConstFetch;
11+
use PhpParser\Node\Expr\FuncCall;
12+
use PhpParser\Node\Name;
13+
use PhpParser\Node\Stmt;
14+
use PhpParser\Node\Stmt\Break_;
15+
use PhpParser\Node\Stmt\Expression;
16+
use PhpParser\Node\Stmt\Foreach_;
17+
use PhpParser\Node\Stmt\If_;
18+
use Rector\Rector\AbstractRector;
19+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
20+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
21+
22+
/**
23+
* @changelog https://php.watch/versions/8.4/array_find-array_find_key-array_any-array_all
24+
*
25+
* @see \Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayAnyRector\DowngradeArrayAnyRectorTest
26+
*/
27+
final class DowngradeArrayAnyRector extends AbstractRector
28+
{
29+
public function getNodeTypes(): array
30+
{
31+
return [Expression::class];
32+
}
33+
34+
public function getRuleDefinition(): RuleDefinition
35+
{
36+
return new RuleDefinition(
37+
'Downgrade array_any() to foreach loop',
38+
[
39+
new CodeSample(
40+
<<<'CODE_SAMPLE'
41+
$found = array_any($animals, fn($animal) => str_starts_with($animal, 'c'));
42+
CODE_SAMPLE
43+
,
44+
<<<'CODE_SAMPLE'
45+
$found = false;
46+
foreach ($animals as $animal) {
47+
if (str_starts_with($animal, 'c')) {
48+
$found = true;
49+
break;
50+
}
51+
}
52+
CODE_SAMPLE
53+
),
54+
]
55+
);
56+
}
57+
58+
/**
59+
* @param Expression $node
60+
* @return Stmt[]|null
61+
*/
62+
public function refactor(Node $node): ?array
63+
{
64+
if (! $node->expr instanceof Assign) {
65+
return null;
66+
}
67+
68+
if (! $node->expr->expr instanceof FuncCall) {
69+
return null;
70+
}
71+
72+
if (! $this->isName($node->expr->expr, 'array_all')) {
73+
return null;
74+
}
75+
76+
if ($node->expr->expr->isFirstClassCallable()) {
77+
return null;
78+
}
79+
80+
$args = $node->expr->expr->getArgs();
81+
if (count($args) !== 2) {
82+
return null;
83+
}
84+
85+
if (! $args[1]->value instanceof ArrowFunction) {
86+
return null;
87+
}
88+
89+
$valueCond = $args[1]->value->expr;
90+
$if = new If_($valueCond, [
91+
'stmts' => [
92+
new Expression(new Assign($node->expr->var, new ConstFetch(new Name('true')))),
93+
new Break_(),
94+
],
95+
]);
96+
97+
return [
98+
// init
99+
new Expression(new Assign($node->expr->var, new ConstFetch(new Name('false')))),
100+
101+
// foreach loop
102+
new Foreach_(
103+
$args[0]->value,
104+
$args[1]->value->params[0]->var,
105+
isset($args[1]->value->params[1]->var)
106+
? [
107+
'keyVar' => $args[1]->value->params[1]->var,
108+
'stmts' => [$if],
109+
]
110+
: [
111+
'stmts' => [$if],
112+
],
113+
),
114+
];
115+
}
116+
}

0 commit comments

Comments
 (0)