Completed
Push — master ( 180a00...5febbb )
by Alexandr
03:11
created

Processor::getPreResolvedValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 11
Bugs 2 Features 1
Metric Value
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.6666
c 11
b 2
f 1
cc 2
eloc 4
nc 2
nop 3
crap 2.0185
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\Context\ExecutionContext;
13
use Youshido\GraphQL\Execution\Visitor\AbstractQueryVisitor;
14
use Youshido\GraphQL\Execution\Visitor\MaxComplexityQueryVisitor;
15
use Youshido\GraphQL\Field\AbstractField;
16
use Youshido\GraphQL\Field\Field;
17
use Youshido\GraphQL\Introspection\Field\SchemaField;
18
use Youshido\GraphQL\Introspection\Field\TypeDefinitionField;
19
use Youshido\GraphQL\Parser\Ast\Field as FieldAst;
20
use Youshido\GraphQL\Parser\Ast\Fragment;
21
use Youshido\GraphQL\Parser\Ast\FragmentInterface;
22
use Youshido\GraphQL\Parser\Ast\FragmentReference;
23
use Youshido\GraphQL\Parser\Ast\Mutation;
24
use Youshido\GraphQL\Parser\Ast\Query;
25
use Youshido\GraphQL\Parser\Ast\TypedFragmentReference;
26
use Youshido\GraphQL\Parser\Parser;
27
use Youshido\GraphQL\Schema\AbstractSchema;
28
use Youshido\GraphQL\Type\AbstractType;
29
use Youshido\GraphQL\Type\Object\AbstractObjectType;
30
use Youshido\GraphQL\Type\Scalar\AbstractScalarType;
31
use Youshido\GraphQL\Type\TypeInterface;
32
use Youshido\GraphQL\Type\TypeMap;
33
use Youshido\GraphQL\Type\TypeService;
34
use Youshido\GraphQL\Type\Union\AbstractUnionType;
35
use Youshido\GraphQL\Validator\Exception\ResolveException;
36
use Youshido\GraphQL\Validator\ResolveValidator\ResolveValidator;
37
use Youshido\GraphQL\Validator\ResolveValidator\ResolveValidatorInterface;
38
use Youshido\GraphQL\Validator\SchemaValidator\SchemaValidator;
39
40
class Processor
41
{
42
43
    const TYPE_NAME_QUERY = '__typename';
44
45
    /** @var  array */
46
    protected $data;
47
48
    /** @var ResolveValidatorInterface */
49
    protected $resolveValidator;
50
51
    /** @var ExecutionContext */
52
    protected $executionContext;
53
54
    /** @var int */
55
    protected $maxComplexity;
56
57 29
    public function __construct(AbstractSchema $schema)
58
    {
59 29
        (new SchemaValidator())->validate($schema);
60
61 28
        $this->introduceIntrospectionFields($schema);
62 28
        $this->executionContext = new ExecutionContext();
63 28
        $this->executionContext->setSchema($schema);
64
65 28
        $this->resolveValidator = new ResolveValidator($this->executionContext);
66 28
    }
67
68
69 27
    public function processPayload($payload, $variables = [], $reducers = [])
70
    {
71 27
        if ($this->executionContext->hasErrors()) {
72 7
            $this->executionContext->clearErrors();
73 7
        }
74
75 27
        $this->data = [];
76
77
        try {
78 27
            $this->parseAndCreateRequest($payload, $variables);
79
80 27
            $queryType    = $this->executionContext->getSchema()->getQueryType();
81 27
            $mutationType = $this->executionContext->getSchema()->getMutationType();
82
83 27
            if ($this->maxComplexity) {
84 1
                $reducers[] = new MaxComplexityQueryVisitor($this->maxComplexity);
85 1
            }
86
87 27
            $this->reduceQuery($queryType, $mutationType, $reducers);
88
89 27
            foreach ($this->executionContext->getRequest()->getOperationsInOrder() as $operation) {
90 27
                if ($operationResult = $this->executeOperation($operation, $operation instanceof Mutation ? $mutationType : $queryType)) {
91 24
                    $this->data = array_merge($this->data, $operationResult);
92 24
                };
93 27
            }
94
95 27
        } catch (\Exception $e) {
96 4
            $this->executionContext->addError($e);
97
        }
98
99 27
        return $this;
100
    }
101
102 27
    protected function parseAndCreateRequest($payload, $variables = [])
103
    {
104 27
        if (empty($payload)) {
105 1
            throw new \Exception('Must provide an operation.');
106
        }
107 27
        $parser = new Parser();
108
109 27
        $data = $parser->parse($payload);
110 27
        $this->executionContext->setRequest(new Request($data, $variables));
111 27
    }
112
113
    /**
114
     * @param Query|Field        $query
115
     * @param AbstractObjectType $currentLevelSchema
116
     * @return array|bool|mixed
117
     */
118 27
    protected function executeOperation(Query $query, $currentLevelSchema)
119
    {
120 27
        if (!$this->resolveValidator->objectHasField($currentLevelSchema, $query)) {
121 1
            return null;
122
        }
123
124
        /** @var AbstractField $field */
125 27
        $operationField = $currentLevelSchema->getField($query->getName());
126 27
        $alias          = $query->getAlias() ?: $query->getName();
127
128 27
        if (!$this->resolveValidator->validateArguments($operationField, $query, $this->executionContext->getRequest())) {
129 6
            return null;
130
        }
131
132 24
        return [$alias => $this->processQueryAST($query, $operationField)];
133
    }
134
135
    /**
136
     * @param Query         $query
137
     * @param AbstractField $field
138
     * @param               $contextValue
139
     * @return array|mixed|null
140
     */
141 24
    protected function processQueryAST(Query $query, AbstractField $field, $contextValue = null)
142
    {
143 24
        if (!$this->resolveValidator->validateArguments($field, $query, $this->executionContext->getRequest())) {
144
            return null;
145
        }
146
147 24
        $resolvedValue = $this->resolveFieldValue($field, $contextValue, $query);
148
149 24
        if (!$this->resolveValidator->isValidValueForField($field, $resolvedValue)) {
150 2
            return null;
151
        }
152
153 24
        return $this->collectValueForQueryWithType($query, $field->getType(), $resolvedValue);
154
    }
155
156
    /**
157
     * @param Query|Mutation $query
158
     * @param AbstractType   $fieldType
159
     * @param mixed          $resolvedValue
160
     * @return array|mixed
161
     */
162 24
    protected function collectValueForQueryWithType(Query $query, AbstractType $fieldType, $resolvedValue)
163
    {
164 24
        if (is_null($resolvedValue)) {
165 7
            return null;
166
        }
167
168 22
        $fieldType = $this->resolveValidator->resolveTypeIfAbstract($fieldType, $resolvedValue);
169 22
        $value     = [];
170
171 22
        if ($fieldType->getKind() == TypeMap::KIND_LIST) {
172 9
            if (!$this->resolveValidator->hasArrayAccess($resolvedValue)) return null;
173
174 9
            $namedType          = $fieldType->getNamedType();
175 9
            $isAbstract         = TypeService::isAbstractType($namedType);
176 9
            $validItemStructure = false;
177
178 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...
179 8
                $value[] = [];
180 8
                $index   = count($value) - 1;
181
182 8
                if ($isAbstract) {
183 2
                    $namedType = $this->resolveValidator->resolveAbstractType($fieldType->getNamedType(), $resolvedValueItem);
184 2
                }
185
186 8
                if (!$validItemStructure) {
187 8
                    if (!$namedType->isValidValue($resolvedValueItem)) {
188 1
                        $this->executionContext->addError(new ResolveException(sprintf('Not valid resolve value in %s field', $query->getName())));
189 1
                        $value[$index] = null;
190 1
                        continue;
191
                    }
192 7
                    $validItemStructure = true;
193 7
                }
194
195 7
                $value[$index] = $this->processQueryFields($query, $namedType, $resolvedValueItem, $value[$index]);
196 9
            }
197 9
        } else {
198 22
            if (!$query->hasFields()) {
199 5
                if (TypeService::isObjectType($fieldType)) {
200 1
                    throw new ResolveException(sprintf('You have to specify fields for "%s"', $query->getName()));
201
                }
202 4
203
                return $this->getOutputValue($fieldType, $resolvedValue);
204
            }
205 20
206
            $value = $this->processQueryFields($query, $fieldType, $resolvedValue, $value);
207
        }
208 20
209
        return $value;
210
    }
