Completed
Push — master ( d9547a...f0235a )
by Alexandr
06:59 queued 03:18
created

Processor::createResolveInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
/*
3
* This file is a part of graphql-youshido project.
4
*
5
* @author Portey Vasil <[email protected]>
6
* @author Alexandr Viniychuk <[email protected]>
7
* created: 11/28/15 1:05 AM
8
*/
9
10
namespace Youshido\GraphQL\Execution;
11
12
use Youshido\GraphQL\Execution\Container\Container;
13
use Youshido\GraphQL\Execution\Context\ExecutionContext;
14
use Youshido\GraphQL\Execution\Visitor\AbstractQueryVisitor;
15
use Youshido\GraphQL\Execution\Visitor\MaxComplexityQueryVisitor;
16
use Youshido\GraphQL\Field\AbstractField;
17
use Youshido\GraphQL\Field\Field;
18
use Youshido\GraphQL\Parser\Ast\Field as FieldAst;
19
use Youshido\GraphQL\Parser\Ast\Fragment;
20
use Youshido\GraphQL\Parser\Ast\FragmentInterface;
21
use Youshido\GraphQL\Parser\Ast\FragmentReference;
22
use Youshido\GraphQL\Parser\Ast\Mutation;
23
use Youshido\GraphQL\Parser\Ast\Query;
24
use Youshido\GraphQL\Parser\Ast\TypedFragmentReference;
25
use Youshido\GraphQL\Parser\Parser;
26
use Youshido\GraphQL\Schema\AbstractSchema;
27
use Youshido\GraphQL\Type\AbstractType;
28
use Youshido\GraphQL\Type\Object\AbstractObjectType;
29
use Youshido\GraphQL\Type\TypeInterface;
30
use Youshido\GraphQL\Type\TypeMap;
31
use Youshido\GraphQL\Type\TypeService;
32
use Youshido\GraphQL\Type\Union\AbstractUnionType;
33
use Youshido\GraphQL\Validator\Exception\ResolveException;
34
use Youshido\GraphQL\Validator\ResolveValidator\ResolveValidator;
35
use Youshido\GraphQL\Validator\ResolveValidator\ResolveValidatorInterface;
36
37
class Processor
38
{
39
40
    const TYPE_NAME_QUERY = '__typename';
41
42
    /** @var  array */
43
    protected $data;
44
45
    /** @var ResolveValidatorInterface */
46
    protected $resolveValidator;
47
48
    /** @var ExecutionContext */
49
    protected $executionContext;
50
51
    /** @var int */
52
    protected $maxComplexity;
53
54 30
    public function __construct(AbstractSchema $schema)
55
    {
56 30
        if (empty($this->executionContext)) {
57 30
            $this->executionContext = new ExecutionContext($schema);
58 30
            $this->executionContext->setContainer(new Container());
59
        }
60 30
        $this->resolveValidator = new ResolveValidator($this->executionContext);
61 30
    }
62
63 28
    public function processPayload($payload, $variables = [], $reducers = [])
64
    {
65 28
        $this->data = [];
66
67
        try {
68 28
            $this->parseAndCreateRequest($payload, $variables);
69
70 28
            $queryType    = $this->executionContext->getSchema()->getQueryType();
71 28
            $mutationType = $this->executionContext->getSchema()->getMutationType();
72
73 28
            if ($this->maxComplexity) {
74 1
                $reducers[] = new MaxComplexityQueryVisitor($this->maxComplexity);
75
            }
76
77 28
            $this->reduceQuery($queryType, $mutationType, $reducers);
78
79 28
            foreach ($this->executionContext->getRequest()->getOperationsInOrder() as $operation) {
80 28
                if ($operationResult = $this->executeOperation($operation, $operation instanceof Mutation ? $mutationType : $queryType)) {
81 28
                    $this->data = array_merge($this->data, $operationResult);
82
                };
83
            }
84
85 4
        } catch (\Exception $e) {
86 4
            $this->executionContext->addError($e);
87
        }
88
89 28
        return $this;
90
    }
91
92 28
    protected function parseAndCreateRequest($payload, $variables = [])
93
    {
94 28
        if (empty($payload)) {
95 1
            throw new \Exception('Must provide an operation.');
96
        }
97 28
        $parser = new Parser();
98
99 28
        $data = $parser->parse($payload);
100 28
        $this->executionContext->setRequest(new Request($data, $variables));
101 28
    }
102
103
    /**
104
     * @param Query|Field        $query
105
     * @param AbstractObjectType $currentLevelSchema
106
     * @return array|bool|mixed
107
     */
108 28
    protected function executeOperation(Query $query, $currentLevelSchema)
109
    {
110 28
        if (!$this->resolveValidator->objectHasField($currentLevelSchema, $query)) {
111 1
            return null;
112
        }
113
114
        /** @var AbstractField $field */
115 28
        $operationField = $currentLevelSchema->getField($query->getName());
116 28
        $alias          = $query->getAlias() ?: $query->getName();
117
118 28
        if (!$this->resolveValidator->validateArguments($operationField, $query, $this->executionContext->getRequest())) {
119 6
            return null;
120
        }
121
122 25
        return [$alias => $this->processQueryAST($query, $operationField)];
123
    }
124
125
    /**
126
     * @param Query         $query
127
     * @param AbstractField $field
128
     * @param               $contextValue
129
     * @return array|mixed|null
130
     */
131 25
    protected function processQueryAST(Query $query, AbstractField $field, $contextValue = null)
132
    {
133 25
        if (!$this->resolveValidator->validateArguments($field, $query, $this->executionContext->getRequest())) {
134
            return null;
135
        }
136
137 25
        $resolvedValue = $this->resolveFieldValue($field, $contextValue, $query->getFields(), $this->parseArgumentsValues($field, $query));
138
139 25
        if (!$this->resolveValidator->isValidValueForField($field, $resolvedValue)) {
140 2
            return null;
141
        }
142
143 25
        return $this->collectValueForQueryWithType($query, $field->getType(), $resolvedValue);
144
    }
145
146
    /**
147
     * @param Query|Mutation $query
148
     * @param AbstractType   $fieldType
149
     * @param mixed          $resolvedValue
150
     * @return array|mixed
151
     * @throws ResolveException
152
     */
153 25
    protected function collectValueForQueryWithType(Query $query, AbstractType $fieldType, $resolvedValue)
154
    {
155 25
        if (is_null($resolvedValue)) {
156 7
            return null;
157
        }
158
159 23
        $value = [];
160
161 23
        if (!$query->hasFields()) {
162 6
            $fieldType = $this->resolveValidator->resolveTypeIfAbstract($fieldType, $resolvedValue);
163
164 6
            if (TypeService::isObjectType($fieldType->getNamedType())) {
165 1
                throw new ResolveException(sprintf('You have to specify fields for "%s"', $query->getName()));
166
            }
167 5
            if (TypeService::isScalarType($fieldType)) {
168 5
                return $this->getOutputValue($fieldType, $resolvedValue);
169
            }
170
        }
171
172 20
        if ($fieldType->getKind() == TypeMap::KIND_LIST) {
173 9
            if (!$this->resolveValidator->hasArrayAccess($resolvedValue)) return null;
174
175 9
            $namedType          = $fieldType->getNamedType();
176 9
            $isAbstract         = TypeService::isAbstractType($namedType);
177 9
            $validItemStructure = false;
178
179 9
            foreach ($resolvedValue as $resolvedValueItem) {
0 ignored issues
show
Bug introduced by
The expression $resolvedValue of type object|integer|double|string|array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
180 8
                $value[] = [];
181 8
                $index   = count($value) - 1;
182
183 8
                if ($isAbstract) {
184 2
                    $namedType = $this->resolveValidator->resolveAbstractType($fieldType->getNamedType(), $resolvedValueItem);
185
                }
186
187 8
                if (!$validItemStructure) {
188 8
                    if (!$namedType->isValidValue($resolvedValueItem)) {
189 1
                        $this->executionContext->addError(new ResolveException(sprintf('Not valid resolve value in %s field', $query->getName())));
190 1
                        $value[$index] = null;
191 1
                        continue;
192
                    }
193 7
                    $validItemStructure = true;
194
                }
195
196 9
                $value[$index] = $this->processQueryFields($query, $namedType, $resolvedValueItem, $value[$index]);
197
            }
198
        } else {
199 20
            $value = $this->processQueryFields($query, $fieldType, $resolvedValue, $value);
200
        }
201
202 20
        return $value;
203
    }
204
205
    /**
206
     * @param FieldAst      $fieldAst
207
     * @param AbstractField $field
208
     *
209
     * @param mixed         $contextValue
210
     * @return array|mixed|null
211
     * @throws ResolveException
212
     * @throws \Exception
213
     */
214 19
    protected function processFieldAST(FieldAst $fieldAst, AbstractField $field, $contextValue)
215
    {
216 19
        $value            = null;
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
217 19
        $fieldType        = $field->getType();
218 19
        $preResolvedValue = $this->getPreResolvedValue($contextValue, $fieldAst, $field);
219
220 19
        if ($fieldType->getKind() == TypeMap::KIND_LIST) {
221 1
            $listValue = [];
222 1
            $type      = $fieldType->getNamedType();
223
224 1
            foreach ($preResolvedValue as $resolvedValueItem) {
225 1
                $listValue[] = $this->getOutputValue($type, $resolvedValueItem);
226
            }
227
228 1
            $value = $listValue;
229
        } else {
230 19
            $value = $this->getOutputValue($fieldType, $preResolvedValue);
231
        }
232
233 19
        return $value;
234
    }
235
236 25
    protected function createResolveInfo($field, $fields)
237
    {
238 25
        return new ResolveInfo($field, $fields, $this->executionContext);
239
    }
240
241
    /**
242
     * @param               $contextValue
243
     * @param FieldAst      $fieldAst
244
     * @param AbstractField $field
245
     *
246
     * @throws \Exception
247
     *
248
     * @return mixed
249
     */
250 19
    protected function getPreResolvedValue($contextValue, FieldAst $fieldAst, AbstractField $field)
251
    {
252 19
        if ($field->hasArguments() && !$this->resolveValidator->validateArguments($field, $fieldAst, $this->executionContext->getRequest())) {
253
            throw new \Exception(sprintf('Not valid arguments for the field "%s"', $fieldAst->getName()));
254
        }
255
256 19
        return $this->resolveFieldValue($field, $contextValue, [$fieldAst], $fieldAst->getKeyValueArguments());
257
258
    }
259
260 25
    protected function resolveFieldValue(AbstractField $field, $contextValue, array $fields, array $args)
261
    {
262 25
        return $field->resolve($contextValue, $args, $this->createResolveInfo($field, $fields));
263
    }
264
265
    /**
266
     * @param $field     AbstractField
267
     * @param $query     Query
268
     *
269
     * @return array
270
     */
271 25
    protected function parseArgumentsValues(AbstractField $field, Query $query)
272
    {
273 25
        $args = [];
274 25
        foreach ($query->getArguments() as $argument) {
275 15
            if ($configArgument = $field->getConfig()->getArgument($argument->getName())) {
276 15
                $args[$argument->getName()] = $configArgument->getType()->parseValue($argument->getValue()->getValue());
277
            }
278
        }
279
280 25
        return $args;
281
    }
282
283
    /**
284
     * @param $query         Query|FragmentInterface
285
     * @param $queryType     AbstractObjectType|TypeInterface|Field|AbstractType
286
     * @param $resolvedValue mixed
287
     * @param $value         array
288
     *
289
     * @throws \Exception
290
     *
291
     * @return array
292
     */
293 20
    protected function processQueryFields($query, AbstractType $queryType, $resolvedValue, $value)
294
    {
295 20
        $originalType = $queryType;
296 20
        $queryType    = $this->resolveValidator->resolveTypeIfAbstract($queryType, $resolvedValue);
297 20
        $currentType  = $queryType->getNullableType();
298
299
300 20 View Code Duplication
        if ($currentType->getKind() == TypeMap::KIND_SCALAR) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
301 1
            if (!$query->hasFields()) {
302 1
                return $this->getOutputValue($currentType, $resolvedValue);
303
            } else {
304 1
                $this->executionContext->addError(new ResolveException(sprintf('Fields are not found in query "%s"', $query->getName())));
305
306 1
                return null;
307
            }
308
        }
309
310 20
        foreach ($query->getFields() as $fieldAst) {
311
312 20
            if ($fieldAst instanceof FragmentInterface) {
313
                /** @var TypedFragmentReference $fragment */
314 3
                $fragment = $fieldAst;
315 3
                if ($fieldAst instanceof FragmentReference) {
316
                    /** @var Fragment $fragment */
317 2
                    $fieldAstName = $fieldAst->getName();
318 2
                    $fragment     = $this->executionContext->getRequest()->getFragment($fieldAstName);
319 2
                    $this->resolveValidator->assertValidFragmentForField($fragment, $fieldAst, $originalType);
320 1
                } elseif ($fragment->getTypeName() !== $queryType->getName()) {
321 1
                    continue;
322
                }
323
324 3
                $fragmentValue = $this->processQueryFields($fragment, $queryType, $resolvedValue, $value);
325 3
                $value         = is_array($fragmentValue) ? $fragmentValue : [];
326
            } else {
327 20
                $fieldAstName = $fieldAst->getName();
328 20
                $alias        = $fieldAst->getAlias() ?: $fieldAstName;
329
330 20
                if ($fieldAstName == self::TYPE_NAME_QUERY) {
331 1
                    $value[$alias] = $queryType->getName();
332
                } else {
333 20
                    $field = $currentType->getField($fieldAstName);
334 20
                    if (!$field) {
335 3
                        $this->executionContext->addError(new ResolveException(sprintf('Field "%s" is not found in type "%s"', $fieldAstName, $currentType->getName())));
336
337 3
                        return null;
338
                    }
339 20
                    if ($fieldAst instanceof Query) {
340 10
                        $value[$alias] = $this->processQueryAST($fieldAst, $field, $resolvedValue);
341
                    } elseif ($fieldAst instanceof FieldAst) {
342 19
                        $value[$alias] = $this->processFieldAST($fieldAst, $field, $resolvedValue);
343
                    } else {
344 20
                        return $value;
345
                    }
346
                }
347
            }
348
349
        }
350
351 20
        return $value;
352
    }
