Total Complexity | 87 |
Total Lines | 611 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like ValuesResolver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ValuesResolver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class ValuesResolver |
||
42 | { |
||
43 | /** |
||
44 | * Prepares an object map of argument values given a list of argument |
||
45 | * definitions and list of argument AST nodes. |
||
46 | * |
||
47 | * @see https://facebook.github.io/graphql/October2016/#CoerceArgumentValues() |
||
48 | * |
||
49 | * @param Field|Directive $definition |
||
50 | * @param ArgumentsAwareInterface $node |
||
51 | * @param array $variableValues |
||
52 | * @return array |
||
53 | * @throws ExecutionException |
||
54 | * @throws InvalidTypeException |
||
55 | * @throws InvariantException |
||
56 | */ |
||
57 | public function coerceArgumentValues($definition, ArgumentsAwareInterface $node, array $variableValues = []): array |
||
58 | { |
||
59 | $coercedValues = []; |
||
60 | $argumentDefinitions = $definition->getArguments(); |
||
61 | $argumentNodes = $node->getArguments(); |
||
62 | |||
63 | if (empty($argumentDefinitions)) { |
||
64 | return $coercedValues; |
||
65 | } |
||
66 | |||
67 | /** @var ArgumentNode[] $argumentNodeMap */ |
||
68 | $argumentNodeMap = keyMap($argumentNodes, function (ArgumentNode $value) { |
||
69 | return $value->getNameValue(); |
||
70 | }); |
||
71 | |||
72 | foreach ($argumentDefinitions as $argumentDefinition) { |
||
73 | $argumentName = $argumentDefinition->getName(); |
||
74 | $argumentType = $argumentDefinition->getType(); |
||
75 | $argumentNode = $argumentNodeMap[$argumentName] ?? null; |
||
76 | $defaultValue = $argumentDefinition->getDefaultValue(); |
||
77 | $argumentValue = null !== $argumentNode ? $argumentNode->getValue() : null; |
||
78 | |||
79 | if (null !== $argumentNode && $argumentValue instanceof VariableNode) { |
||
80 | $variableName = $argumentValue->getNameValue(); |
||
81 | $hasValue = !empty($variableValues) && \array_key_exists($variableName, $variableValues); |
||
82 | $isNull = $hasValue && null === $variableValues[$variableName]; |
||
83 | } else { |
||
84 | $hasValue = null !== $argumentNode; |
||
85 | $isNull = $hasValue && $argumentValue instanceof NullValueNode; |
||
86 | } |
||
87 | |||
88 | if (!$hasValue && null !== $defaultValue) { |
||
89 | // If no argument was provided where the definition has a default value, |
||
90 | // use the default value. |
||
91 | $coercedValues[$argumentName] = $defaultValue; |
||
92 | } elseif ((!$hasValue || $isNull) && $argumentType instanceof NonNullType) { |
||
93 | // If no argument or a null value was provided to an argument with a |
||
94 | // non-null type (required), produce a field error. |
||
95 | if ($isNull) { |
||
96 | throw new ExecutionException( |
||
97 | \sprintf( |
||
98 | 'Argument "%s" of non-null type "%s" must not be null.', |
||
99 | $argumentName, |
||
100 | $argumentType |
||
101 | ), |
||
102 | [$argumentValue] |
||
103 | ); |
||
104 | } elseif (null !== $argumentNode && $argumentValue instanceof VariableNode) { |
||
105 | $variableName = $argumentValue->getNameValue(); |
||
106 | throw new ExecutionException( |
||
107 | \sprintf( |
||
108 | 'Argument "%s" of required type "%s" was provided the variable "$%s" ' |
||
109 | . 'which was not provided a runtime value.', |
||
110 | $argumentName, |
||
111 | $argumentType, |
||
112 | $variableName |
||
113 | ), |
||
114 | [$argumentValue] |
||
115 | ); |
||
116 | } else { |
||
117 | throw new ExecutionException( |
||
118 | \sprintf( |
||
119 | 'Argument "%s" of required type "%s" was not provided.', |
||
120 | $argumentName, |
||
121 | $argumentType |
||
122 | ), |
||
123 | [$node] |
||
124 | ); |
||
125 | } |
||
126 | } elseif ($hasValue) { |
||
127 | if ($argumentValue instanceof NullValueNode) { |
||
128 | // If the explicit value `null` was provided, an entry in the coerced |
||
129 | // values must exist as the value `null`. |
||
130 | $coercedValues[$argumentName] = null; |
||
131 | } elseif ($argumentValue instanceof VariableNode) { |
||
132 | $variableName = $argumentValue->getNameValue(); |
||
133 | invariant(!empty($variableValues), 'Must exist for hasValue to be true.'); |
||
134 | // Note: This does no further checking that this variable is correct. |
||
135 | // This assumes that this query has been validated and the variable |
||
136 | // usage here is of the correct type. |
||
137 | $coercedValues[$argumentName] = $variableValues[$variableName]; |
||
138 | } else { |
||
139 | $valueNode = $argumentNode->getValue(); |
||
|
|||
140 | try { |
||
141 | // Value nodes that cannot be resolved should be treated as invalid values |
||
142 | // because there is no undefined value in PHP so that we throw an exception |
||
143 | $coercedValue = ValueASTConverter::convert($valueNode, $argumentType, $variableValues); |
||
144 | } catch (\Exception $ex) { |
||
145 | // Note: ValuesOfCorrectType validation should catch this before |
||
146 | // execution. This is a runtime check to ensure execution does not |
||
147 | // continue with an invalid argument value. |
||
148 | throw new ExecutionException( |
||
149 | \sprintf( |
||
150 | 'Argument "%s" has invalid value %s.', |
||
151 | $argumentName, |
||
152 | (string)$argumentValue |
||
153 | ), |
||
154 | [$argumentValue], |
||
155 | null, |
||
156 | null, |
||
157 | null, |
||
158 | null, |
||
159 | $ex |
||
160 | ); |
||
161 | } |
||
162 | $coercedValues[$argumentName] = $coercedValue; |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | |||
167 | return $coercedValues; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Prepares an object map of argument values given a directive definition |
||
172 | * and a AST node which may contain directives. Optionally also accepts a map |
||
173 | * of variable values. |
||
174 | * |
||
175 | * If the directive does not exist on the node, returns null. |
||
176 | * |
||
177 | * @param Directive $directive |
||
178 | * @param mixed $node |
||
179 | * @param array $variableValues |
||
180 | * @return array|null |
||
181 | * @throws ExecutionException |
||
182 | * @throws InvalidTypeException |
||
183 | * @throws InvariantException |
||
184 | */ |
||
185 | public function coerceDirectiveValues( |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Prepares an object map of variableValues of the correct type based on the |
||
204 | * provided variable definitions and arbitrary input. If the input cannot be |
||
205 | * parsed to match the variable definitions, a GraphQLError will be thrown. |
||
206 | * |
||
207 | * @param Schema $schema |
||
208 | * @param array|VariableDefinitionNode[] $variableDefinitionNodes |
||
209 | * @param array $inputs |
||
210 | * @return CoercedValue |
||
211 | * @throws GraphQLException |
||
212 | * @throws InvariantException |
||
213 | * @throws ConversionException |
||
214 | */ |
||
215 | public function coerceVariableValues( |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @param TypeInterface|null $type |
||
305 | * @return bool |
||
306 | */ |
||
307 | protected function isInputType(?TypeInterface $type) |
||
308 | { |
||
309 | return ($type instanceof ScalarType) || |
||
310 | ($type instanceof EnumType) || |
||
311 | ($type instanceof InputObjectType) || |
||
312 | (($type instanceof WrappingTypeInterface) && $this->isInputType($type->getOfType())); |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Returns either a value which is valid for the provided type or a list of |
||
317 | * encountered coercion errors. |
||
318 | * |
||
319 | * @param mixed|array $value |
||
320 | * @param mixed $type |
||
321 | * @param NodeInterface $blameNode |
||
322 | * @param Path|null $path |
||
323 | * @return CoercedValue |
||
324 | * @throws GraphQLException |
||
325 | * @throws InvariantException |
||
326 | */ |
||
327 | private function coerceValue($value, $type, $blameNode, ?Path $path = null): CoercedValue |
||
328 | { |
||
329 | if ($type instanceof NonNullType) { |
||
330 | return $this->coerceValueForNonNullType($value, $type, $blameNode, $path); |
||
331 | } |
||
332 | |||
333 | if (null === $value) { |
||
334 | return new CoercedValue(null); |
||
335 | } |
||
336 | |||
337 | if ($type instanceof ScalarType) { |
||
338 | return $this->coerceValueForScalarType($value, $type, $blameNode, $path); |
||
339 | } |
||
340 | |||
341 | if ($type instanceof EnumType) { |
||
342 | return $this->coerceValueForEnumType($value, $type, $blameNode, $path); |
||
343 | } |
||
344 | |||
345 | if ($type instanceof ListType) { |
||
346 | return $this->coerceValueForListType($value, $type, $blameNode, $path); |
||
347 | } |
||
348 | |||
349 | if ($type instanceof InputObjectType) { |
||
350 | return $this->coerceValueForInputObjectType($value, $type, $blameNode, $path); |
||
351 | } |
||
352 | |||
353 | throw new GraphQLException('Unexpected type.'); |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * @param mixed $value |
||
358 | * @param NonNullType $type |
||
359 | * @param NodeInterface $blameNode |
||
360 | * @param Path|null $path |
||
361 | * @return CoercedValue |
||
362 | * @throws GraphQLException |
||
363 | * @throws InvariantException |
||
364 | */ |
||
365 | protected function coerceValueForNonNullType( |
||
366 | $value, |
||
367 | NonNullType $type, |
||
368 | NodeInterface $blameNode, |
||
369 | ?Path $path |
||
370 | ): CoercedValue { |
||
371 | if (null === $value) { |
||
372 | return new CoercedValue(null, [ |
||
373 | $this->buildCoerceException( |
||
374 | sprintf('Expected non-nullable type %s not to be null', (string)$type), |
||
375 | $blameNode, |
||
376 | $path |
||
377 | ) |
||
378 | ]); |
||
379 | } |
||
380 | return $this->coerceValue($value, $type->getOfType(), $blameNode, $path); |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Scalars determine if a value is valid via parseValue(), which can |
||
385 | * throw to indicate failure. If it throws, maintain a reference to |
||
386 | * the original error. |
||
387 | * |
||
388 | * @param mixed $value |
||
389 | * @param ScalarType $type |
||
390 | * @param NodeInterface $blameNode |
||
391 | * @param Path|null $path |
||
392 | * @return CoercedValue |
||
393 | */ |
||
394 | protected function coerceValueForScalarType( |
||
395 | $value, |
||
396 | ScalarType $type, |
||
397 | NodeInterface $blameNode, |
||
398 | ?Path $path |
||
399 | ): CoercedValue { |
||
400 | try { |
||
401 | $parseResult = $type->parseValue($value); |
||
402 | if (null === $parseResult) { |
||
403 | return new CoercedValue(null, [ |
||
404 | new GraphQLException(sprintf('Expected type %s', (string)$type)) |
||
405 | ]); |
||
406 | } |
||
407 | return new CoercedValue($parseResult); |
||
408 | } catch (InvalidTypeException|CoercingException $ex) { |
||
409 | return new CoercedValue(null, [ |
||
410 | $this->buildCoerceException( |
||
411 | sprintf('Expected type %s', (string)$type), |
||
412 | $blameNode, |
||
413 | $path, |
||
414 | $ex->getMessage(), |
||
415 | $ex |
||
416 | ) |
||
417 | ]); |
||
418 | } |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * @param mixed $value |
||
423 | * @param EnumType $type |
||
424 | * @param NodeInterface $blameNode |
||
425 | * @param Path|null $path |
||
426 | * @return CoercedValue |
||
427 | * @throws InvariantException |
||
428 | */ |
||
429 | protected function coerceValueForEnumType( |
||
430 | $value, |
||
431 | EnumType $type, |
||
432 | NodeInterface $blameNode, |
||
433 | ?Path $path |
||
434 | ): CoercedValue { |
||
435 | if (\is_string($value) && null !== ($enumValue = $type->getValue($value))) { |
||
436 | return new CoercedValue($enumValue); |
||
437 | } |
||
438 | |||
439 | $suggestions = suggestionList((string)$value, \array_map(function (EnumValue $enumValue) { |
||
440 | return $enumValue->getName(); |
||
441 | }, $type->getValues())); |
||
442 | |||
443 | $didYouMean = (!empty($suggestions)) |
||
444 | ? 'did you mean' . \implode(',', $suggestions) |
||
445 | : null; |
||
446 | |||
447 | return new CoercedValue(null, [ |
||
448 | $this->buildCoerceException(\sprintf('Expected type %s', $type->getName()), $blameNode, $path, $didYouMean) |
||
449 | ]); |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * @param mixed $value |
||
454 | * @param InputObjectType $type |
||
455 | * @param NodeInterface $blameNode |
||
456 | * @param Path|null $path |
||
457 | * @return CoercedValue |
||
458 | * @throws GraphQLException |
||
459 | * @throws InvariantException |
||
460 | */ |
||
461 | protected function coerceValueForInputObjectType( |
||
462 | $value, |
||
463 | InputObjectType $type, |
||
464 | NodeInterface $blameNode, |
||
465 | ?Path $path |
||
466 | ): CoercedValue { |
||
467 | $errors = []; |
||
468 | $coercedValues = []; |
||
469 | $fields = $type->getFields(); |
||
470 | |||
471 | // Ensure every defined field is valid. |
||
472 | foreach ($fields as $field) { |
||
473 | $fieldType = $field->getType(); |
||
474 | |||
475 | if (!isset($value[$field->getName()])) { |
||
476 | if (!empty($field->getDefaultValue())) { |
||
477 | $coercedValue[$field->getName()] = $field->getDefaultValue(); |
||
478 | } elseif ($fieldType instanceof NonNullType) { |
||
479 | $errors[] = new GraphQLException( |
||
480 | \sprintf( |
||
481 | "Field %s of required type %s! was not provided.", |
||
482 | $this->printPath(new Path($path, $field->getName())), |
||
483 | (string)$fieldType->getOfType() |
||
484 | ) |
||
485 | ); |
||
486 | } |
||
487 | } else { |
||
488 | $fieldValue = $value[$field->getName()]; |
||
489 | $coercedValue = $this->coerceValue( |
||
490 | $fieldValue, |
||
491 | $fieldType, |
||
492 | $blameNode, |
||
493 | new Path($path, $field->getName()) |
||
494 | ); |
||
495 | |||
496 | if ($coercedValue->hasErrors()) { |
||
497 | $errors = \array_merge($errors, $coercedValue->getErrors()); |
||
498 | } elseif (empty($errors)) { |
||
499 | $coercedValues[$field->getName()] = $coercedValue->getValue(); |
||
500 | } |
||
501 | } |
||
502 | } |
||
503 | |||
504 | // Ensure every provided field is defined. |
||
505 | foreach ($value as $fieldName => $fieldValue) { |
||
506 | if (!isset($fields[$fieldName])) { |
||
507 | $suggestions = suggestionList($fieldName, \array_keys($fields)); |
||
508 | $didYouMean = (!empty($suggestions)) |
||
509 | ? 'did you mean' . \implode(',', $suggestions) |
||
510 | : null; |
||
511 | |||
512 | $errors[] = $this->buildCoerceException( |
||
513 | \sprintf('Field "%s" is not defined by type %s', $fieldName, $type->getName()), |
||
514 | $blameNode, |
||
515 | $path, |
||
516 | $didYouMean |
||
517 | ); |
||
518 | } |
||
519 | } |
||
520 | |||
521 | return new CoercedValue($coercedValues, $errors); |
||
522 | } |
||
523 | |||
524 | /** |
||
525 | * @param mixed $value |
||
526 | * @param ListType $type |
||
527 | * @param NodeInterface $blameNode |
||
528 | * @param Path|null $path |
||
529 | * @return CoercedValue |
||
530 | * @throws GraphQLException |
||
531 | * @throws InvariantException |
||
532 | */ |
||
533 | protected function coerceValueForListType( |
||
534 | $value, |
||
535 | ListType $type, |
||
536 | NodeInterface $blameNode, |
||
537 | ?Path $path |
||
538 | ): CoercedValue { |
||
539 | $itemType = $type->getOfType(); |
||
540 | |||
541 | if (\is_array($value) || $value instanceof \Traversable) { |
||
542 | $errors = []; |
||
543 | $coercedValues = []; |
||
544 | foreach ($value as $index => $itemValue) { |
||
545 | $coercedValue = $this->coerceValue($itemValue, $itemType, $blameNode, new Path($path, $index)); |
||
546 | |||
547 | if ($coercedValue->hasErrors()) { |
||
548 | $errors = \array_merge($errors, $coercedValue->getErrors()); |
||
549 | } else { |
||
550 | $coercedValues[] = $coercedValue->getValue(); |
||
551 | } |
||
552 | } |
||
553 | |||
554 | return new CoercedValue($coercedValues, $errors); |
||
555 | } |
||
556 | |||
557 | // Lists accept a non-list value as a list of one. |
||
558 | $coercedValue = $this->coerceValue($value, $itemType, $blameNode); |
||
559 | |||
560 | return new CoercedValue([$coercedValue->getValue()], $coercedValue->getErrors()); |
||
561 | } |
||
562 | |||
563 | /** |
||
564 | * @param string $message |
||
565 | * @param NodeInterface $blameNode |
||
566 | * @param Path|null $path |
||
567 | * @param null|string $subMessage |
||
568 | * @param GraphQLException|null $originalException |
||
569 | * @return GraphQLException |
||
570 | */ |
||
571 | protected function buildCoerceException( |
||
572 | string $message, |
||
573 | NodeInterface $blameNode, |
||
574 | ?Path $path, |
||
575 | ?string $subMessage = null, |
||
576 | ?GraphQLException $originalException = null |
||
577 | ) { |
||
578 | $stringPath = $this->printPath($path); |
||
579 | |||
580 | return new CoercingException( |
||
581 | $message . |
||
582 | (($stringPath !== '') ? ' at ' . $stringPath : $stringPath) . |
||
583 | (($subMessage !== null) ? '; ' . $subMessage : '.'), |
||
584 | [$blameNode], |
||
585 | null, |
||
586 | null, |
||
587 | // TODO: Change this to null |
||
588 | [], |
||
589 | null, |
||
590 | $originalException |
||
591 | ); |
||
592 | } |
||
593 | |||
594 | /** |
||
595 | * @param Path|null $path |
||
596 | * @return string |
||
597 | */ |
||
598 | protected function printPath(?Path $path) |
||
612 | } |
||
613 | |||
614 | /** |
||
615 | * @param VariableNode $variableNode |
||
616 | * @param TypeInterface $argumentType |
||
617 | * @param string $argumentName |
||
618 | * @param array $variableValues |
||
619 | * @param mixed $defaultValue |
||
620 | * @return mixed |
||
621 | * @throws ExecutionException |
||
622 | */ |
||
623 | protected function coerceValueForVariableNode( |
||
652 | ); |
||
653 | } |
||
654 | } |
||
655 | } |
||
656 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.