Skip to content

Commit ae54048

Browse files
committed
Make NodeVisitor Node argument covariant
When creating a node visitor that uses more specific types, we get this error: ``` Parameter #2 $visitor of static method GraphQL\Language\Visitor::visit() expects array<string, array<string, callable(GraphQL\Language\AST\Node): (GraphQL\Language\VisitorOperation|void|false|null)>|(callable(GraphQL\Language\AST\Node): (GraphQL\Language\VisitorOperation|void|false|null))>, array{OperationDefinition: array{enter: Closure(GraphQL\Language\AST\OperationDefinitionNode): void, leave: Closure(): void}, Field: array{enter: Closure(GraphQL\Language\AST\FieldNode): void, leave: Closure(GraphQL\Language\AST\FieldNode): void}, InlineFragment: array{enter: Closure(GraphQL\Language\AST\InlineFragmentNode): void, leave: Closure(GraphQL\Language\AST\InlineFragmentNode): void}, FragmentDefinition: array{enter: Closure(GraphQL\Language\AST\FragmentDefinitionNode): void, leave: Closure(GraphQL\Language\AST\FragmentDefinitionNode): void}, SelectionSet: Closure(GraphQL\Language\AST\SelectionSetNode): void} given. ``` For the following code: ```php $ast = Visitor::visit($ast, [ NodeKind::OPERATION_DEFINITION => [ 'enter' => function (OperationDefinitionNode $node) : void { $this->parents[] = $node->operation; // Remove the operation name as we don't need this $node->name = null; }, 'leave' => function () : void { array_pop($this->parents); }, ], NodeKind::FIELD => [ 'enter' => function (FieldNode $node) : void { $this->parents[] = $node->name->value; }, 'leave' => function (FieldNode $node) : void { array_pop($this->parents); }, ], // ... ``` By changing the signature to `covariant` the problem goes away.
1 parent b2c9686 commit ae54048

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

docs/class-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,7 @@ visitor API:
12981298
]
12991299
]);
13001300

1301-
@phpstan-type NodeVisitor callable(Node): (VisitorOperation|null|false|void)
1301+
@phpstan-type NodeVisitor callable(covariant Node): (VisitorOperation|null|false|void)
13021302
@phpstan-type VisitorArray array<string, NodeVisitor>|array<string, array<string, NodeVisitor>>
13031303

13041304
@see \GraphQL\Tests\Language\VisitorTest

src/Language/Visitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
* ]
9090
* ]);
9191
*
92-
* @phpstan-type NodeVisitor callable(Node): (VisitorOperation|null|false|void)
92+
* @phpstan-type NodeVisitor callable(covariant Node): (VisitorOperation|null|false|void)
9393
* @phpstan-type VisitorArray array<string, NodeVisitor>|array<string, array<string, NodeVisitor>>
9494
*
9595
* @see \GraphQL\Tests\Language\VisitorTest

0 commit comments

Comments
 (0)