| Total Complexity | 54 |
| Total Lines | 388 |
| 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 |
||
| 34 | class ValuesHelper |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * Prepares an object map of argument values given a list of argument |
||
| 38 | * definitions and list of argument AST nodes. |
||
| 39 | * |
||
| 40 | * Note: The returned value is a plain Object with a prototype, since it is |
||
| 41 | * exposed to user code. Care should be taken to not pull values from the |
||
| 42 | * Object prototype. |
||
| 43 | * |
||
| 44 | * @see http://facebook.github.io/graphql/October2016/#CoerceArgumentValues() |
||
| 45 | * |
||
| 46 | * @param Field|Directive $definition |
||
| 47 | * @param ArgumentsAwareInterface $node |
||
| 48 | * @param array $variableValues |
||
| 49 | * @return array |
||
| 50 | * @throws ExecutionException |
||
| 51 | * @throws InvalidTypeException |
||
| 52 | * @throws InvariantException |
||
| 53 | */ |
||
| 54 | public function coerceArgumentValues($definition, ArgumentsAwareInterface $node, array $variableValues = []): array |
||
| 55 | { |
||
| 56 | $coercedValues = []; |
||
| 57 | $argumentDefinitions = $definition->getArguments(); |
||
| 58 | $argumentNodes = $node->getArguments(); |
||
| 59 | |||
| 60 | if (empty($argumentDefinitions) || empty($argumentNodes)) { |
||
| 61 | return $coercedValues; |
||
| 62 | } |
||
| 63 | |||
| 64 | $argumentNodeMap = keyMap($argumentNodes, function (ArgumentNode $value) { |
||
| 65 | return $value->getNameValue(); |
||
| 66 | }); |
||
| 67 | |||
| 68 | foreach ($argumentDefinitions as $argumentDefinition) { |
||
| 69 | $argumentName = $argumentDefinition->getName(); |
||
| 70 | $argumentType = $argumentDefinition->getType(); |
||
| 71 | /** @var ArgumentNode $argumentNode */ |
||
| 72 | $argumentNode = $argumentNodeMap[$argumentName]; |
||
| 73 | $defaultValue = $argumentDefinition->getDefaultValue(); |
||
| 74 | |||
| 75 | if (null === $argumentNode) { |
||
| 76 | if (null === $defaultValue) { |
||
| 77 | $coercedValues[$argumentName] = $defaultValue; |
||
| 78 | } elseif (!$argumentType instanceof NonNullType) { |
||
| 79 | throw new ExecutionException( |
||
| 80 | sprintf('Argument "%s" of required type "%s" was not provided.', $argumentName, $argumentType), |
||
| 81 | [$node] |
||
| 82 | ); |
||
| 83 | } |
||
| 84 | } elseif ($argumentNode instanceof VariableNode) { |
||
| 85 | $coercedValues[$argumentName] = $this->coerceValueForVariableNode( |
||
| 86 | $argumentNode, |
||
| 87 | $argumentType, |
||
| 88 | $argumentName, |
||
| 89 | $variableValues, |
||
| 90 | $defaultValue |
||
| 91 | ); |
||
| 92 | } else { |
||
| 93 | $coercedValue = null; |
||
|
|
|||
| 94 | |||
| 95 | try { |
||
| 96 | $coercedValue = valueFromAST($argumentNode->getValue(), $argumentType, $variableValues); |
||
| 97 | } catch (CoercingException $ex) { |
||
| 98 | // Value nodes that cannot be resolved should be treated as invalid values |
||
| 99 | // therefore we catch the exception and leave the `$coercedValue` as `null`. |
||
| 100 | } |
||
| 101 | |||
| 102 | if (null === $coercedValue) { |
||
| 103 | // Note: ValuesOfCorrectType validation should catch this before |
||
| 104 | // execution. This is a runtime check to ensure execution does not |
||
| 105 | // continue with an invalid argument value. |
||
| 106 | throw new ExecutionException( |
||
| 107 | sprintf('Argument "%s" has invalid value %s.', $argumentName, $argumentNode), |
||
| 108 | [$argumentNode->getValue()] |
||
| 109 | ); |
||
| 110 | } |
||
| 111 | |||
| 112 | $coercedValues[$argumentName] = $coercedValue; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | return $coercedValues; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Prepares an object map of argument values given a directive definition |
||
| 121 | * and a AST node which may contain directives. Optionally also accepts a map |
||
| 122 | * of variable values. |
||
| 123 | * |
||
| 124 | * If the directive does not exist on the node, returns undefined. |
||
| 125 | * |
||
| 126 | * Note: The returned value is a plain Object with a prototype, since it is |
||
| 127 | * exposed to user code. Care should be taken to not pull values from the |
||
| 128 | * Object prototype. |
||
| 129 | * |
||
| 130 | * @param Directive $directive |
||
| 131 | * @param mixed $node |
||
| 132 | * @param array $variableValues |
||
| 133 | * @return array|null |
||
| 134 | * @throws ExecutionException |
||
| 135 | * @throws InvalidTypeException |
||
| 136 | * @throws InvariantException |
||
| 137 | */ |
||
| 138 | public function coerceDirectiveValues( |
||
| 139 | Directive $directive, |
||
| 140 | $node, |
||
| 141 | array $variableValues = [] |
||
| 142 | ): ?array { |
||
| 143 | $directiveNode = $node->hasDirectives() |
||
| 144 | ? find($node->getDirectives(), function (NameAwareInterface $value) use ($directive) { |
||
| 145 | return $value->getNameValue() === $directive->getName(); |
||
| 146 | }) : null; |
||
| 147 | |||
| 148 | if (null !== $directiveNode) { |
||
| 149 | return $this->coerceArgumentValues($directive, $directiveNode, $variableValues); |
||
| 150 | } |
||
| 151 | |||
| 152 | return null; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param Schema $schema |
||
| 157 | * @param array|VariableDefinitionNode[] $variableDefinitionNodes |
||
| 158 | * @param $input |
||
| 159 | * @return CoercedValue |
||
| 160 | * @throws \Exception |
||
| 161 | */ |
||
| 162 | public function coerceVariableValues(Schema $schema, array $variableDefinitionNodes, array $inputs): CoercedValue |
||
| 163 | { |
||
| 164 | $coercedValues = []; |
||
| 165 | $errors = []; |
||
| 166 | |||
| 167 | foreach ($variableDefinitionNodes as $variableDefinitionNode) { |
||
| 168 | $variableName = $variableDefinitionNode->getVariable()->getNameValue(); |
||
| 169 | $variableType = typeFromAST($schema, $variableDefinitionNode->getType()); |
||
| 170 | |||
| 171 | $type = $variableType; |
||
| 172 | if ($variableType instanceof WrappingTypeInterface) { |
||
| 173 | $type = $variableType->getOfType(); |
||
| 174 | } |
||
| 175 | |||
| 176 | if (!$type instanceof InputTypeInterface) { |
||
| 177 | throw new GraphQLException('InputTypeInterface'); |
||
| 178 | } else { |
||
| 179 | if (!isset($inputs[$variableName])) { |
||
| 180 | if ($variableType instanceof NonNullType) { |
||
| 181 | throw new GraphQLException('NonNullType'); |
||
| 182 | } elseif ($variableDefinitionNode->getDefaultValue() !== null) { |
||
| 183 | $coercedValues[$variableName] = valueFromAST( |
||
| 184 | $variableDefinitionNode->getDefaultValue(), |
||
| 185 | $variableType |
||
| 186 | ); |
||
| 187 | } |
||
| 188 | } else { |
||
| 189 | $value = $inputs[$variableName]; |
||
| 190 | $coercedValue = $this->coerceValue($value, $variableType, $variableDefinitionNode); |
||
| 191 | if ($coercedValue->hasErrors()) { |
||
| 192 | $messagePrelude = sprintf( |
||
| 193 | 'Variable "$%s" got invalid value %s', |
||
| 194 | $variableName, json_encode |
||
| 195 | ($value) |
||
| 196 | ); |
||
| 197 | foreach ($coercedValue->getErrors() as $error) { |
||
| 198 | $errors[] = $this->buildCoerceException( |
||
| 199 | $messagePrelude, |
||
| 200 | $variableDefinitionNode, |
||
| 201 | [], |
||
| 202 | $error->getMessage(), |
||
| 203 | $error |
||
| 204 | ); |
||
| 205 | } |
||
| 206 | } else { |
||
| 207 | $coercedValues[$variableName] = $coercedValue->getValue(); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | return new CoercedValue($coercedValues, $errors); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param mixed|array $value |
||
| 218 | * @param $type |
||
| 219 | * @param $blameNode |
||
| 220 | * @param array $path |
||
| 221 | * @return CoercedValue |
||
| 222 | * @throws InvariantException |
||
| 223 | * @throws GraphQLException |
||
| 224 | */ |
||
| 225 | private function coerceValue($value, $type, $blameNode, ?array $path = null): CoercedValue |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param string $message |
||
| 357 | * @param NodeInterface $blameNode |
||
| 358 | * @param array|null $path |
||
| 359 | * @param null|string $subMessage |
||
| 360 | * @param GraphQLException|null $originalError |
||
| 361 | * @return GraphQLException |
||
| 362 | */ |
||
| 363 | protected function buildCoerceException( |
||
| 381 | ); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param VariableNode $variableNode |
||
| 386 | * @param TypeInterface $argumentType |
||
| 387 | * @param string $argumentName |
||
| 388 | * @param array $variableValues |
||
| 389 | * @param mixed $defaultValue |
||
| 390 | * @return mixed |
||
| 391 | * @throws ExecutionException |
||
| 392 | */ |
||
| 393 | protected function coerceValueForVariableNode( |
||
| 422 | ); |
||
| 423 | } |
||
| 424 | } |
||
| 426 |