Skip to content

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

Merged
spawnia merged 10 commits into
webonyx:masterfrom
OpaqueRock:fix-visit-custom-kind-warning
Oct 25, 2026
Merged

spawnia merged 10 commits into
webonyx:masterfrom
OpaqueRock:fix-visit-custom-kind-warning

Conversation

@OpaqueRock

@OpaqueRock OpaqueRock commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

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.

Changelog

Fixed

  • Avoid undefined array key warning when visiting nodes with a custom kind

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 thread tests/Language/VisitorTest.php Outdated
Comment thread tests/Language/VisitorTest.php Outdated

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

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.

Copilot review overview

🟢 Approval recommended

The focused implementation correctly suppresses the warning while preserving behavior, with adequate regression coverage.

Review effort: Balanced
Findings: None

Resolved since last review (2)

Comment thread src/Language/Visitor.php Outdated
@spawnia
spawnia merged commit ee4e28d into webonyx:master Oct 25, 2026
22 checks passed
@spawnia

spawnia commented Oct 25, 2026

Copy link
Copy Markdown
Collaborator

Thanks @OpaqueRock! Released as v15.38.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants