Skip to content

Commit 4b109d2

Browse files
committed
Clean up visitor types
1 parent 8b9e8bd commit 4b109d2

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

docs/class-reference.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,10 +1269,10 @@ a new version of the AST with the changes applied will be returned from the
12691269
visit function.
12701270

12711271
$editedAST = Visitor::visit($ast, [
1272-
'enter' => function ($node, $key, $parent, $path, $ancestors) {
1272+
'enter' => function (Node $node, $key, $parent, array $path, array $ancestors) {
12731273
// ...
12741274
},
1275-
'leave' => function ($node, $key, $parent, $path, $ancestors) {
1275+
'leave' => function (Node $node, $key, $parent, array $path, array $ancestors) {
12761276
// ...
12771277
}
12781278
]);
@@ -1285,21 +1285,21 @@ visitor API:
12851285
1. Named visitors triggered when entering a node a specific kind.
12861286

12871287
Visitor::visit($ast, [
1288-
'Kind' => function ($node) {
1289-
// enter the "Kind" node
1288+
NodeKind::OBJECT_TYPE_DEFINITION => function (ObjectTypeDefinitionNode $node) {
1289+
// enter the "ObjectTypeDefinition" node
12901290
}
12911291
]);
12921292

12931293
2. Named visitors that trigger upon entering and leaving a node of
12941294
a specific kind.
12951295

12961296
Visitor::visit($ast, [
1297-
'Kind' => [
1298-
'enter' => function ($node) {
1299-
// enter the "Kind" node
1297+
NodeKind::OBJECT_TYPE_DEFINITION => [
1298+
'enter' => function (ObjectTypeDefinitionNode $node) {
1299+
// enter the "ObjectTypeDefinition" node
13001300
}
1301-
'leave' => function ($node) {
1302-
// leave the "Kind" node
1301+
'leave' => function (ObjectTypeDefinitionNode $node) {
1302+
// leave the "ObjectTypeDefinition" node
13031303
}
13041304
]
13051305
]);
@@ -1318,14 +1318,14 @@ visitor API:
13181318
4. Parallel visitors for entering and leaving nodes of a specific kind.
13191319

13201320
Visitor::visit($ast, [
1321-
'enter' => [
1322-
'Kind' => function($node) {
1323-
// enter the "Kind" node
1321+
'enter' => [
1322+
NodeKind::OBJECT_TYPE_DEFINITION => function (ObjectTypeDefinitionNode $node) {
1323+
// enter the "ObjectTypeDefinition" node
13241324
}
13251325
},
13261326
'leave' => [
1327-
'Kind' => function ($node) {
1328-
// leave the "Kind" node
1327+
NodeKind::OBJECT_TYPE_DEFINITION => function (ObjectTypeDefinitionNode $node) {
1328+
// leave the "ObjectTypeDefinition" node
13291329
}
13301330
]
13311331
]);

src/Language/Visitor.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
* visit function.
2929
*
3030
* $editedAST = Visitor::visit($ast, [
31-
* 'enter' => function ($node, $key, $parent, $path, $ancestors) {
31+
* 'enter' => function (Node $node, $key, $parent, array $path, array $ancestors) {
3232
* // ...
3333
* },
34-
* 'leave' => function ($node, $key, $parent, $path, $ancestors) {
34+
* 'leave' => function (Node $node, $key, $parent, array $path, array $ancestors) {
3535
* // ...
3636
* }
3737
* ]);
@@ -44,21 +44,21 @@
4444
* 1. Named visitors triggered when entering a node a specific kind.
4545
*
4646
* Visitor::visit($ast, [
47-
* 'Kind' => function ($node) {
48-
* // enter the "Kind" node
47+
* NodeKind::OBJECT_TYPE_DEFINITION => function (ObjectTypeDefinitionNode $node) {
48+
* // enter the "ObjectTypeDefinition" node
4949
* }
5050
* ]);
5151
*
5252
* 2. Named visitors that trigger upon entering and leaving a node of
5353
* a specific kind.
5454
*
5555
* Visitor::visit($ast, [
56-
* 'Kind' => [
57-
* 'enter' => function ($node) {
58-
* // enter the "Kind" node
56+
* NodeKind::OBJECT_TYPE_DEFINITION => [
57+
* 'enter' => function (ObjectTypeDefinitionNode $node) {
58+
* // enter the "ObjectTypeDefinition" node
5959
* }
60-
* 'leave' => function ($node) {
61-
* // leave the "Kind" node
60+
* 'leave' => function (ObjectTypeDefinitionNode $node) {
61+
* // leave the "ObjectTypeDefinition" node
6262
* }
6363
* ]
6464
* ]);
@@ -78,13 +78,13 @@
7878
*
7979
* Visitor::visit($ast, [
8080
* 'enter' => [
81-
* 'Kind' => function($node) {
82-
* // enter the "Kind" node
81+
* NodeKind::OBJECT_TYPE_DEFINITION => function (ObjectTypeDefinitionNode $node) {
82+
* // enter the "ObjectTypeDefinition" node
8383
* }
8484
* },
8585
* 'leave' => [
86-
* 'Kind' => function ($node) {
87-
* // leave the "Kind" node
86+
* NodeKind::OBJECT_TYPE_DEFINITION => function (ObjectTypeDefinitionNode $node) {
87+
* // leave the "ObjectTypeDefinition" node
8888
* }
8989
* ]
9090
* ]);
@@ -478,7 +478,7 @@ public static function visitWithTypeInfo(TypeInfo $typeInfo, array $visitor): ar
478478
/**
479479
* @phpstan-param VisitorArray $visitor
480480
*
481-
* @return (callable(Node $node, string $key, Node|NodeList<Node>|null $parent, array<int, int|string> $path, array<int, Node|NodeList<Node>> $ancestors): (VisitorOperation|Node|null))|(callable(Node): (VisitorOperation|void|false|null))|null
481+
* @return (callable(Node $node, string|int|null $key, Node|NodeList<Node>|null $parent, array<int, int|string> $path, array<int, Node|NodeList<Node>> $ancestors): (VisitorOperation|Node|null))|(callable(Node): (VisitorOperation|void|false|null))|null
482482
*/
483483
protected static function extractVisitFn(array $visitor, string $kind, bool $isLeaving): ?callable
484484
{

tests/Language/VisitorTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ public function testValidatesPathArgument(): void
115115
Visitor::visit(
116116
$ast,
117117
[
118-
'enter' => function ($node, $key, $parent, $path) use ($ast, &$visited): void {
118+
'enter' => function (Node $node, $key, $parent, array $path) use ($ast, &$visited): void {
119119
$this->checkVisitorFnArgs($ast, func_get_args());
120120
$visited[] = ['enter', $path];
121121
},
122-
'leave' => function ($node, $key, $parent, $path) use ($ast, &$visited): void {
122+
'leave' => function (Node $node, $key, $parent, array $path) use ($ast, &$visited): void {
123123
$this->checkVisitorFnArgs($ast, func_get_args());
124124
$visited[] = ['leave', $path];
125125
},
@@ -149,7 +149,7 @@ public function testValidatesAncestorsArgument(): void
149149
$visitedNodes = [];
150150

151151
Visitor::visit($ast, [
152-
'enter' => static function ($node, $key, $parent, $path, $ancestors) use (&$visitedNodes): void {
152+
'enter' => static function (Node $node, $key, $parent, array $path, array $ancestors) use (&$visitedNodes): void {
153153
$inArray = is_numeric($key);
154154
if ($inArray) {
155155
$visitedNodes[] = $parent;
@@ -160,7 +160,7 @@ public function testValidatesAncestorsArgument(): void
160160
$expectedAncestors = array_slice($visitedNodes, 0, -2);
161161
self::assertEquals($expectedAncestors, $ancestors);
162162
},
163-
'leave' => static function ($node, $key, $parent, $path, $ancestors) use (&$visitedNodes): void {
163+
'leave' => static function (Node $node, $key, $parent, array $path, array $ancestors) use (&$visitedNodes): void {
164164
$expectedAncestors = array_slice($visitedNodes, 0, -2);
165165
self::assertEquals($expectedAncestors, $ancestors);
166166

@@ -363,7 +363,7 @@ public function testAllowsSkippingASubTree(): void
363363
Visitor::visit(
364364
$ast,
365365
[
366-
'enter' => function (Node $node) use (&$visited, $ast): ?VisitorOperation {
366+
'enter' => function ($node) use (&$visited, $ast): ?VisitorOperation {
367367
$this->checkVisitorFnArgs($ast, func_get_args());
368368
$visited[] = ['enter', $node->kind, property_exists($node, 'value') ? $node->value : null];
369369
if ($node instanceof FieldNode && $node->name->value === 'b') {
@@ -372,7 +372,7 @@ public function testAllowsSkippingASubTree(): void
372372

373373
return null;
374374
},
375-
'leave' => function (Node $node) use (&$visited, $ast): void {
375+
'leave' => function ($node) use (&$visited, $ast): void {
376376
$this->checkVisitorFnArgs($ast, func_get_args());
377377
$visited[] = ['leave', $node->kind, property_exists($node, 'value') ? $node->value : null];
378378
},
@@ -408,7 +408,7 @@ public function testAllowsEarlyExitWhileVisiting(): void
408408
Visitor::visit(
409409
$ast,
410410
[
411-
'enter' => function (Node $node) use (&$visited, $ast): ?VisitorOperation {
411+
'enter' => function ($node) use (&$visited, $ast): ?VisitorOperation {
412412
$this->checkVisitorFnArgs($ast, func_get_args());
413413
$visited[] = ['enter', $node->kind, property_exists($node, 'value') ? $node->value : null];
414414
if ($node instanceof NameNode && $node->value === 'x') {
@@ -417,7 +417,7 @@ public function testAllowsEarlyExitWhileVisiting(): void
417417

418418
return null;
419419
},
420-
'leave' => function (Node $node) use (&$visited, $ast): void {
420+
'leave' => function ($node) use (&$visited, $ast): void {
421421
$this->checkVisitorFnArgs($ast, func_get_args());
422422
$visited[] = ['leave', $node->kind, property_exists($node, 'value') ? $node->value : null];
423423
},

0 commit comments

Comments
 (0)