353
354
    protected function getFieldValidatedValue(AbstractField $field, $value)
355
    {
356
        return ($this->resolveValidator->isValidValueForField($field, $value)) ? $this->getOutputValue($field->getType(), $value) : null;
357
    }
358
359 22
    protected function getOutputValue(AbstractType $type, $value)
360
    {
361 22
        return in_array($type->getKind(), [TypeMap::KIND_OBJECT, TypeMap::KIND_NON_NULL]) ? $value : $type->serialize($value);
362
    }
363
364 28
    public function getResponseData()
365
    {
366 28
        $result = [];
367
368 28
        if (!empty($this->data)) {
369 25
            $result['data'] = $this->data;
370
        }
371
372 28
        if ($this->executionContext->hasErrors()) {
373 10
            $result['errors'] = $this->executionContext->getErrorsArray();
374
        }
375
376 28
        return $result;
377
    }
378
379
    /**
380
     * You can access ExecutionContext to check errors and inject dependencies
381
     *
382
     * @return ExecutionContext
383
     */
384 9
    public function getExecutionContext()
385
    {
386 9
        return $this->executionContext;
387
    }
388
389
    /**
390
     * Convenience function for attaching a MaxComplexityQueryVisitor($max) to the next processor run
391
     *
392
     * @param int $max
393
     */