211
212
    /**
213
     * @param FieldAst      $fieldAst
214
     * @param AbstractField $field
215
     *
216
     * @param mixed         $contextValue
217
     * @return array|mixed|null
218
     * @throws ResolveException
219
     * @throws \Exception
220 19
     */
221
    protected function processFieldAST(FieldAst $fieldAst, AbstractField $field, $contextValue)
222 19
    {
223 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...
224 19
        $fieldType        = $field->getType();
225
        $preResolvedValue = $this->getPreResolvedValue($contextValue, $fieldAst, $field);
226 19
227 1
        if ($fieldType->getKind() == TypeMap::KIND_LIST) {
228 1
            $listValue = [];
229
            $type      = $fieldType->getNamedType();
230 1
231 1
            foreach ($preResolvedValue as $resolvedValueItem) {
232 1
                $listValue[] = $this->getOutputValue($type, $resolvedValueItem);
233
            }
234 1
235 1
            $value = $listValue;
236 19
        } else {
237
            $value = $this->getOutputValue($fieldType, $preResolvedValue);
238
        }
239 19
240
        return $value;
241
    }
242
243
    /**
244
     * @param AbstractField $field
245
     * @param mixed         $contextValue
246
     * @param Query         $query
247
     *
248
     * @return mixed
249 24
     */
