Fix undefined-array-key warning in visit() for custom Node kinds#1952
Open
OpaqueRock wants to merge 1 commit into
Open
Fix undefined-array-key warning in visit() for custom Node kinds#1952OpaqueRock wants to merge 1 commit into
OpaqueRock wants to merge 1 commit into
Conversation
Node::kind is not required to be a NodeKind constant (custom Node subclasses are supported), so $visitorKeys[$node->kind] can be undefined. Previously the ?? [] fallback was applied to the whole ternary result, so it never triggered for the $inList branch and left the non-list branch's array access unguarded. Move the fallback onto just that array access instead. Add regression tests covering generic and kind-specific visitors for a custom Node kind.
OpaqueRock
added a commit
to OpaqueRock/graphql-php
that referenced
this pull request
Jul 22, 2026
The visit() undefined-array-key warning fix for custom Node kinds, along with its regression tests, has been split out into a separate PR (webonyx#1952) since it is unrelated to the Lexer/Visitor optimization work here.
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request fixes a PHP undefined array key notice/warning in Visitor::visit() when traversing AST nodes whose kind is not one of the built-in NodeKind constants (i.e., consumer-defined custom Node subclasses). It also adds tests intended to cover visiting such custom-kind nodes.
Changes:
- Adjust
Visitor::visit()to apply the?? []fallback directly to the$visitorKeys[$node->kind]array access, preventing undefined-key warnings for unknown kinds. - Add tests that exercise
visit()with a custom-kindNode, validating generic and kind-specific enter/leave visitor invocation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/Language/Visitor.php |
Moves null-coalescing fallback onto the visitorKeys[$node->kind] access to suppress undefined-key warnings for custom kinds. |
tests/Language/VisitorTest.php |
Adds coverage for visiting a custom-kind Node with both generic and kind-specific visitors. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+1922
to
+1932
| Visitor::visit( | ||
| $customNode, | ||
| [ | ||
| 'enter' => static function (Node $node) use (&$visited): void { | ||
| $visited[] = ['enter', $node->kind]; | ||
| }, | ||
| 'leave' => static function (Node $node) use (&$visited): void { | ||
| $visited[] = ['leave', $node->kind]; | ||
| }, | ||
| ] | ||
| ); |
Comment on lines
+1952
to
+1964
| Visitor::visit( | ||
| $customNode, | ||
| [ | ||
| 'CustomKind' => [ | ||
| 'enter' => static function (Node $node) use (&$visited): void { | ||
| $visited[] = ['enter', $node->kind]; | ||
| }, | ||
| 'leave' => static function (Node $node) use (&$visited): void { | ||
| $visited[] = ['leave', $node->kind]; | ||
| }, | ||
| ], | ||
| ] | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix undefined-array-key warning in visit() for custom Node kinds
Problem
Node::kindis not required to be one of the built-inNodeKindconstants — consumers can define customNodesubclasses with arbitrary kind strings.Visitor::visit()resolved a node's child keys like this:The parentheses are misplaced:
??only suppresses an undefined-index/notice-level warning when it directly wraps the array access it's guarding. Here it wraps the whole ternary instead, so$visitorKeys[$node->kind]is evaluated as a plain array access when$inListis false, and PHP emits an undefined-array-key warning whenever a Node with a custom (non-NodeKind) kind is visited. The resulting value of$keyswas already correct in that case (it still ends up[]) — only the warning was spurious.Fix
Move the
?? []fallback directly onto the array access so??can suppress the warning as intended:No behavior change — custom-kind nodes were already treated as leaves (no children); this just removes the spurious warning.
Tests
Added regression tests covering generic and kind-specific enter/leave visitors invoked on a custom-kind Node via
visit().Context
Split out of #1948, where this pre-existing bug was noticed while optimizing
Visitor::visitInParallel(). It's an independent, smaller fix so it can be reviewed/merged on its own.