Skip to content

Commit 88f8856

Browse files
authored
Simplify visitor (#1324)
1 parent b9cfc95 commit 88f8856

File tree

2 files changed

+20
-33
lines changed

2 files changed

+20
-33
lines changed

docs/class-reference.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,15 +1117,11 @@ visitor API:
11171117
/**
11181118
* Visit the AST (see class description for details).
11191119
*
1120-
* @template TNode of Node
1121-
*
1122-
* @param NodeList<TNode>|Node $root
1120+
* @param NodeList<Node>|Node $root
11231121
* @param VisitorArray $visitor
11241122
* @param array<string, mixed>|null $keyMap
11251123
*
1126-
* @throws \Exception
1127-
*
1128-
* @return Node|mixed
1124+
* @return mixed
11291125
*
11301126
* @api
11311127
*/

src/Language/Visitor.php

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,11 @@ class Visitor
156156
/**
157157
* Visit the AST (see class description for details).
158158
*
159-
* @template TNode of Node
160-
*
161-
* @param NodeList<TNode>|Node $root
159+
* @param NodeList<Node>|Node $root
162160
* @param VisitorArray $visitor
163161
* @param array<string, mixed>|null $keyMap
164162
*
165-
* @throws \Exception
166-
*
167-
* @return Node|mixed
163+
* @return mixed
168164
*
169165
* @api
170166
*/
@@ -187,7 +183,6 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null
187183
$parent = null;
188184
$path = [];
189185
$ancestors = [];
190-
$newRoot = $root;
191186

192187
do {
193188
++$index;
@@ -240,20 +235,17 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null
240235
] = \array_pop($stack);
241236
} else {
242237
if ($parent === null) {
243-
$node = $newRoot;
238+
$node = $root;
244239
} else {
245240
$key = $inList
246241
? $index
247242
: $keys[$index];
248243
$node = $parent instanceof NodeList
249244
? $parent[$key]
250245
: $parent->{$key};
251-
}
252-
if ($node === null) {
253-
continue;
254-
}
255-
256-
if ($parent !== null) {
246+
if ($node === null) {
247+
continue;
248+
}
257249
$path[] = $key;
258250
}
259251
}
@@ -264,7 +256,7 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null
264256
throw new \Exception('Invalid AST Node: ' . \json_encode($node));
265257
}
266258

267-
$visitFn = self::getVisitFn($visitor, $node->kind, $isLeaving);
259+
$visitFn = self::extractVisitFn($visitor, $node->kind, $isLeaving);
268260

269261
if ($visitFn !== null) {
270262
$result = $visitFn($node, $key, $parent, $path, $ancestors);
@@ -324,11 +316,9 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null
324316
}
325317
} while (\count($stack) > 0);
326318

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;
332322
}
333323

334324
/**
@@ -368,6 +358,8 @@ public static function removeNode(): VisitorRemoveNode
368358
}
369359

370360
/**
361+
* Combines the given visitors to run in parallel.
362+
*
371363
* @phpstan-param array<int, VisitorArray> $visitors
372364
*
373365
* @return VisitorArray
@@ -384,7 +376,7 @@ public static function visitInParallel(array $visitors): array
384376
continue;
385377
}
386378

387-
$fn = self::getVisitFn(
379+
$fn = self::extractVisitFn(
388380
$visitors[$i],
389381
$node->kind,
390382
false
@@ -412,7 +404,7 @@ public static function visitInParallel(array $visitors): array
412404
'leave' => static function (Node $node) use ($visitors, $skipping, $visitorsCount) {
413405
for ($i = 0; $i < $visitorsCount; ++$i) {
414406
if ($skipping[$i] === null) {
415-
$fn = self::getVisitFn(
407+
$fn = self::extractVisitFn(
416408
$visitors[$i],
417409
$node->kind,
418410
true
@@ -440,8 +432,7 @@ public static function visitInParallel(array $visitors): array
440432
}
441433

442434
/**
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.
445436
*
446437
* @phpstan-param VisitorArray $visitor
447438
*
@@ -452,7 +443,7 @@ public static function visitWithTypeInfo(TypeInfo $typeInfo, array $visitor): ar
452443
return [
453444
'enter' => static function (Node $node) use ($typeInfo, $visitor) {
454445
$typeInfo->enter($node);
455-
$fn = self::getVisitFn($visitor, $node->kind, false);
446+
$fn = self::extractVisitFn($visitor, $node->kind, false);
456447

457448
if ($fn === null) {
458449
return null;
@@ -471,7 +462,7 @@ public static function visitWithTypeInfo(TypeInfo $typeInfo, array $visitor): ar
471462
return $result;
472463
},
473464
'leave' => static function (Node $node) use ($typeInfo, $visitor) {
474-
$fn = self::getVisitFn($visitor, $node->kind, true);
465+
$fn = self::extractVisitFn($visitor, $node->kind, true);
475466
$result = $fn !== null
476467
? $fn(...\func_get_args())
477468
: null;
@@ -488,7 +479,7 @@ public static function visitWithTypeInfo(TypeInfo $typeInfo, array $visitor): ar
488479
*
489480
* @return callable(Node $node, string $key, Node|NodeList $parent, array<int, int|string $path, array<int, Node|NodeList> $ancestors): VisitorOperation|Node|null
490481
*/
491-
public static function getVisitFn(array $visitor, string $kind, bool $isLeaving): ?callable
482+
protected static function extractVisitFn(array $visitor, string $kind, bool $isLeaving): ?callable
492483
{
493484
$kindVisitor = $visitor[$kind] ?? null;
494485

0 commit comments

Comments
 (0)