Skip to content

Commit 6f2327b

Browse files
authored
[DowngradePhp84] Add DowngradeArrayFindKeyRector (#293)
1 parent 197f088 commit 6f2327b

File tree

6 files changed

+228
-0
lines changed

6 files changed

+228
-0
lines changed

config/set/downgrade-php84.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Rector\Config\RectorConfig;
66
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayAllRector;
77
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayAnyRector;
8+
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayFindKeyRector;
89
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayFindRector;
910
use Rector\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector;
1011
use Rector\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector;
@@ -18,5 +19,6 @@
1819
DowngradeArrayAllRector::class,
1920
DowngradeArrayAnyRector::class,
2021
DowngradeArrayFindRector::class,
22+
DowngradeArrayFindKeyRector::class,
2123
]);
2224
};
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\DowngradeArrayFindKeyRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class DowngradeArrayFindKeyRectorTest 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\DowngradeArrayFindKeyRector\Fixture;
4+
5+
class Fixture
6+
{
7+
public function run(array $animals)
8+
{
9+
$found = array_find_key($animals, fn($animal) => str_starts_with($animal, 'c'));
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayFindKeyRector\Fixture;
18+
19+
class Fixture
20+
{
21+
public function run(array $animals)
22+
{
23+
$found = null;
24+
foreach ($animals as $idx => $animal) {
25+
if (str_starts_with($animal, 'c')) {
26+
$found = $idx;
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_find_key($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 = null;
24+
foreach ($animals as $key => $animal) {
25+
if (str_starts_with($animal, 'c') && $key > 0) {
26+
$found = $key;
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\DowngradeArrayFindKeyRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(DowngradeArrayFindKeyRector::class);
10+
};
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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\Expr\Variable;
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\Naming\Naming\VariableNaming;
20+
use Rector\PHPStan\ScopeFetcher;
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\DowngradeArrayFindKeyRector\DowngradeArrayFindKeyRectorTest
29+
*/
30+
final class DowngradeArrayFindKeyRector extends AbstractRector
31+
{
32+
public function __construct(
33+
private readonly VariableNaming $variableNaming
34+
) {
35+
}
36+
37+
public function getNodeTypes(): array
38+
{
39+
return [Expression::class];
40+
}
41+
42+
public function getRuleDefinition(): RuleDefinition
43+
{
44+
return new RuleDefinition(
45+
'Downgrade array_find_key() to foreach loop',
46+
[
47+
new CodeSample(
48+
<<<'CODE_SAMPLE'
49+
$found = array_find_key($animals, fn($animal) => str_starts_with($animal, 'c'));
50+
CODE_SAMPLE
51+
,
52+
<<<'CODE_SAMPLE'
53+
$found = null;
54+
foreach ($animals as $idx => $animal) {
55+
if (str_starts_with($animal, 'c')) {
56+
$found = $idx;
57+
break;
58+
}
59+
}
60+
CODE_SAMPLE
61+
),
62+
]
63+
);
64+
}
65+
66+
/**
67+
* @param Expression $node
68+
* @return Stmt[]|null
69+
*/
70+
public function refactor(Node $node): ?array
71+
{
72+
if (! $node->expr instanceof Assign) {
73+
return null;
74+
}
75+
76+
if (! $node->expr->expr instanceof FuncCall) {
77+
return null;
78+
}
79+
80+
if (! $this->isName($node->expr->expr, 'array_find_key')) {
81+
return null;
82+
}
83+
84+
if ($node->expr->expr->isFirstClassCallable()) {
85+
return null;
86+
}
87+
88+
$args = $node->expr->expr->getArgs();
89+
if (count($args) !== 2) {
90+
return null;
91+
}
92+
93+
if (! $args[1]->value instanceof ArrowFunction) {
94+
return null;
95+
}
96+
97+
$keyVar = $args[1]->value->params[1]->var ?? new Variable($this->variableNaming->createCountedValueName(
98+
'idx',
99+
ScopeFetcher::fetch($node)
100+
));
101+
102+
$valueCond = $args[1]->value->expr;
103+
$if = new If_($valueCond, [
104+
'stmts' => [new Expression(new Assign($node->expr->var, $keyVar)), new Break_()],
105+
]);
106+
107+
return [
108+
// init
109+
new Expression(new Assign($node->expr->var, new ConstFetch(new Name('null')))),
110+
111+
// foreach loop
112+
new Foreach_(
113+
$args[0]->value,
114+
$args[1]->value->params[0]->var,
115+
[
116+
'keyVar' => $keyVar,
117+
'stmts' => [$if],
118+
]
119+
),
120+
];
121+
}
122+
}

0 commit comments

Comments
 (0)