-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathDowngradeArrayKeyFirstLastRector.php
More file actions
262 lines (218 loc) · 7.51 KB
/
DowngradeArrayKeyFirstLastRector.php
File metadata and controls
262 lines (218 loc) · 7.51 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
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp73\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Cast\Array_;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Echo_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
use PHPStan\Analyser\Scope;
use Rector\Naming\Naming\VariableNaming;
use Rector\NodeAnalyzer\ExprInTopStmtMatcher;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Enum\NodeGroup;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://wiki.php.net/rfc/array_key_first_last
*
* @see \Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector\DowngradeArrayKeyFirstLastRectorTest
*/
final class DowngradeArrayKeyFirstLastRector extends AbstractRector
{
public function __construct(
private readonly VariableNaming $variableNaming,
private readonly ExprInTopStmtMatcher $exprInTopStmtMatcher
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Downgrade array_key_first() and array_key_last() functions', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($items)
{
$firstItemKey = array_key_first($items);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($items)
{
reset($items);
$firstItemKey = key($items);
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [...NodeGroup::STMTS_AWARE, Switch_::class, Return_::class, Expression::class, Echo_::class];
}
/**
* @param StmtsAware|Switch_|Return_|Expression|Echo_ $node
* @return Node[]|null
*/
public function refactor(Node $node): ?array
{
$exprArrayKeyFirst = $this->exprInTopStmtMatcher->match(
$node,
function (Node $subNode): bool {
if (! $subNode instanceof FuncCall) {
return false;
}
return $this->isName($subNode, 'array_key_first');
}
);
if ($exprArrayKeyFirst instanceof FuncCall) {
return $this->refactorArrayKeyFirst($exprArrayKeyFirst, $node);
}
$exprArrayKeyLast = $this->exprInTopStmtMatcher->match(
$node,
function (Node $subNode): bool {
if (! $subNode instanceof FuncCall) {
return false;
}
return $this->isName($subNode, 'array_key_last');
}
);
if ($exprArrayKeyLast instanceof FuncCall) {
return $this->refactorArrayKeyLast($exprArrayKeyLast, $node);
}
return null;
}
private function resolveVariableFromCallLikeScope(CallLike $callLike, ?Scope $scope): Variable
{
/** @var MethodCall|FuncCall|StaticCall|New_|NullsafeMethodCall $callLike */
if ($callLike instanceof New_) {
$variableName = (string) $this->getName($callLike->class);
} else {
$variableName = (string) $this->getName($callLike->name);
}
if ($variableName === '') {
$variableName = 'array';
}
return new Variable($this->variableNaming->createCountedValueName($variableName, $scope));
}
/**
* @return Node[]|null
*/
private function refactorArrayKeyFirst(
FuncCall $funcCall,
Node|Switch_|Return_|Expression|Echo_ $stmt
): null|array {
$args = $funcCall->getArgs();
if (! isset($args[0])) {
return null;
}
$originalArray = $args[0]->value;
$array = $this->resolveCastedArray($originalArray);
$newStmts = [];
if ($originalArray instanceof CallLike) {
$scope = $originalArray->getAttribute(AttributeKey::SCOPE);
$array = $this->resolveVariableFromCallLikeScope($originalArray, $scope);
}
if ($originalArray !== $array) {
$newStmts[] = new Expression(new Assign($array, $originalArray));
}
$resetFuncCall = $this->nodeFactory->createFuncCall('reset', [$array]);
$newStmts[] = $this->resolvePrependNewStmt($array, $resetFuncCall, $stmt);
$funcCall->name = new Name('key');
if ($originalArray !== $array) {
$firstArg = $args[0];
$firstArg->value = $array;
}
$newStmts[] = $stmt;
return $newStmts;
}
/**
* @return Node[]|null
*/
private function refactorArrayKeyLast(
FuncCall $funcCall,
Node|Switch_|Return_|Expression|Echo_ $stmt
): null|array {
$args = $funcCall->getArgs();
$firstArg = $args[0] ?? null;
if (! $firstArg instanceof Arg) {
return null;
}
$originalArray = $firstArg->value;
$array = $this->resolveCastedArray($originalArray);
$newStmts = [];
if ($originalArray instanceof CallLike) {
$scope = $originalArray->getAttribute(AttributeKey::SCOPE);
$array = $this->resolveVariableFromCallLikeScope($originalArray, $scope);
}
if ($originalArray !== $array) {
$newStmts[] = new Expression(new Assign($array, $originalArray));
}
$endFuncCall = $this->nodeFactory->createFuncCall('end', [$array]);
$newStmts[] = $this->resolvePrependNewStmt($array, $endFuncCall, $stmt);
$funcCall->name = new Name('key');
if ($originalArray !== $array) {
$firstArg->value = $array;
}
$newStmts[] = $stmt;
$resetExpression = new Expression($this->nodeFactory->createFuncCall('reset', [$array]));
if (property_exists($stmt, 'stmts')) {
$stmt->stmts = array_merge([$resetExpression], $stmt->stmts);
} elseif (! $stmt instanceof Return_) {
$newStmts[] = $resetExpression;
}
return $newStmts;
}
private function resolvePrependNewStmt(
Expr|Variable $array,
FuncCall $funcCall,
Stmt|Node $stmt
): Expression|If_ {
if (! $stmt instanceof If_ || $stmt->cond instanceof FuncCall || ! $stmt->cond instanceof BooleanOr) {
return new Expression($funcCall);
}
$if = new If_($this->nodeFactory->createFuncCall('is_array', [$array]));
$if->stmts[] = new Expression($funcCall);
return $if;
}
private function resolveCastedArray(Expr $expr): Expr|Variable
{
if (! $expr instanceof Array_) {
return $expr;
}
if ($expr->expr instanceof Array_) {
return $this->resolveCastedArray($expr->expr);
}
$scope = $expr->getAttribute(AttributeKey::SCOPE);
$variableName = $this->variableNaming->createCountedValueName(
(string) $this->getName($expr->expr),
$scope
);
return new Variable($variableName);
}
}