|
6 | 6 | use GraphQL\Language\AST\NodeKind;
|
7 | 7 | use GraphQL\Language\AST\NodeList;
|
8 | 8 | use GraphQL\Utils\TypeInfo;
|
| 9 | +use GraphQL\Utils\Utils; |
9 | 10 |
|
10 | 11 | /**
|
11 | 12 | * Utility for efficient AST traversal and modification.
|
|
88 | 89 | * ]
|
89 | 90 | * ]);
|
90 | 91 | *
|
91 |
| - * @phpstan-type VisitorArray array<string, callable(Node): VisitorOperation|mixed|null>|array<string, array<string, callable(Node): VisitorOperation|mixed|null>> |
| 92 | + * @phpstan-type NodeVisitor callable(Node): (VisitorOperation|null|false|void) |
| 93 | + * @phpstan-type VisitorArray array<string, NodeVisitor>|array<string, array<string, NodeVisitor>> |
92 | 94 | */
|
93 | 95 | class Visitor
|
94 | 96 | {
|
@@ -218,6 +220,11 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null
|
218 | 220 | ++$editOffset;
|
219 | 221 | } else {
|
220 | 222 | if ($node instanceof NodeList || \is_array($node)) {
|
| 223 | + if (! $editValue instanceof Node) { |
| 224 | + $notNode = Utils::printSafe($editValue); |
| 225 | + throw new \Exception("Can only add Node to NodeList, got: {$notNode}."); |
| 226 | + } |
| 227 | + |
221 | 228 | $node[$editKey] = $editValue;
|
222 | 229 | } else {
|
223 | 230 | $node->{$editKey} = $editValue;
|
@@ -267,26 +274,23 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null
|
267 | 274 |
|
268 | 275 | if ($visitFn !== null) {
|
269 | 276 | $result = $visitFn($node, $key, $parent, $path, $ancestors);
|
270 |
| - $editValue = null; |
271 | 277 |
|
272 | 278 | if ($result !== null) {
|
273 |
| - if ($result instanceof VisitorOperation) { |
274 |
| - if ($result instanceof VisitorStop) { |
275 |
| - break; |
276 |
| - } |
| 279 | + if ($result instanceof VisitorStop) { |
| 280 | + break; |
| 281 | + } |
277 | 282 |
|
278 |
| - if (! $isLeaving && $result instanceof VisitorSkipNode) { |
| 283 | + if ($result instanceof VisitorSkipNode) { |
| 284 | + if (! $isLeaving) { |
279 | 285 | \array_pop($path);
|
280 |
| - continue; |
281 | 286 | }
|
282 |
| - |
283 |
| - if ($result instanceof VisitorRemoveNode) { |
284 |
| - $editValue = null; |
285 |
| - } |
286 |
| - } else { |
287 |
| - $editValue = $result; |
| 287 | + continue; |
288 | 288 | }
|
289 | 289 |
|
| 290 | + $editValue = $result instanceof VisitorRemoveNode |
| 291 | + ? null |
| 292 | + : $result; |
| 293 | + |
290 | 294 | $edits[] = [$key, $editValue];
|
291 | 295 | if (! $isLeaving) {
|
292 | 296 | if (! ($editValue instanceof Node)) {
|
|
0 commit comments