250
    protected function resolveFieldValue(AbstractField $field, $contextValue, Query $query)
251 24
    {
252 22
        if ($resolveFunc = $field->getConfig()->getResolveFunction()) {
253 9
            return $resolveFunc($contextValue, $this->parseArgumentsValues($field, $query), $this->createResolveInfo($field, $query->getFields()));
254 1
        } elseif ($propertyValue = TypeService::getPropertyValue($contextValue, $field->getName())) {
255
            return $propertyValue;
256 8
        } else {
257
            return $field->resolve($contextValue, $this->parseArgumentsValues($field, $query), $this->createResolveInfo($field, $query->getFields()));
258
        }
259
    }
260 24
261 24
    protected function createResolveInfo($field, $fields)
262
    {
263
        return new ResolveInfo($field, $fields, $this->executionContext);
264
    }
265
266
    /**
267
     * @param               $contextValue
268
     * @param FieldAst      $fieldAst
269
     * @param AbstractField $field
270
     *
271
     * @throws \Exception
272
     *
273 19
     * @return mixed
274
     */
275 19
    protected function getPreResolvedValue($contextValue, FieldAst $fieldAst, AbstractField $field)
276 19
    {
277
        /** has to be split in validate arguments and set variables */
278 19
        if (!$this->resolveValidator->validateArguments($field, $fieldAst, $this->executionContext->getRequest())) {
279 3
            throw new \Exception(sprintf('Not valid arguments for the field "%s"', $fieldAst->getName()));
280
        }
281
282
        return $field->resolve($contextValue, $fieldAst->getKeyValueArguments(), $this->createResolveInfo($field, [$fieldAst]));
283 3
    }
284
285
    /**
286
     * @param $field     AbstractField
287
     * @param $query     Query
288 19
     *
289 14
     * @return array
290 8
     */
291 6
    protected function parseArgumentsValues(AbstractField $field, Query $query)
292 2
    {
293 2
        $args = [];
294 2
        foreach ($query->getArguments() as $argument) {
295
            if ($configArgument = $field->getConfig()->getArgument($argument->getName())) {
296 2
                $args[$argument->getName()] = $configArgument->getType()->parseValue($argument->getValue()->getValue());
297 1
            }
298
        }
299
300 2
        return $args;
301
    }
302
303
    /**
304
     * @param $query         Query|FragmentInterface
305
     * @param $queryType     AbstractObjectType|TypeInterface|Field|AbstractType
306
     * @param $resolvedValue mixed
307
     * @param $value         array
308
     *
309 24
     * @throws \Exception
310
     *
311 24
     * @return array
312 24
     */
313 15
    protected function processQueryFields($query, AbstractType $queryType, $resolvedValue, $value)
314 15
    {
315 15
        $currentType = $queryType->getNullableType();
316 24
317 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...
318 24
            if (!$query->hasFields()) {
319
                return $this->getOutputValue($currentType, $resolvedValue);
320
            } else {
321
                $this->executionContext->addError(new ResolveException(sprintf('Fields are not found in query "%s"', $query->getName())));
322
323
                return null;
324
            }
325
        }
