Skip to content

Commit 4dc53ba

Browse files
committed
fix sloppy if conditions
1 parent 385dbe6 commit 4dc53ba

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

src/Execution/Executor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ public function collectFields(ExecutionContext $executionContext, GraphQLObjectT
127127
break;
128128
}
129129
$visitedFragmentNames[$fragName] = true;
130-
$fragment = $executionContext->getFragments()[$fragName];
131-
if (!$fragment || !$this->doesFragmentConditionMatch($executionContext, $fragment, $runtimeType)) {
130+
$fragment = $executionContext->getFragments()[$fragName] ?? null;
131+
if ($fragment === null || !$this->doesFragmentConditionMatch($executionContext, $fragment, $runtimeType)) {
132132
break;
133133
}
134134
$this->collectFields(
@@ -166,8 +166,8 @@ private function getFieldEntryKey($node): string
166166
*/
167167
private function doesFragmentConditionMatch(ExecutionContext $executionContext, $fragment, GraphQLObjectType $type): bool
168168
{
169-
$typeConditionNode = $fragment["typeCondition"];
170-
if (!$typeConditionNode) {
169+
$typeConditionNode = $fragment["typeCondition"] ?? null;
170+
if ($typeConditionNode === null) {
171171
return true;
172172
}
173173

src/Execution/Values.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
abstract class Values
1414
{
15+
/**
16+
* @param Schema $schema
17+
* @param array $variableDefinitions
18+
* @param array $inputs
19+
* @return array
20+
*/
1521
public static function getVariableValues(Schema $schema, array $variableDefinitions, array $inputs): array
1622
{
1723
$errors = [];
@@ -23,6 +29,14 @@ public static function getVariableValues(Schema $schema, array $variableDefiniti
2329
return $errors;
2430
}
2531

32+
/**
33+
* @param Schema $schema
34+
* @param array $variableDefinitions
35+
* @param array $inputs
36+
* @return array
37+
* @throws GraphQLError
38+
* @throws \GraphQL\Errors\ValidationError
39+
*/
2640
public static function coerceVariableValues(Schema $schema, array $variableDefinitions, array $inputs): array
2741
{
2842
$coercedValues = [];
@@ -69,6 +83,13 @@ public static function coerceVariableValues(Schema $schema, array $variableDefin
6983
return $coercedValues;
7084
}
7185

86+
/**
87+
* @param GraphQLDirective $directiveDef
88+
* @param $node
89+
* @param $variableValues
90+
* @return array|null
91+
* @throws GraphQLError
92+
*/
7293
public static function getDirectiveValues(GraphQLDirective $directiveDef, $node, $variableValues): ?array
7394
{
7495
$directiveNode = array_filter($node["directives"], function ($directive) use ($directiveDef) {
@@ -83,6 +104,14 @@ public static function getDirectiveValues(GraphQLDirective $directiveDef, $node,
83104
return null;
84105
}
85106

107+
/**
108+
* @param $def
109+
* @param $node
110+
* @param $variableValues
111+
* @return array
112+
* @throws GraphQLError
113+
* @throws \GraphQL\Errors\ValidationError
114+
*/
86115
public static function getArgumentValues($def, $node, $variableValues): array
87116
{
88117
$coercedValues = [];
@@ -99,7 +128,7 @@ public static function getArgumentValues($def, $node, $variableValues): array
99128
$argumentNode = $argNodeMap[$name] ?? null;
100129

101130
// if no argument specified in AST, check if default argument exists and is valid
102-
if (!$argumentNode) {
131+
if ($argumentNode === null) {
103132
if ($argDef->getDefaultValue() !== null) {
104133
$coercedValues[$name] = $argDef->getDefaultValue();
105134
} else if ($argType->isNonNullType()) {

src/Utilities/Ast.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
abstract class Ast
1414
{
15+
/**
16+
* @param Schema $schema
17+
* @param $typeNode
18+
* @return GraphQLList|GraphQLNonNull|GraphQLType|null
19+
* @throws GraphQLError
20+
*/
1521
static function typeFromAst(Schema $schema, $typeNode)
1622
{
1723
if ($typeNode["kind"] === "ListType") {
@@ -31,9 +37,16 @@ static function typeFromAst(Schema $schema, $typeNode)
3137
);
3238
}
3339

34-
static function valueFromAst(array $valueNode, GraphQLType $type, ?array $variables = null)
40+
/**
41+
* @param array|null $valueNode
42+
* @param GraphQLType $type
43+
* @param array|null $variables
44+
* @return array|UndefinedValue|mixed|null
45+
* @throws ValidationError
46+
*/
47+
static function valueFromAst(?array $valueNode, GraphQLType $type, ?array $variables = null)
3548
{
36-
if (!$valueNode) {
49+
if ($valueNode === null) {
3750
// When there is no node, then there is also no value.
3851
// Importantly, this is different from returning the value null.
3952
return new UndefinedValue();
@@ -182,6 +195,11 @@ static function valueFromAst(array $valueNode, GraphQLType $type, ?array $variab
182195
return new UndefinedValue();
183196
}
184197

198+
/**
199+
* @param $valueNode
200+
* @param $variables
201+
* @return bool
202+
*/
185203
private static function isMissingVariable($valueNode, $variables): bool
186204
{
187205
return (

0 commit comments

Comments
 (0)