Repository navigation
Expand file tree
/
Copy pathContextNodeVisitor.php
More file actions
371 lines (307 loc) · 11.4 KB
/
Copy pathContextNodeVisitor.php
File metadata and controls
371 lines (307 loc) · 11.4 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
declare(strict_types=1);
namespace Rector\PhpParser\NodeVisitor;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Attribute;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PostDec;
use PhpParser\Node\Expr\PostInc;
use PhpParser\Node\Expr\PreDec;
use PhpParser\Node\Expr\PreInc;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Do_;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\Finally_;
use PhpParser\Node\Stmt\For_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\TryCatch;
use PhpParser\Node\Stmt\Unset_;
use PhpParser\Node\Stmt\While_;
use PhpParser\Node\UnionType;
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class ContextNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
{
/**
* Context is tracked by depth during the main traversal, instead of traversing
* the loop, try and attribute children again on every enter
*/
private int $loopDepth = 0;
private int $tryDepth = 0;
private int $attributeDepth = 0;
/**
* @var array<array{int, int}>
*/
private array $outerLoopAndTryDepths = [];
/**
* Whether the enclosing function-like declares a native return type not accepting a Closure
*
* @var bool[]
*/
private array $returnsArrayStack = [];
/**
* @param Node[] $nodes
*/
public function beforeTraverse(array $nodes): null
{
$this->loopDepth = 0;
$this->tryDepth = 0;
$this->attributeDepth = 0;
$this->outerLoopAndTryDepths = [];
$this->returnsArrayStack = [];
return null;
}
public function enterNode(Node $node): ?Node
{
$this->decorateEnclosingContext($node);
if ($node instanceof For_ || $node instanceof Foreach_ || $node instanceof While_ || $node instanceof Do_ || $node instanceof Switch_) {
$this->processContextInLoop($node);
++$this->loopDepth;
return null;
}
if ($node instanceof FunctionLike) {
$this->processReturnTypeArray($node);
}
if ($node instanceof Return_ && $node->expr instanceof Array_ && end($this->returnsArrayStack)) {
$node->expr->setAttribute(AttributeKey::IS_RETURN_NOT_ACCEPTING_CLOSURE, true);
return null;
}
if ($node instanceof ArrayDimFetch) {
$this->processInsideArrayDimFetch($node);
return null;
}
if ($node instanceof Unset_) {
foreach ($node->vars as $var) {
$var->setAttribute(AttributeKey::IS_UNSET_VAR, true);
}
return null;
}
if ($node instanceof TryCatch) {
++$this->tryDepth;
return null;
}
if ($node instanceof Isset_) {
foreach ($node->vars as $var) {
$var->setAttribute(AttributeKey::IS_ISSET_VAR, true);
}
return null;
}
if ($node instanceof Attribute) {
++$this->attributeDepth;
return null;
}
if ($node instanceof Array_) {
$this->processArrayInSerializedCallableKey($node);
return null;
}
if ($node instanceof MethodCall) {
$this->processSetFactoryArgument($node);
return null;
}
if ($node instanceof If_ || $node instanceof Else_ || $node instanceof ElseIf_) {
$this->processContextInIf($node);
return null;
}
if ($node instanceof Arg) {
$node->value->setAttribute(AttributeKey::IS_ARG_VALUE, true);
if ($node->unpack) {
$node->value->setAttribute(AttributeKey::IS_UNPACKED_ARG_VALUE, true);
}
return null;
}
if ($node instanceof Param) {
$node->var->setAttribute(AttributeKey::IS_PARAM_VAR, true);
return null;
}
if ($node instanceof PostDec || $node instanceof PostInc || $node instanceof PreDec || $node instanceof PreInc) {
$node->var->setAttribute(AttributeKey::IS_INCREMENT_OR_DECREMENT, true);
return null;
}
if ($node instanceof BooleanAnd) {
$node->right->setAttribute(AttributeKey::IS_RIGHT_AND, true);
return null;
}
$this->processContextInClass($node);
return null;
}
public function leaveNode(Node $node): null
{
if ($node instanceof For_ || $node instanceof Foreach_ || $node instanceof While_ || $node instanceof Do_ || $node instanceof Switch_) {
--$this->loopDepth;
return null;
}
if ($node instanceof TryCatch) {
--$this->tryDepth;
return null;
}
// back to the try stmts of the parent TryCatch, see decorateEnclosingContext()
if ($node instanceof Catch_ || $node instanceof Finally_) {
++$this->tryDepth;
return null;
}
if ($node instanceof Attribute) {
--$this->attributeDepth;
return null;
}
if ($node instanceof FunctionLike) {
array_pop($this->returnsArrayStack);
}
if ($node instanceof Class_ || $node instanceof FunctionLike) {
[$this->loopDepth, $this->tryDepth] = array_pop($this->outerLoopAndTryDepths) ?? [0, 0];
}
return null;
}
private function decorateEnclosingContext(Node $node): void
{
// catch and finally are not part of the try block of their parent TryCatch
if ($node instanceof Catch_ || $node instanceof Finally_) {
--$this->tryDepth;
}
// nested class or function-like does not belong to outer loop or try block
if ($node instanceof Class_ || $node instanceof FunctionLike) {
$this->outerLoopAndTryDepths[] = [$this->loopDepth, $this->tryDepth];
$this->loopDepth = 0;
$this->tryDepth = 0;
} elseif ($this->tryDepth > 0) {
$node->setAttribute(AttributeKey::IS_IN_TRY_BLOCK, true);
}
if ($this->loopDepth > 0 && ($node instanceof If_ || $node instanceof Break_)) {
$node->setAttribute(AttributeKey::IS_IN_LOOP_OR_SWITCH, true);
}
if ($this->attributeDepth > 0) {
if ($node instanceof Array_) {
$node->setAttribute(AttributeKey::IS_ARRAY_IN_ATTRIBUTE, true);
}
if ($node instanceof Closure) {
$node->setAttribute(AttributeKey::IS_CLOSURE_IN_ATTRIBUTE, true);
}
}
}
private function processReturnTypeArray(FunctionLike $functionLike): void
{
$returnsArray = $this->isArrayNotAcceptingClosureType($functionLike->getReturnType());
$this->returnsArrayStack[] = $returnsArray;
if ($returnsArray && $functionLike instanceof ArrowFunction && $functionLike->expr instanceof Array_) {
$functionLike->expr->setAttribute(AttributeKey::IS_RETURN_NOT_ACCEPTING_CLOSURE, true);
}
}
private function isArrayNotAcceptingClosureType(Identifier|Name|ComplexType|null $type): bool
{
if ($type instanceof NullableType) {
$type = $type->type;
}
$types = $type instanceof UnionType ? $type->types : [$type];
$typeNames = [];
foreach ($types as $singleType) {
if (! $singleType instanceof Identifier && ! $singleType instanceof Name) {
return false;
}
$typeNames[] = strtolower($singleType->toString());
}
if (array_intersect($typeNames, ['callable', 'closure', 'mixed', 'object']) !== []) {
return false;
}
return array_intersect($typeNames, ['array', 'iterable']) !== [];
}
private function processInsideArrayDimFetch(ArrayDimFetch $arrayDimFetch): void
{
if ($arrayDimFetch->var instanceof PropertyFetch || $arrayDimFetch->var instanceof StaticPropertyFetch) {
$arrayDimFetch->var->setAttribute(AttributeKey::INSIDE_ARRAY_DIM_FETCH, true);
}
}
private function processContextInClass(Node $node): void
{
if ($node instanceof Class_) {
if ($node->extends instanceof FullyQualified) {
$node->extends->setAttribute(AttributeKey::IS_CLASS_EXTENDS, true);
}
foreach ($node->implements as $implement) {
$implement->setAttribute(AttributeKey::IS_CLASS_IMPLEMENT, true);
}
}
}
/**
* The array is data, not a callable to convert: 'callback'/'factory' keyed item holds
* a [$value, 'method'] pair likely to be serialized as a string.
*/
private function processArrayInSerializedCallableKey(Array_ $array): void
{
foreach ($array->items as $arrayItem) {
if (! $arrayItem instanceof ArrayItem) {
continue;
}
if (! $arrayItem->key instanceof String_) {
continue;
}
if (! in_array($arrayItem->key->value, ['callback', 'factory', 'validation_groups'], true)) {
continue;
}
if ($arrayItem->value instanceof Array_) {
$arrayItem->value->setAttribute(AttributeKey::IS_ARRAY_AS_STRING_CALLABLE, true);
}
}
}
/**
* Symfony's Definition::setFactory() does not accept a first class callable,
* keep the [$value, 'method'] array as is.
*/
private function processSetFactoryArgument(MethodCall $methodCall): void
{
if (! $methodCall->name instanceof Identifier) {
return;
}
if ($methodCall->name->toString() !== 'setFactory') {
return;
}
$firstArg = $methodCall->args[0] ?? null;
if (! $firstArg instanceof Arg) {
return;
}
if ($firstArg->value instanceof Array_) {
$firstArg->value->setAttribute(AttributeKey::IS_ARRAY_AS_STRING_CALLABLE, true);
}
}
private function processContextInIf(If_|Else_|ElseIf_ $node): void
{
foreach ($node->stmts as $stmt) {
if ($stmt instanceof Break_) {
$stmt->setAttribute(AttributeKey::IS_IN_IF, true);
}
}
}
private function processContextInLoop(For_|Foreach_|While_|Do_|Switch_ $node): void
{
if ($node instanceof Foreach_) {
if ($node->keyVar instanceof Variable) {
$node->keyVar->setAttribute(AttributeKey::IS_VARIABLE_LOOP, true);
}
$node->valueVar->setAttribute(AttributeKey::IS_VARIABLE_LOOP, true);
}
}
}