From 40c610acb3016d2e1253e0434e86e32b672f4ec8 Mon Sep 17 00:00:00 2001 From: OpaqueRock <305760598+OpaqueRock@users.noreply.github.com> Date: Wed, 22 Jul 2026 17:26:30 +0300 Subject: [PATCH 1/3] Fix undefined-array-key warning in visit() for custom Node kinds 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. --- src/Language/Visitor.php | 4 ++- tests/Language/VisitorTest.php | 66 ++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/Language/Visitor.php b/src/Language/Visitor.php index d1101c68a..c63f45da2 100644 --- a/src/Language/Visitor.php +++ b/src/Language/Visitor.php @@ -319,7 +319,9 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null ]; $inList = $node instanceof NodeList; - $keys = ($inList ? $node : $visitorKeys[$node->kind]) ?? []; + // Nodes with a kind not present in $visitorKeys (e.g. a custom + // Node subclass) are treated as leaves, i.e. no children. + $keys = $inList ? $node : ($visitorKeys[$node->kind] ?? []); $index = -1; $edits = []; if ($parent !== null) { diff --git a/tests/Language/VisitorTest.php b/tests/Language/VisitorTest.php index 12acd43e0..70788a281 100644 --- a/tests/Language/VisitorTest.php +++ b/tests/Language/VisitorTest.php @@ -1905,4 +1905,70 @@ public function testThrowsExceptionWhenAddingIntegerToNodeList(): void ] ); } + + /** + * visit() must correctly invoke generic enter/leave visitors for a Node + * whose kind is not one of the built-in NodeKind constants (e.g. a + * custom Node subclass defined by a consumer). + */ + public function testVisitCallsGenericVisitorsForCustomKind(): void + { + $customNode = new class([]) extends Node { + public string $kind = 'CustomKind'; + }; + + $visited = []; + + 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]; + }, + ] + ); + + self::assertSame( + [ + ['enter', 'CustomKind'], + ['leave', 'CustomKind'], + ], + $visited + ); + } + + /** Same as testVisitCallsGenericVisitorsForCustomKind, but for a visitor keyed specifically by the custom kind. */ + public function testVisitCallsKindSpecificVisitorForCustomKind(): void + { + $customNode = new class([]) extends Node { + public string $kind = 'CustomKind'; + }; + + $visited = []; + + 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]; + }, + ], + ] + ); + + self::assertSame( + [ + ['enter', 'CustomKind'], + ['leave', 'CustomKind'], + ], + $visited + ); + } } From bade01b52ae0fa15933146c663b261a0932beded Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Tue, 6 Oct 2026 21:02:26 +0200 Subject: [PATCH 2/3] Fail custom kind visitor tests on warnings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The tests passed even with the warning present, since PHPUnit only reports warnings. Add changelog entry, drop redundant comments. 🤖 Generated with Claude Code --- CHANGELOG.md | 1 + src/Language/Visitor.php | 2 -- tests/Language/VisitorTest.php | 27 +++++++++++++++++++-------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3f63cee6..456138c52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ You can find and compare releases at the [GitHub release page](https://github.co ### Fixed - Reject invalid UTF-8 in documents https://github.com/webonyx/graphql-php/pull/1987 +- Avoid undefined array key warning when visiting nodes with a custom kind https://github.com/webonyx/graphql-php/pull/1952 ## v15.37.3 diff --git a/src/Language/Visitor.php b/src/Language/Visitor.php index c63f45da2..863bc932e 100644 --- a/src/Language/Visitor.php +++ b/src/Language/Visitor.php @@ -319,8 +319,6 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null ]; $inList = $node instanceof NodeList; - // Nodes with a kind not present in $visitorKeys (e.g. a custom - // Node subclass) are treated as leaves, i.e. no children. $keys = $inList ? $node : ($visitorKeys[$node->kind] ?? []); $index = -1; $edits = []; diff --git a/tests/Language/VisitorTest.php b/tests/Language/VisitorTest.php index 70788a281..2484300ab 100644 --- a/tests/Language/VisitorTest.php +++ b/tests/Language/VisitorTest.php @@ -1906,11 +1906,6 @@ public function testThrowsExceptionWhenAddingIntegerToNodeList(): void ); } - /** - * visit() must correctly invoke generic enter/leave visitors for a Node - * whose kind is not one of the built-in NodeKind constants (e.g. a - * custom Node subclass defined by a consumer). - */ public function testVisitCallsGenericVisitorsForCustomKind(): void { $customNode = new class([]) extends Node { @@ -1919,7 +1914,7 @@ public function testVisitCallsGenericVisitorsForCustomKind(): void $visited = []; - Visitor::visit( + self::visitFailingOnWarnings( $customNode, [ 'enter' => static function (Node $node) use (&$visited): void { @@ -1940,7 +1935,6 @@ public function testVisitCallsGenericVisitorsForCustomKind(): void ); } - /** Same as testVisitCallsGenericVisitorsForCustomKind, but for a visitor keyed specifically by the custom kind. */ public function testVisitCallsKindSpecificVisitorForCustomKind(): void { $customNode = new class([]) extends Node { @@ -1949,7 +1943,7 @@ public function testVisitCallsKindSpecificVisitorForCustomKind(): void $visited = []; - Visitor::visit( + self::visitFailingOnWarnings( $customNode, [ 'CustomKind' => [ @@ -1971,4 +1965,21 @@ public function testVisitCallsKindSpecificVisitorForCustomKind(): void $visited ); } + + /** + * @param array $visitor + * + * @throws \Exception + */ + private static function visitFailingOnWarnings(Node $root, array $visitor): void + { + set_error_handler(static function (int $severity, string $message): bool { + throw new \ErrorException($message, 0, $severity); + }); + try { + Visitor::visit($root, $visitor); + } finally { + restore_error_handler(); + } + } } From a74c0612bda9124b5d91a91ec9b9d838862c8491 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Sun, 25 Oct 2026 16:44:01 +0100 Subject: [PATCH 3/3] Update src/Language/Visitor.php --- src/Language/Visitor.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Language/Visitor.php b/src/Language/Visitor.php index 863bc932e..5bfcc48ce 100644 --- a/src/Language/Visitor.php +++ b/src/Language/Visitor.php @@ -319,7 +319,9 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null ]; $inList = $node instanceof NodeList; - $keys = $inList ? $node : ($visitorKeys[$node->kind] ?? []); + $keys = $inList + ? $node + : ($visitorKeys[$node->kind] ?? []); $index = -1; $edits = []; if ($parent !== null) {