326
327
        foreach ($query->getFields() as $fieldAst) {
328
329
            if ($fieldAst instanceof FragmentInterface) {
330
                /** @var TypedFragmentReference $fragment */
331 20
                $fragment = $fieldAst;
332
                if ($fieldAst instanceof FragmentReference) {
333 20
                    /** @var Fragment $fragment */
334
                    $fieldAstName = $fieldAst->getName();
335 20
                    $fragment     = $this->executionContext->getRequest()->getFragment($fieldAstName);
336 1
                    $this->resolveValidator->assertValidFragmentForField($fragment, $fieldAst, $queryType);
337 1
                } elseif ($fragment->getTypeName() !== $queryType->getName()) {
338
                    continue;
339 1
                }
340
341 1
                $fragmentValue = $this->processQueryFields($fragment, $queryType, $resolvedValue, $value);
342
                $value         = is_array($fragmentValue) ? $fragmentValue : [];
343
            } else {
344
                $fieldAstName = $fieldAst->getName();
345 20
                $alias        = $fieldAst->getAlias() ?: $fieldAstName;
346
347 20
                if ($fieldAstName == self::TYPE_NAME_QUERY) {
348
                    $value[$alias] = $queryType->getName();
349 3
                } else {
350 3
                    $field = $currentType->getField($fieldAstName);
351
                    if (!$field) {
352 2
                        $this->executionContext->addError(new ResolveException(sprintf('Field "%s" is not found in type "%s"', $fieldAstName, $currentType->getName())));
353 2
354 2
                        return null;
355 3
                    }
356 1
                    if ($fieldAst instanceof Query) {
357
                        $value[$alias] = $this->processQueryAST($fieldAst, $field, $resolvedValue);
358
                    } elseif ($fieldAst instanceof FieldAst) {
359 3
                        $value[$alias] = $this->processFieldAST($fieldAst, $field, $resolvedValue);
360 3
                    } else {
361 3
                        return $value;
362 20
                    }
363 20
                }
364
            }
365 20
366 1
        }
367 1
368 20
        return $value;
369 20
    }
370 3
371
    protected function getFieldValidatedValue(AbstractField $field, $value)
372 3
    {
373
        return ($this->resolveValidator->isValidValueForField($field, $value)) ? $this->getOutputValue($field->getType(), $value) : null;
374 20
    }
375 10
376 20
    protected function getOutputValue(AbstractType $type, $value)
377 19
    {
378 19
        return in_array($type->getKind(), [TypeMap::KIND_OBJECT, TypeMap::KIND_NON_NULL]) ? $value : $type->serialize($value);
379
    }
380
381
    protected function introduceIntrospectionFields(AbstractSchema $schema)
382
    {
383
        $schemaField = new SchemaField();
384 20
        $schema->addQueryField($schemaField);
385
        $schema->addQueryField(new TypeDefinitionField());
386 20
    }
387
388
    public function getResponseData()
389
    {
390
        $result = [];
391
392
        if (!empty($this->data)) {
393
            $result['data'] = $this->data;
394 21
        }
395
396 21
        if ($this->executionContext->hasErrors()) {
397
            $result['errors'] = $this->executionContext->getErrorsArray();
398
        }
399 28
400
        return $result;
401 28
    }
402 28
403 28
    /**
404 28
     * Convenience function for attaching a MaxComplexityQueryVisitor($max) to the next processor run
405
     *
406 27
     * @param int $max
407
     */
408 27
    public function setMaxComplexity($max)
409
    {
410 27
        $this->maxComplexity = $max;
411 24
    }
412 24
413
    /**
414 27
     * Apply all of $reducers to this query.  Example reducer operations: checking for maximum query complexity,
415 10
     * performing look-ahead query planning, etc.
416 10
     *
417
     * @param AbstractType           $queryType
418 27
     * @param AbstractType           $mutationType
419
     * @param AbstractQueryVisitor[] $reducers
420
     */
421
    protected function reduceQuery($queryType, $mutationType, array $reducers)
422
    {
423
        foreach ($reducers as $reducer) {
424
            foreach ($this->executionContext->getRequest()->getOperationsInOrder() as $operation) {
425
                $this->doVisit($operation, $operation instanceof Mutation ? $mutationType : $queryType, $reducer);
426 1
            }
427
        }
428 1
    }
429 1
430
    /**
431
     * Entry point for the `walkQuery` routine.  Execution bounces between here, where the reducer's ->visit() method
432
     * is invoked, and `walkQuery` where we send in the scores from the `visit` call.
433
     *
434
     * @param Query                $query
435
     * @param AbstractType         $currentLevelSchema
436
     * @param AbstractQueryVisitor $reducer
437
     */
438
    protected function doVisit(Query $query, $currentLevelSchema, $reducer)