394 1
    public function setMaxComplexity($max)
395
    {
396 1
        $this->maxComplexity = $max;
397 1
    }
398
399
    /**
400
     * Apply all of $reducers to this query.  Example reducer operations: checking for maximum query complexity,
401
     * performing look-ahead query planning, etc.
402
     *
403
     * @param AbstractType           $queryType
404
     * @param AbstractType           $mutationType
405
     * @param AbstractQueryVisitor[] $reducers
406
     */
407 28
    protected function reduceQuery($queryType, $mutationType, array $reducers)
408
    {
409 28
        foreach ($reducers as $reducer) {
410 2
            foreach ($this->executionContext->getRequest()->getOperationsInOrder() as $operation) {
411 2
                $this->doVisit($operation, $operation instanceof Mutation ? $mutationType : $queryType, $reducer);
412
            }
413
        }
414 28
    }
415
416
    /**
417
     * Entry point for the `walkQuery` routine.  Execution bounces between here, where the reducer's ->visit() method
418
     * is invoked, and `walkQuery` where we send in the scores from the `visit` call.
419
     *
420
     * @param Query                $query
421
     * @param AbstractType         $currentLevelSchema
422
     * @param AbstractQueryVisitor $reducer
423
     */
424 2
    protected function doVisit(Query $query, $currentLevelSchema, $reducer)
