| Total Complexity | 50 |
| Total Lines | 361 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ValuesHelper 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 ValuesHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class ValuesHelper |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * Prepares an object map of argument values given a list of argument |
||
| 37 | * definitions and list of argument AST nodes. |
||
| 38 | * |
||
| 39 | * Note: The returned value is a plain Object with a prototype, since it is |
||
| 40 | * exposed to user code. Care should be taken to not pull values from the |
||
| 41 | * Object prototype. |
||
| 42 | * |
||
| 43 | * @see http://facebook.github.io/graphql/October2016/#CoerceArgumentValues() |
||
| 44 | * |
||
| 45 | * @param Field|Directive $definition |
||
| 46 | * @param ArgumentsAwareInterface $node |
||
| 47 | * @param array $variableValues |
||
| 48 | * @return array |
||
| 49 | * @throws ExecutionException |
||
| 50 | * @throws InvalidTypeException |
||
| 51 | * @throws InvariantException |
||
| 52 | */ |
||
| 53 | public function coerceArgumentValues($definition, ArgumentsAwareInterface $node, array $variableValues = []): array |
||
| 54 | { |
||
| 55 | $coercedValues = []; |
||
| 56 | $argumentDefinitions = $definition->getArguments(); |
||
| 57 | $argumentNodes = $node->getArguments(); |
||
| 58 | |||
| 59 | if (empty($argumentDefinitions) || empty($argumentNodes)) { |
||
| 60 | return $coercedValues; |
||
| 61 | } |
||
| 62 | |||
| 63 | $argumentNodeMap = keyMap($argumentNodes, function (ArgumentNode $value) { |
||
| 64 | return $value->getNameValue(); |
||
| 65 | }); |
||
| 66 | |||
| 67 | foreach ($argumentDefinitions as $argumentDefinition) { |
||
| 68 | $argumentName = $argumentDefinition->getName(); |
||
| 69 | $argumentType = $argumentDefinition->getType(); |
||
| 70 | /** @var ArgumentNode $argumentNode */ |
||
| 71 | $argumentNode = $argumentNodeMap[$argumentName]; |
||
| 72 | $defaultValue = $argumentDefinition->getDefaultValue(); |
||
| 73 | |||
| 74 | if (null === $argumentNode) { |
||
| 75 | if (null === $defaultValue) { |
||
| 76 | $coercedValues[$argumentName] = $defaultValue; |
||
| 77 | } elseif (!$argumentType instanceof NonNullType) { |
||
| 78 | throw new ExecutionException( |
||
| 79 | sprintf('Argument "%s" of required type "%s" was not provided.', $argumentName, $argumentType), |
||
| 80 | [$node] |
||
| 81 | ); |
||
| 82 | } |
||
| 83 | } elseif ($argumentNode instanceof VariableNode) { |
||
| 84 | $coercedValues[$argumentName] = $this->coerceValueForVariableNode( |
||
| 85 | $argumentNode, |
||
| 86 | $argumentType, |
||
| 87 | $argumentName, |
||
| 88 | $variableValues, |
||
| 89 | $defaultValue |
||
| 90 | ); |
||
| 91 | } else { |
||
| 92 | $coercedValue = null; |
||
|
|
|||
| 93 | |||
| 94 | try { |
||
| 95 | $coercedValue = valueFromAST($argumentNode->getValue(), $argumentType, $variableValues); |
||
| 96 | } catch (CoercingException $ex) { |
||
| 97 | // Value nodes that cannot be resolved should be treated as invalid values |
||
| 98 | // therefore we catch the exception and leave the `$coercedValue` as `null`. |
||
| 99 | } |
||
| 100 | |||
| 101 | if (null === $coercedValue) { |
||
| 102 | // Note: ValuesOfCorrectType validation should catch this before |
||
| 103 | // execution. This is a runtime check to ensure execution does not |
||
| 104 | // continue with an invalid argument value. |
||
| 105 | throw new ExecutionException( |
||
| 106 | sprintf('Argument "%s" has invalid value %s.', $argumentName, $argumentNode), |
||
| 107 | [$argumentNode->getValue()] |
||
| 108 | ); |
||
| 109 | } |
||
| 110 | |||
| 111 | $coercedValues[$argumentName] = $coercedValue; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | return $coercedValues; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Prepares an object map of argument values given a directive definition |
||
| 120 | * and a AST node which may contain directives. Optionally also accepts a map |
||
| 121 | * of variable values. |
||
| 122 | * |
||
| 123 | * If the directive does not exist on the node, returns undefined. |
||
| 124 | * |
||
| 125 | * Note: The returned value is a plain Object with a prototype, since it is |
||
| 126 | * exposed to user code. Care should be taken to not pull values from the |
||
| 127 | * Object prototype. |
||
| 128 | * |
||
| 129 | * @param Directive $directive |
||
| 130 | * @param mixed $node |
||
| 131 | * @param array $variableValues |
||
| 132 | * @return array|null |
||
| 133 | * @throws ExecutionException |
||
| 134 | * @throws InvalidTypeException |
||
| 135 | * @throws InvariantException |
||
| 136 | */ |
||
| 137 | public function coerceDirectiveValues( |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param Schema $schema |
||
| 156 | * @param array|VariableDefinitionNode[] $variableDefinitionNodes |
||
| 157 | * @param $input |
||
| 158 | * @return array |
||
| 159 | * @throws \Exception |
||
| 160 | */ |
||
| 161 | public function coerceVariableValues(Schema $schema, array $variableDefinitionNodes, array $inputs): array |
||
| 207 | ]; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param $value |
||
| 212 | * @param $type |
||
| 213 | * @param $blameNode |
||
| 214 | * @param array $path |
||
| 215 | * @return array |
||
| 216 | * @throws GraphQLException |
||
| 217 | * @throws InvariantException |
||
| 218 | */ |
||
| 219 | private function coerceValue($value, $type, $blameNode, ?array $path = []) |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param VariableNode $variableNode |
||
| 358 | * @param TypeInterface $argumentType |
||
| 359 | * @param string $argumentName |
||
| 360 | * @param array $variableValues |
||
| 361 | * @param mixed $defaultValue |
||
| 362 | * @return mixed |
||
| 363 | * @throws ExecutionException |
||
| 364 | */ |
||
| 365 | protected function coerceValueForVariableNode( |
||
| 398 |