Skip to content

Commit 5a455ff

Browse files
authored
Make NodeVisitor Node argument covariant
When creating a node visitor that uses more specific types, we get this error: ``` Parameter webonyx#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 5a455ff

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

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)