Skip to content

Commit 6fae74e

Browse files
[DowngradePhp84] Add DowngradeArrayAllRector (#290)
* [DowngradePhp84] Add DowngradeArrayAllRector * [DowngradePhp84] Add DowngradeArrayAllRector * [DowngradePhp84] Add DowngradeArrayAllRector * key * [ci-review] Rector Rectify * key * fix --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent 4b31180 commit 6fae74e

File tree

7 files changed

+228
-2
lines changed

7 files changed

+228
-2
lines changed

config/set/downgrade-php84.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6+
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayAllRector;
67
use Rector\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector;
78
use Rector\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector;
89
use Rector\ValueObject\PhpVersion;
@@ -12,5 +13,6 @@
1213
$rectorConfig->rules([
1314
DowngradeNewMethodCallWithoutParenthesesRector::class,
1415
DowngradeRoundingModeEnumRector::class,
16+
DowngradeArrayAllRector::class,
1517
]);
1618
};

rector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
->withPhpSets(php82: true)
1313
->withSets([PHPUnitSetList::PHPUNIT_100, PHPUnitSetList::PHPUNIT_CODE_QUALITY])
1414
->withPreparedSets(
15+
deadCode: true,
1516
codeQuality: true,
1617
codingStyle: true,
17-
deadCode: true,
18+
typeDeclarations: true,
1819
privatization: true,
1920
naming: true,
20-
typeDeclarations: true,
2121
earlyReturn: true,
2222
rectorPreset: true
2323
)->withPaths([
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: 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 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\DowngradeArrayAllRector\Fixture;
18+
19+
class WithKey
20+
{
21+
public function run(array $animals)
22+
{
23+
$found = true;
24+
foreach ($animals as $key => $animal) {
25+
if (!(str_starts_with($animal, 'c') && $key > 0)) {
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: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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\DowngradeArrayAllRector\DowngradeArrayAllRectorTest
28+
*/
29+
final class DowngradeArrayAllRector 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_all() to foreach loop',
40+
[
41+
new CodeSample(
42+
<<<'CODE_SAMPLE'
43+
$found = array_all($animals, fn($animal) => str_starts_with($animal, 'c'));
44+
CODE_SAMPLE
45+
,
46+
<<<'CODE_SAMPLE'
47+
$found = true;
48+
foreach ($animals as $animal) {
49+
if (!str_starts_with($animal, 'c')) {
50+
$found = false;
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+
$valueCond->setAttribute(AttributeKey::ORIGINAL_NODE, null);
93+
94+
$if = new If_(new BooleanNot($valueCond), [
95+
'stmts' => [
96+
new Expression(new Assign($node->expr->var, new ConstFetch(new Name('false')))),
97+
new Break_(),
98+
],
99+
]);
100+
101+
return [
102+
// init
103+
new Expression(new Assign($node->expr->var, new ConstFetch(new Name('true')))),
104+
105+
// foreach loop
106+
new Foreach_(
107+
$args[0]->value,
108+
$args[1]->value->params[0]->var,
109+
isset($args[1]->value->params[1]->var)
110+
? [
111+
'keyVar' => $args[1]->value->params[1]->var,
112+
'stmts' => [$if],
113+
]
114+
: [
115+
'stmts' => [$if],
116+
],
117+
),
118+
];
119+
}
120+
}

0 commit comments

Comments
 (0)