Completed
Push — master ( a7a918...731c42 )
by Alexandr
03:36
created

Processor::reduceQuery()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

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