forked from rectorphp/rector-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhileEachToForeachRector.php
More file actions
141 lines (119 loc) · 3.71 KB
/
WhileEachToForeachRector.php
File metadata and controls
141 lines (119 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
declare(strict_types=1);
namespace Rector\Php72\Rector\While_;
use PhpParser\Node;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\While_;
use Rector\NodeManipulator\AssignManipulator;
use Rector\Php72\ValueObject\ListAndEach;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\DeprecatedAtVersionInterface;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Php72\Rector\While_\WhileEachToForeachRector\WhileEachToForeachRectorTest
*/
final class WhileEachToForeachRector extends AbstractRector implements MinPhpVersionInterface, DeprecatedAtVersionInterface
{
public function __construct(
private readonly AssignManipulator $assignManipulator
) {
}
public function provideMinPhpVersion(): int
{
return PhpVersionFeature::BASELINE_SUPPORTED_PHP;
}
public function provideDeprecatedAtVersion(): int
{
return PhpVersionFeature::DEPRECATE_EACH;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Use `foreach()` instead of deprecated `each()`',
[
new CodeSample(
<<<'CODE_SAMPLE'
while (list($key, $callback) = each($callbacks)) {
// ...
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
foreach ($callbacks as $key => $callback) {
// ...
}
CODE_SAMPLE
),
new CodeSample(
<<<'CODE_SAMPLE'
while (list($key) = each($callbacks)) {
// ...
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
foreach (array_keys($callbacks) as $key) {
// ...
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [While_::class];
}
/**
* @param While_ $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->cond instanceof Assign) {
return null;
}
$listAndEach = $this->assignManipulator->matchListAndEach($node->cond);
if (! $listAndEach instanceof ListAndEach) {
return null;
}
$eachFuncCall = $listAndEach->getEachFuncCall();
$list = $listAndEach->getList();
if (! isset($eachFuncCall->getArgs()[0])) {
return null;
}
$firstArg = $eachFuncCall->getArgs()[0];
$foreachedExpr = count($list->items) === 1 ? $this->nodeFactory->createFuncCall(
'array_keys',
[$firstArg]
) : $firstArg->value;
$arrayItem = array_pop($list->items);
$isTrailingCommaLast = false;
if (! $arrayItem instanceof ArrayItem) {
$foreachedExpr = $this->nodeFactory->createFuncCall('array_keys', [$eachFuncCall->args[0]]);
/** @var ArrayItem $arrayItem */
$arrayItem = current($list->items);
$isTrailingCommaLast = true;
}
$foreach = new Foreach_($foreachedExpr, $arrayItem->value, [
'stmts' => $node->stmts,
]);
$this->mirrorComments($foreach, $node);
// is key included? add it to foreach
if ($list->items !== []) {
/** @var ArrayItem|null $keyItem */
$keyItem = array_pop($list->items);
if ($keyItem instanceof ArrayItem && ! $isTrailingCommaLast) {
$foreach->keyVar = $keyItem->value;
}
}
return $foreach;
}
}