Skip to content

Fix undefined-array-key warning in visit() for custom Node kinds#1952

Open
OpaqueRock wants to merge 1 commit into
webonyx:masterfrom
OpaqueRock:fix-visit-custom-kind-warning
Open

Fix undefined-array-key warning in visit() for custom Node kinds#1952
OpaqueRock wants to merge 1 commit into
webonyx:masterfrom
OpaqueRock:fix-visit-custom-kind-warning

Conversation

@OpaqueRock

Copy link
Copy Markdown

Fix undefined-array-key warning in visit() for custom Node kinds

Problem

Node::kind is not required to be one of the built-in NodeKind constants — consumers can define custom Node subclasses with arbitrary kind strings. Visitor::visit() resolved a node's child keys like this:

$keys = ($inList ? $node : $visitorKeys[$node->kind]) ?? [];

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 $inList is false, and PHP emits an undefined-array-key warning whenever a Node with a custom (non-NodeKind) kind is visited. The resulting value of $keys was 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:

$keys = $inList ? $node : ($visitorKeys[$node->kind] ?? []);

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.

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.
@spawnia
spawnia requested a review from Copilot July 22, 2026 15:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-kind Node, 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];
},
],
]
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants