@@ -156,15 +156,11 @@ class Visitor
156
156
/**
157
157
* Visit the AST (see class description for details).
158
158
*
159
- * @template TNode of Node
160
- *
161
- * @param NodeList<TNode>|Node $root
159
+ * @param NodeList<Node>|Node $root
162
160
* @param VisitorArray $visitor
163
161
* @param array<string, mixed>|null $keyMap
164
162
*
165
- * @throws \Exception
166
- *
167
- * @return Node|mixed
163
+ * @return mixed
168
164
*
169
165
* @api
170
166
*/
@@ -187,7 +183,6 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null
187
183
$ parent = null ;
188
184
$ path = [];
189
185
$ ancestors = [];
190
- $ newRoot = $ root ;
191
186
192
187
do {
193
188
++$ index ;
@@ -240,20 +235,17 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null
240
235
] = \array_pop ($ stack );
241
236
} else {
242
237
if ($ parent === null ) {
243
- $ node = $ newRoot ;
238
+ $ node = $ root ;
244
239
} else {
245
240
$ key = $ inList
246
241
? $ index
247
242
: $ keys [$ index ];
248
243
$ node = $ parent instanceof NodeList
249
244
? $ parent [$ key ]
250
245
: $ parent ->{$ key };
251
- }
252
- if ($ node === null ) {
253
- continue ;
254
- }
255
-
256
- if ($ parent !== null ) {
246
+ if ($ node === null ) {
247
+ continue ;
248
+ }
257
249
$ path [] = $ key ;
258
250
}
259
251
}
@@ -264,7 +256,7 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null
264
256
throw new \Exception ('Invalid AST Node: ' . \json_encode ($ node ));
265
257
}
266
258
267
- $ visitFn = self ::getVisitFn ($ visitor , $ node ->kind , $ isLeaving );
259
+ $ visitFn = self ::extractVisitFn ($ visitor , $ node ->kind , $ isLeaving );
268
260
269
261
if ($ visitFn !== null ) {
270
262
$ result = $ visitFn ($ node , $ key , $ parent , $ path , $ ancestors );
@@ -324,11 +316,9 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null
324
316
}
325
317
} while (\count ($ stack ) > 0 );
326
318
327
- if (\count ($ edits ) > 0 ) {
328
- $ newRoot = $ edits [0 ][1 ];
329
- }
330
-
331
- return $ newRoot ;
319
+ return \count ($ edits ) > 0
320
+ ? $ edits [0 ][1 ]
321
+ : $ root ;
332
322
}
333
323
334
324
/**
@@ -368,6 +358,8 @@ public static function removeNode(): VisitorRemoveNode
368
358
}
369
359
370
360
/**
361
+ * Combines the given visitors to run in parallel.
362
+ *
371
363
* @phpstan-param array<int, VisitorArray> $visitors
372
364
*
373
365
* @return VisitorArray
@@ -384,7 +376,7 @@ public static function visitInParallel(array $visitors): array
384
376
continue ;
385
377
}
386
378
387
- $ fn = self ::getVisitFn (
379
+ $ fn = self ::extractVisitFn (
388
380
$ visitors [$ i ],
389
381
$ node ->kind ,
390
382
false
@@ -412,7 +404,7 @@ public static function visitInParallel(array $visitors): array
412
404
'leave ' => static function (Node $ node ) use ($ visitors , $ skipping , $ visitorsCount ) {
413
405
for ($ i = 0 ; $ i < $ visitorsCount ; ++$ i ) {
414
406
if ($ skipping [$ i ] === null ) {
415
- $ fn = self ::getVisitFn (
407
+ $ fn = self ::extractVisitFn (
416
408
$ visitors [$ i ],
417
409
$ node ->kind ,
418
410
true
@@ -440,8 +432,7 @@ public static function visitInParallel(array $visitors): array
440
432
}
441
433
442
434
/**
443
- * Creates a new visitor instance which maintains a provided TypeInfo instance
444
- * along with visiting visitor.
435
+ * Creates a new visitor that updates TypeInfo and delegates to the given visitor.
445
436
*
446
437
* @phpstan-param VisitorArray $visitor
447
438
*
@@ -452,7 +443,7 @@ public static function visitWithTypeInfo(TypeInfo $typeInfo, array $visitor): ar
452
443
return [
453
444
'enter ' => static function (Node $ node ) use ($ typeInfo , $ visitor ) {
454
445
$ typeInfo ->enter ($ node );
455
- $ fn = self ::getVisitFn ($ visitor , $ node ->kind , false );
446
+ $ fn = self ::extractVisitFn ($ visitor , $ node ->kind , false );
456
447
457
448
if ($ fn === null ) {
458
449
return null ;
@@ -471,7 +462,7 @@ public static function visitWithTypeInfo(TypeInfo $typeInfo, array $visitor): ar
471
462
return $ result ;
472
463
},
473
464
'leave ' => static function (Node $ node ) use ($ typeInfo , $ visitor ) {
474
- $ fn = self ::getVisitFn ($ visitor , $ node ->kind , true );
465
+ $ fn = self ::extractVisitFn ($ visitor , $ node ->kind , true );
475
466
$ result = $ fn !== null
476
467
? $ fn (...\func_get_args ())
477
468
: null ;
@@ -488,7 +479,7 @@ public static function visitWithTypeInfo(TypeInfo $typeInfo, array $visitor): ar
488
479
*
489
480
* @return callable(Node $node, string $key, Node|NodeList $parent, array<int, int|string $path, array<int, Node|NodeList> $ancestors): VisitorOperation|Node|null
490
481
*/
491
- public static function getVisitFn (array $ visitor , string $ kind , bool $ isLeaving ): ?callable
482
+ protected static function extractVisitFn (array $ visitor , string $ kind , bool $ isLeaving ): ?callable
492
483
{
493
484
$ kindVisitor = $ visitor [$ kind ] ?? null ;
494
485
0 commit comments