425
    {
426 2
        if (!($currentLevelSchema instanceof AbstractObjectType) || !$currentLevelSchema->hasField($query->getName())) {
427
            return;
428
        }
429
430 2
        if ($operationField = $currentLevelSchema->getField($query->getName())) {
431
432 2
            $coroutine = $this->walkQuery($query, $operationField);
433
434 2
            if ($results = $coroutine->current()) {
435 2
                $queryCost = 0;
436 2
                while ($results) {
437
                    // initial values come from advancing the generator via ->current, subsequent values come from ->send()
438 2
                    list($queryField, $astField, $childCost) = $results;
439
440
                    /**
441
                     * @var Query|FieldAst $queryField
442
                     * @var Field          $astField
443
                     */
444 2
                    $cost = $reducer->visit($queryField->getKeyValueArguments(), $astField->getConfig(), $childCost);
445 2
                    $queryCost += $cost;
446 2
                    $results = $coroutine->send($cost);
447
                }
448
            }
449
        }
450 2
    }
451
452
    /**
453
     * Coroutine to walk the query and schema in DFS manner (see AbstractQueryVisitor docs for more info) and yield a
454
     * tuple of (queryNode, schemaNode, childScore)
455
     *
456
     * childScore costs are accumulated via values sent into the coroutine.
457
     *
458
     * Most of the branching in this function is just to handle the different types in a query: Queries, Unions,
459
     * Fragments (anonymous and named), and Fields.  The core of the function is simple: recurse until we hit the base
460
     * case of a Field and yield that back up to the visitor up in `doVisit`.
461
     *
462
     * @param Query|Field|FragmentInterface $queryNode
463
     * @param AbstractField                 $currentLevelAST
464
     *
465
     * @return \Generator
466
     */
