Skip to content

Commit 3b5439b

Browse files
authored
fix: do not add false to NodeList (#1314)
1 parent e084611 commit 3b5439b

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

phpstan.neon.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ parameters:
3939
- message: "#^Property GraphQL\\\\Tests\\\\Type\\\\SchemaTest\\:\\:\\$implementingType is never read, only written\\.$#"
4040
path: tests
4141

42+
# Cannot satisfy input covariance
43+
- '~(expects|should return) 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\)>(,| but returns)?~'
44+
4245
includes:
4346
- phpstan-baseline.neon
4447
- phpstan/include-by-php-version.php

src/Language/Visitor.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use GraphQL\Language\AST\NodeKind;
77
use GraphQL\Language\AST\NodeList;
88
use GraphQL\Utils\TypeInfo;
9+
use GraphQL\Utils\Utils;
910

1011
/**
1112
* Utility for efficient AST traversal and modification.
@@ -88,7 +89,8 @@
8889
* ]
8990
* ]);
9091
*
91-
* @phpstan-type VisitorArray array<string, callable(Node): VisitorOperation|mixed|null>|array<string, array<string, callable(Node): VisitorOperation|mixed|null>>
92+
* @phpstan-type NodeVisitor callable(Node): (VisitorOperation|null|false|void)
93+
* @phpstan-type VisitorArray array<string, NodeVisitor>|array<string, array<string, NodeVisitor>>
9294
*/
9395
class Visitor
9496
{
@@ -218,6 +220,11 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null
218220
++$editOffset;
219221
} else {
220222
if ($node instanceof NodeList || \is_array($node)) {
223+
if (! $editValue instanceof Node) {
224+
$notNode = Utils::printSafe($editValue);
225+
throw new \Exception("Can only add Node to NodeList, got: {$notNode}.");
226+
}
227+
221228
$node[$editKey] = $editValue;
222229
} else {
223230
$node->{$editKey} = $editValue;
@@ -267,26 +274,23 @@ public static function visit(object $root, array $visitor, ?array $keyMap = null
267274

268275
if ($visitFn !== null) {
269276
$result = $visitFn($node, $key, $parent, $path, $ancestors);
270-
$editValue = null;
271277

272278
if ($result !== null) {
273-
if ($result instanceof VisitorOperation) {
274-
if ($result instanceof VisitorStop) {
275-
break;
276-
}
279+
if ($result instanceof VisitorStop) {
280+
break;
281+
}
277282

278-
if (! $isLeaving && $result instanceof VisitorSkipNode) {
283+
if ($result instanceof VisitorSkipNode) {
284+
if (! $isLeaving) {
279285
\array_pop($path);
280-
continue;
281286
}
282-
283-
if ($result instanceof VisitorRemoveNode) {
284-
$editValue = null;
285-
}
286-
} else {
287-
$editValue = $result;
287+
continue;
288288
}
289289

290+
$editValue = $result instanceof VisitorRemoveNode
291+
? null
292+
: $result;
293+
290294
$edits[] = [$key, $editValue];
291295
if (! $isLeaving) {
292296
if (! ($editValue instanceof Node)) {

src/Validator/QueryValidationContext.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,8 @@ private function getVariableUsages(HasSelectionSet $node): array
129129
Visitor::visitWithTypeInfo(
130130
$typeInfo,
131131
[
132-
NodeKind::VARIABLE_DEFINITION => static fn (): bool => false,
133-
NodeKind::VARIABLE => static function (VariableNode $variable) use (
134-
&$usages,
135-
$typeInfo
136-
): void {
132+
NodeKind::VARIABLE_DEFINITION => static fn () => Visitor::skipNode(),
133+
NodeKind::VARIABLE => static function (VariableNode $variable) use (&$usages, $typeInfo): void {
137134
$usages[] = [
138135
'node' => $variable,
139136
'type' => $typeInfo->getInputType(),

src/Validator/Rules/SingleFieldSubscription.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111

1212
class SingleFieldSubscription extends ValidationRule
1313
{
14-
/**
15-
* @return array<string, callable>
16-
*/
1714
public function getVisitor(QueryValidationContext $context): array
1815
{
1916
return [

0 commit comments

Comments
 (0)