439 27
    {
440
        if (!($currentLevelSchema instanceof AbstractObjectType) || !$currentLevelSchema->hasField($query->getName())) {
441 27
            return;
442 2
        }
443 2
444 2
        if ($operationField = $currentLevelSchema->getField($query->getName())) {
445 27
446 27
            $coroutine = $this->walkQuery($query, $operationField);
447
448
            if ($results = $coroutine->current()) {
449
                $queryCost = 0;
450
                while ($results) {
451
                    // initial values come from advancing the generator via ->current, subsequent values come from ->send()
452
                    list($queryField, $astField, $childCost) = $results;
453
454
                    /**
455
                     * @var Query|FieldAst $queryField
456 2
                     * @var Field          $astField
457
                     */
458 2
                    $cost = $reducer->visit($queryField->getKeyValueArguments(), $astField->getConfig(), $childCost);
459
                    $queryCost += $cost;
460
                    $results = $coroutine->send($cost);
461
                }
462 2
            }
463
        }
464 2
    }
465
466 2
    /**
467 2
     * Coroutine to walk the query and schema in DFS manner (see AbstractQueryVisitor docs for more info) and yield a
468 2
     * tuple of (queryNode, schemaNode, childScore)
469
     *
470 2
     * childScore costs are accumulated via values sent into the coroutine.
471
     *
472
     * Most of the branching in this function is just to handle the different types in a query: Queries, Unions,
473
     * Fragments (anonymous and named), and Fields.  The core of the function is simple: recurse until we hit the base
474
     * case of a Field and yield that back up to the visitor up in `doVisit`.
475
     *
476 2
     * @param Query|Field|FragmentInterface $queryNode
477 2
     * @param AbstractField                 $currentLevelAST
478 2
     *
479 2
     * @return \Generator
480 2
     */
481 2
    protected function walkQuery($queryNode, AbstractField $currentLevelAST)
482 2
    {
483
        $childrenScore = 0;
484
        if (!($queryNode instanceof FieldAst)) {
485
            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...
486
                if ($queryField instanceof FragmentInterface) {
487
                    if ($queryField instanceof FragmentReference) {
488
                        $queryField = $this->executionContext->getRequest()->getFragment($queryField->getName());
489
                    }
490
                    // the next 7 lines are essentially equivalent to `yield from $this->walkQuery(...)` in PHP7.
491
                    // for backwards compatibility this is equivalent.
492
                    // This pattern is repeated multiple times in this function, and unfortunately cannot be extracted or
493
                    // made less verbose.
494
                    $gen  = $this->walkQuery($queryField, $currentLevelAST);
0 ignored issues
show
Bug introduced by
It seems like $queryField defined by $this->executionContext-...$queryField->getName()) on line 488 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...
495
                    $next = $gen->current();
496
                    while ($next) {
497
                        $received = (yield $next);
498
                        $childrenScore += (int)$received;
499 2
                        $next = $gen->send($received);
500
                    }
501 2
                } else {
502 2
                    $fieldType = $currentLevelAST->getType()->getNamedType();
503 2
                    if ($fieldType instanceof AbstractUnionType) {
504 2
                        foreach ($fieldType->getTypes() as $unionFieldType) {
505 1 View Code Duplication
                            if ($fieldAst = $unionFieldType->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...
506
                                $gen  = $this->walkQuery($queryField, $fieldAst);
507
                                $next = $gen->current();
508
                                while ($next) {
509
                                    $received = (yield $next);
510
                                    $childrenScore += (int)$received;
511
                                    $next = $gen->send($received);
512 1
                                }
513 1
                            }
514 1
                        }
515 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...
516 1
                        $gen  = $this->walkQuery($queryField, $fieldAst);
517 1
                        $next = $gen->current();
518 1
                        while ($next) {
519 1
                            $received = (yield $next);
520 2
                            $childrenScore += (int)$received;
521 2
                            $next = $gen->send($received);
522 1
                        }
523 1
                    }
524 1
                }
525 1
            }
526 1
        }
527 1
        // sanity check.  don't yield fragments; they don't contribute to cost
528 1
        if ($queryNode instanceof Query || $queryNode instanceof FieldAst) {
529 1
            // BASE CASE.  If we're here we're done recursing -
530 1
            // this node is either a field, or a query that we've finished recursing into.
531 1
            yield [$queryNode, $currentLevelAST, $childrenScore];
532 1
        }
533 2
    }
534
}
535