467 2
    protected function walkQuery($queryNode, AbstractField $currentLevelAST)
468
    {
469 2
        $childrenScore = 0;
470 2
        if (!($queryNode instanceof FieldAst)) {
471 2
            foreach ($queryNode->getFields() as $queryField) {
0 ignored issues
show
Bug introduced by
The method getFields does only exist in Youshido\GraphQL\Field\F...raphQL\Parser\Ast\Query, but not in Youshido\GraphQL\Parser\Ast\FragmentInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
472 2
                if ($queryField instanceof FragmentInterface) {
473 1
                    if ($queryField instanceof FragmentReference) {
474
                        $queryField = $this->executionContext->getRequest()->getFragment($queryField->getName());
475
                    }
476
                    // the next 7 lines are essentially equivalent to `yield from $this->walkQuery(...)` in PHP7.
477
                    // for backwards compatibility this is equivalent.
478
                    // This pattern is repeated multiple times in this function, and unfortunately cannot be extracted or
479
                    // made less verbose.
480 1
                    $gen  = $this->walkQuery($queryField, $currentLevelAST);
0 ignored issues
show
Bug introduced by
It seems like $queryField defined by $this->executionContext-...$queryField->getName()) on line 474 can also be of type null or object<Youshido\GraphQL\Parser\Ast\Fragment>; however, Youshido\GraphQL\Execution\Processor::walkQuery() does only seem to accept object<Youshido\GraphQL\...\Ast\FragmentInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
481 1
                    $next = $gen->current();
482 1
                    while ($next) {
483 1
                        $received = (yield $next);
484 1
                        $childrenScore += (int)$received;
485 1
                        $next = $gen->send($received);
486
                    }
487
                } else {
488 2
                    $fieldType = $currentLevelAST->getType()->getNamedType();
489 2
                    if ($fieldType instanceof AbstractUnionType) {
490 1
                        foreach ($fieldType->getTypes() as $unionFieldType) {
491 1 View Code Duplication
                            if ($fieldAst = $unionFieldType->getField($queryField->getName())) {
0 ignored issues
show
Bug introduced by
The method getField does only exist in Youshido\GraphQL\Type\Object\AbstractObjectType, but not in Youshido\GraphQL\Type\Scalar\AbstractScalarType.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
492 1
                                $gen  = $this->walkQuery($queryField, $fieldAst);
493 1
                                $next = $gen->current();
494 1
                                while ($next) {
495 1
                                    $received = (yield $next);
496 1
                                    $childrenScore += (int)$received;
497 1
                                    $next = $gen->send($received);
498
                                }
499
                            }
500
                        }
501 1 View Code Duplication
                    } elseif ($fieldType instanceof AbstractObjectType && $fieldAst = $fieldType->getField($queryField->getName())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
502 1
                        $gen  = $this->walkQuery($queryField, $fieldAst);
503 1
                        $next = $gen->current();
504 2
                        while ($next) {
505 1
                            $received = (yield $next);
506 1
                            $childrenScore += (int)$received;
507 1
                            $next = $gen->send($received);
508
                        }
509
                    }
510
                }
511
            }
512
        }
513
        // sanity check.  don't yield fragments; they don't contribute to cost
514 2
        if ($queryNode instanceof Query || $queryNode instanceof FieldAst) {
515
            // BASE CASE.  If we're here we're done recursing -
516
            // this node is either a field, or a query that we've finished recursing into.
517 2
            yield [$queryNode, $currentLevelAST, $childrenScore];
518
        }
519 2
    }
520
}
521