-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathArrayKeyFirstLastRector.php
More file actions
286 lines (238 loc) · 8.03 KB
/
ArrayKeyFirstLastRector.php
File metadata and controls
286 lines (238 loc) · 8.03 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
declare(strict_types=1);
namespace Rector\Php73\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Reflection\ReflectionProvider;
use Rector\PhpParser\Enum\NodeGroup;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\ValueObject\PolyfillPackage;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Rector\VersionBonding\Contract\RelatedPolyfillInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* This needs to removed 1 floor above, because only nodes in arrays can be removed why traversing,
* see https://github.com/nikic/PHP-Parser/issues/389
*
* @see \Rector\Tests\Php73\Rector\FuncCall\ArrayKeyFirstLastRector\ArrayKeyFirstLastRectorTest
*/
final class ArrayKeyFirstLastRector extends AbstractRector implements MinPhpVersionInterface, RelatedPolyfillInterface
{
private const string ARRAY_KEY_FIRST = 'array_key_first';
private const string ARRAY_KEY_LAST = 'array_key_last';
/**
* @var array<string, string>
*/
private const array PREVIOUS_TO_NEW_FUNCTIONS = [
'reset' => self::ARRAY_KEY_FIRST,
'end' => self::ARRAY_KEY_LAST,
];
public function __construct(
private readonly ReflectionProvider $reflectionProvider,
private readonly BetterNodeFinder $betterNodeFinder
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Make use of array_key_first() and array_key_last()',
[
new CodeSample(
<<<'CODE_SAMPLE'
reset($items);
$firstKey = key($items);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$firstKey = array_key_first($items);
CODE_SAMPLE
),
new CodeSample(
<<<'CODE_SAMPLE'
end($items);
$lastKey = key($items);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$lastKey = array_key_last($items);
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return NodeGroup::STMTS_AWARE;
}
/**
* @param StmtsAware $node
* @return ?StmtsAware
*/
public function refactor(Node $node): ?Node
{
return $this->processArrayKeyFirstLast($node);
}
public function provideMinPhpVersion(): int
{
return PhpVersionFeature::ARRAY_KEY_FIRST_LAST;
}
public function providePolyfillPackage(): string
{
return PolyfillPackage::PHP_73;
}
/**
* @param StmtsAware $stmtsAware
* @return StmtsAware|null
*/
private function processArrayKeyFirstLast(Node $stmtsAware, int $jumpToKey = 0): ?Node
{
if ($stmtsAware->stmts === null) {
return null;
}
/** @var int $totalKeys */
$totalKeys = array_key_last($stmtsAware->stmts);
for ($key = $jumpToKey; $key < $totalKeys; ++$key) {
if (! isset($stmtsAware->stmts[$key], $stmtsAware->stmts[$key + 1])) {
break;
}
if (! $stmtsAware->stmts[$key] instanceof Expression) {
continue;
}
$stmt = $stmtsAware->stmts[$key];
if ($this->shouldSkip($stmt)) {
continue;
}
$nextStmt = $stmtsAware->stmts[$key + 1];
/** @var FuncCall $resetOrEndFuncCall */
$resetOrEndFuncCall = $stmt->expr;
$keyFuncCall = $this->resolveKeyFuncCall($nextStmt, $resetOrEndFuncCall);
if (! $keyFuncCall instanceof FuncCall) {
continue;
}
if ($this->hasInternalPointerChangeNext($stmtsAware, $key + 1, $totalKeys, $keyFuncCall)) {
continue;
}
if (! isset(self::PREVIOUS_TO_NEW_FUNCTIONS[$this->getName($stmt->expr)])) {
continue;
}
$newName = self::PREVIOUS_TO_NEW_FUNCTIONS[$this->getName($stmt->expr)];
$keyFuncCall->name = new Name($newName);
$this->changeNextKeyCall($stmtsAware, $key + 2, $resetOrEndFuncCall, $keyFuncCall->name);
unset($stmtsAware->stmts[$key]);
return $stmtsAware;
}
return null;
}
/**
* @param StmtsAware $stmtsAware
*/
private function changeNextKeyCall(
Node $stmtsAware,
int $key,
FuncCall $resetOrEndFuncCall,
Name $newName
): void {
if ($stmtsAware->stmts === null) {
return;
}
$counter = count($stmtsAware->stmts);
for ($nextKey = $key; $nextKey < $counter; ++$nextKey) {
if (! isset($stmtsAware->stmts[$nextKey])) {
break;
}
if ($stmtsAware->stmts[$nextKey] instanceof Expression && ! $this->shouldSkip(
$stmtsAware->stmts[$nextKey]
)) {
$this->processArrayKeyFirstLast($stmtsAware, $nextKey);
break;
}
$keyFuncCall = $this->resolveKeyFuncCall($stmtsAware->stmts[$nextKey], $resetOrEndFuncCall);
if (! $keyFuncCall instanceof FuncCall) {
continue;
}
$keyFuncCall->name = $newName;
}
}
private function resolveKeyFuncCall(Stmt $nextStmt, FuncCall $resetOrEndFuncCall): ?FuncCall
{
if ($resetOrEndFuncCall->isFirstClassCallable()) {
return null;
}
/** @var FuncCall|null */
return $this->betterNodeFinder->findFirst($nextStmt, function (Node $subNode) use (
$resetOrEndFuncCall
): bool {
if (! $subNode instanceof FuncCall) {
return false;
}
if (! $this->isName($subNode, 'key')) {
return false;
}
if ($subNode->isFirstClassCallable()) {
return false;
}
return $this->nodeComparator->areNodesEqual($resetOrEndFuncCall->getArgs()[0], $subNode->getArgs()[0]);
});
}
/**
* @param StmtsAware $stmtsAware
*/
private function hasInternalPointerChangeNext(
Node $stmtsAware,
int $nextKey,
int $totalKeys,
FuncCall $funcCall
): bool {
for ($key = $nextKey; $key <= $totalKeys; ++$key) {
if (! isset($stmtsAware->stmts[$key])) {
continue;
}
$hasPrevCallNext = (bool) $this->betterNodeFinder->findFirst(
$stmtsAware->stmts[$key],
function (Node $subNode) use ($funcCall): bool {
if (! $subNode instanceof FuncCall) {
return false;
}
if (! $this->isNames($subNode, ['prev', 'next'])) {
return false;
}
if ($subNode->isFirstClassCallable()) {
return true;
}
return $this->nodeComparator->areNodesEqual(
$subNode->getArgs()[0]
->value,
$funcCall->getArgs()[0]
->value
);
}
);
if ($hasPrevCallNext) {
return true;
}
}
return false;
}
private function shouldSkip(Expression $expression): bool
{
if (! $expression->expr instanceof FuncCall) {
return true;
}
if (! $this->isNames($expression->expr, ['reset', 'end'])) {
return true;
}
if (! $this->reflectionProvider->hasFunction(new Name(self::ARRAY_KEY_FIRST), null)) {
return true;
}
return ! $this->reflectionProvider->hasFunction(new Name(self::ARRAY_KEY_LAST), null);
}
}