Total Complexity | 61 |
Total Lines | 499 |
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 | * @see http://facebook.github.io/graphql/October2016/#CoerceArgumentValues() |
||
41 | * |
||
42 | * @param Field|Directive $definition |
||
43 | * @param ArgumentsAwareInterface $node |
||
44 | * @param array $variableValues |
||
45 | * @return array |
||
46 | * @throws ExecutionException |
||
47 | * @throws InvalidTypeException |
||
48 | * @throws InvariantException |
||
49 | */ |
||
50 | public function coerceArgumentValues($definition, ArgumentsAwareInterface $node, array $variableValues = []): array |
||
51 | { |
||
52 | $coercedValues = []; |
||
53 | $argumentDefinitions = $definition->getArguments(); |
||
54 | $argumentNodes = $node->getArguments(); |
||
55 | |||
56 | if (empty($argumentDefinitions) || empty($argumentNodes)) { |
||
57 | return $coercedValues; |
||
58 | } |
||
59 | |||
60 | $argumentNodeMap = keyMap($argumentNodes, function (ArgumentNode $value) { |
||
61 | return $value->getNameValue(); |
||
62 | }); |
||
63 | |||
64 | foreach ($argumentDefinitions as $argumentDefinition) { |
||
65 | $argumentName = $argumentDefinition->getName(); |
||
66 | $argumentType = $argumentDefinition->getType(); |
||
67 | /** @var ArgumentNode $argumentNode */ |
||
68 | $argumentNode = $argumentNodeMap[$argumentName]; |
||
69 | $defaultValue = $argumentDefinition->getDefaultValue(); |
||
70 | |||
71 | if (null === $argumentNode) { |
||
72 | if (null === $defaultValue) { |
||
73 | $coercedValues[$argumentName] = $defaultValue; |
||
74 | } elseif (!$argumentType instanceof NonNullType) { |
||
75 | throw new ExecutionException( |
||
76 | sprintf('Argument "%s" of required type "%s" was not provided.', $argumentName, $argumentType), |
||
77 | [$node] |
||
78 | ); |
||
79 | } |
||
80 | } elseif ($argumentNode instanceof VariableNode) { |
||
81 | $coercedValues[$argumentName] = $this->coerceValueForVariableNode( |
||
82 | $argumentNode, |
||
83 | $argumentType, |
||
84 | $argumentName, |
||
85 | $variableValues, |
||
86 | $defaultValue |
||
87 | ); |
||
88 | } else { |
||
89 | $coercedValue = null; |
||
|
|||
90 | |||
91 | try { |
||
92 | $coercedValue = valueFromAST($argumentNode->getValue(), $argumentType, $variableValues); |
||
93 | } catch (CoercingException $ex) { |
||
94 | // Value nodes that cannot be resolved should be treated as invalid values |
||
95 | // therefore we catch the exception and leave the `$coercedValue` as `null`. |
||
96 | } |
||
97 | |||
98 | if (null === $coercedValue) { |
||
99 | // Note: ValuesOfCorrectType validation should catch this before |
||
100 | // execution. This is a runtime check to ensure execution does not |
||
101 | // continue with an invalid argument value. |
||
102 | throw new ExecutionException( |
||
103 | sprintf('Argument "%s" has invalid value %s.', $argumentName, $argumentNode), |
||
104 | [$argumentNode->getValue()] |
||
105 | ); |
||
106 | } |
||
107 | |||
108 | $coercedValues[$argumentName] = $coercedValue; |
||
109 | } |
||
110 | } |
||
111 | |||
112 | return $coercedValues; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Prepares an object map of argument values given a directive definition |
||
117 | * and a AST node which may contain directives. Optionally also accepts a map |
||
118 | * of variable values. |
||
119 | * |
||
120 | * If the directive does not exist on the node, returns null. |
||
121 | * |
||
122 | * @param Directive $directive |
||
123 | * @param mixed $node |
||
124 | * @param array $variableValues |
||
125 | * @return array|null |
||
126 | * @throws ExecutionException |
||
127 | * @throws InvalidTypeException |
||
128 | * @throws InvariantException |
||
129 | */ |
||
130 | public function coerceDirectiveValues( |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Prepares an object map of variableValues of the correct type based on the |
||
149 | * provided variable definitions and arbitrary input. If the input cannot be |
||
150 | * parsed to match the variable definitions, a GraphQLError will be thrown. |
||
151 | * |
||
152 | * @param Schema $schema |
||
153 | * @param array|VariableDefinitionNode[] $variableDefinitionNodes |
||
154 | * @param $input |
||
155 | * @return CoercedValue |
||
156 | * @throws \Exception |
||
157 | */ |
||
158 | public function coerceVariableValues(Schema $schema, array $variableDefinitionNodes, array $inputs): CoercedValue |
||
159 | { |
||
160 | $coercedValues = []; |
||
161 | $errors = []; |
||
162 | |||
163 | foreach ($variableDefinitionNodes as $variableDefinitionNode) { |
||
164 | $variableName = $variableDefinitionNode->getVariable()->getNameValue(); |
||
165 | $variableType = typeFromAST($schema, $variableDefinitionNode->getType()); |
||
166 | |||
167 | $type = $variableType; |
||
168 | if ($variableType instanceof WrappingTypeInterface) { |
||
169 | $type = $variableType->getOfType(); |
||
170 | } |
||
171 | |||
172 | if (!$type instanceof InputTypeInterface) { |
||
173 | $errors[] = new GraphQLException( |
||
174 | sprintf( |
||
175 | 'Variable "%s" expected value of type %s which cannot be used as an input type.', |
||
176 | $variableName, |
||
177 | $variableDefinitionNode->getType()->getName() |
||
178 | ), |
||
179 | [$variableDefinitionNode->getType()] |
||
180 | ); |
||
181 | } else { |
||
182 | if (!isset($inputs[$variableName])) { |
||
183 | if ($variableType instanceof NonNullType) { |
||
184 | throw new GraphQLException('NonNullType'); |
||
185 | } elseif ($variableDefinitionNode->getDefaultValue() !== null) { |
||
186 | $coercedValues[$variableName] = valueFromAST( |
||
187 | $variableDefinitionNode->getDefaultValue(), |
||
188 | $variableType |
||
189 | ); |
||
190 | } |
||
191 | } else { |
||
192 | $value = $inputs[$variableName]; |
||
193 | $coercedValue = $this->coerceValue($value, $variableType, $variableDefinitionNode); |
||
194 | if ($coercedValue->hasErrors()) { |
||
195 | $messagePrelude = sprintf( |
||
196 | 'Variable "$%s" got invalid value %s', |
||
197 | $variableName, json_encode |
||
198 | ($value) |
||
199 | ); |
||
200 | foreach ($coercedValue->getErrors() as $error) { |
||
201 | $errors[] = $this->buildCoerceException( |
||
202 | $messagePrelude, |
||
203 | $variableDefinitionNode, |
||
204 | [], |
||
205 | $error->getMessage(), |
||
206 | $error |
||
207 | ); |
||
208 | } |
||
209 | } else { |
||
210 | $coercedValues[$variableName] = $coercedValue->getValue(); |
||
211 | } |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | |||
216 | return new CoercedValue($coercedValues, $errors); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Returns either a value which is valid for the provided type or a list of |
||
221 | * encountered coercion errors. |
||
222 | * |
||
223 | * @param mixed|array $value |
||
224 | * @param $type |
||
225 | * @param $blameNode |
||
226 | * @param array $path |
||
227 | * @return CoercedValue |
||
228 | * @throws InvariantException |
||
229 | * @throws GraphQLException |
||
230 | */ |
||
231 | private function coerceValue($value, $type, $blameNode, ?array $path = null): CoercedValue |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @param $value |
||
262 | * @param NonNullType $type |
||
263 | * @param NodeInterface $blameNode |
||
264 | * @param array|null $path |
||
265 | * @return CoercedValue |
||
266 | * @throws GraphQLException |
||
267 | * @throws InvariantException |
||
268 | */ |
||
269 | protected function coerceValueForNonNullType( |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Scalars determine if a value is valid via parseValue(), which can |
||
289 | * throw to indicate failure. If it throws, maintain a reference to |
||
290 | * the original error. |
||
291 | * |
||
292 | * @param $value |
||
293 | * @param ScalarType $type |
||
294 | * @param NodeInterface $blameNode |
||
295 | * @param array|null $path |
||
296 | * @return CoercedValue |
||
297 | */ |
||
298 | protected function coerceValueForScalarType( |
||
299 | $value, |
||
300 | ScalarType $type, |
||
301 | NodeInterface $blameNode, |
||
302 | ?array $path |
||
303 | ): CoercedValue { |
||
304 | try { |
||
305 | $parseResult = $type->parseValue($value); |
||
306 | if (empty($parseResult)) { |
||
307 | return new CoercedValue(null, [ |
||
308 | new GraphQLException(sprintf('Expected type %s', $type->getName())) |
||
309 | ]); |
||
310 | } |
||
311 | return new CoercedValue($parseResult, null); |
||
312 | } catch (\Exception $ex) { |
||
313 | return new CoercedValue(null, [ |
||
314 | $this->buildCoerceException( |
||
315 | sprintf('Expected type %s', $type->getName()), |
||
316 | $blameNode, |
||
317 | $path, |
||
318 | $ex->getMessage(), |
||
319 | $ex |
||
320 | ) |
||
321 | ]); |
||
322 | } |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * @param $value |
||
327 | * @param EnumType $type |
||
328 | * @param NodeInterface $blameNode |
||
329 | * @param array|null $path |
||
330 | * @return CoercedValue |
||
331 | * @throws InvariantException |
||
332 | */ |
||
333 | protected function coerceValueForEnumType( |
||
334 | $value, |
||
335 | EnumType $type, |
||
336 | NodeInterface $blameNode, |
||
337 | ?array $path |
||
338 | ): CoercedValue { |
||
339 | if (is_string($value)) { |
||
340 | $enumValue = $type->getValue($value); |
||
341 | if ($enumValue !== null) { |
||
342 | return new CoercedValue($enumValue, null); |
||
343 | } |
||
344 | } |
||
345 | |||
346 | $suggestions = suggestionList((string)$value, array_map(function (EnumValue $enumValue) { |
||
347 | return $enumValue->getName(); |
||
348 | }, $type->getValues())); |
||
349 | |||
350 | $didYouMean = (!empty($suggestions)) |
||
351 | ? 'did you mean' . implode(',', $suggestions) |
||
352 | : null; |
||
353 | |||
354 | return new CoercedValue(null, [ |
||
355 | $this->buildCoerceException(sprintf('Expected type %s', $type->getName()), $blameNode, $path, $didYouMean) |
||
356 | ]); |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * @param $value |
||
361 | * @param ListType $type |
||
362 | * @param NodeInterface $blameNode |
||
363 | * @param array|null $path |
||
364 | * @return CoercedValue |
||
365 | * @throws GraphQLException |
||
366 | * @throws InvariantException |
||
367 | */ |
||
368 | protected function coerceValueForInputObjectType( |
||
369 | $value, |
||
370 | InputObjectType $type, |
||
371 | NodeInterface $blameNode, |
||
372 | ?array $path |
||
373 | ): CoercedValue { |
||
374 | $errors = []; |
||
375 | $coercedValues = []; |
||
376 | $fields = $type->getFields(); |
||
377 | |||
378 | // Ensure every defined field is valid. |
||
379 | foreach ($fields as $field) { |
||
380 | if (!array_key_exists($field->getName(), $value)) { |
||
381 | if (!empty($field->getDefaultValue())) { |
||
382 | $coercedValue[$field->getName()] = $field->getDefaultValue(); |
||
383 | } elseif ($type instanceof NonNullType) { |
||
384 | $errors[] = new GraphQLException( |
||
385 | sprintf( |
||
386 | "Field %s of required type %s was not provided", |
||
387 | implode(",", array_merge($path, [$field->getName()])), |
||
388 | $type->getName() |
||
389 | ) |
||
390 | ); |
||
391 | } |
||
392 | } else { |
||
393 | $fieldValue = $value[$field->getName()]; |
||
394 | $coercedValue = $this->coerceValue( |
||
395 | $fieldValue, |
||
396 | $field->getType(), |
||
397 | $blameNode, |
||
398 | [$path, $field->getName()] // new path |
||
399 | ); |
||
400 | |||
401 | if ($coercedValue->hasErrors()) { |
||
402 | $errors = array_merge($errors, $coercedValue->getErrors()); |
||
403 | } elseif (empty($errors)) { |
||
404 | $coercedValues[$field->getName()] = $coercedValue->getValue(); |
||
405 | } |
||
406 | } |
||
407 | } |
||
408 | |||
409 | // Ensure every provided field is defined. |
||
410 | foreach ($value as $fieldName => $fieldValue) { |
||
411 | if ($fields[$fieldName] === null) { |
||
412 | $suggestions = suggestionList($fieldName, array_keys($fields)); |
||
413 | $didYouMean = (!empty($suggestions)) |
||
414 | ? 'did you mean' . implode(',', $suggestions) |
||
415 | : null; |
||
416 | |||
417 | $errors[] = $this->buildCoerceException( |
||
418 | sprintf('Field "%s" is not defined by type %s', $fieldName, $type->getName()), |
||
419 | $blameNode, |
||
420 | $path, |
||
421 | $didYouMean |
||
422 | ); |
||
423 | } |
||
424 | } |
||
425 | |||
426 | return new CoercedValue($coercedValues, $errors); |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * @param $value |
||
431 | * @param $type |
||
432 | * @return CoercedValue |
||
433 | * @throws GraphQLException |
||
434 | * @throws InvariantException |
||
435 | */ |
||
436 | protected function coerceValueForListType( |
||
464 | } |
||
465 | |||
466 | /** |
||
467 | * @param string $message |
||
468 | * @param NodeInterface $blameNode |
||
469 | * @param array|null $path |
||
470 | * @param null|string $subMessage |
||
471 | * @param GraphQLException|null $origin$originalException |
||
472 | * @return GraphQLException |
||
473 | */ |
||
474 | protected function buildCoerceException( |
||
492 | ); |
||
493 | } |
||
494 | |||
495 | /** |
||
496 | * @param VariableNode $variableNode |
||
497 | * @param TypeInterface $argumentType |
||
498 | * @param string $argumentName |
||
499 | * @param array $variableValues |
||
500 | * @param mixed $defaultValue |
||
501 | * @return mixed |
||
502 | * @throws ExecutionException |
||
503 | */ |
||
504 | protected function coerceValueForVariableNode( |
||
533 | ); |
||
534 | } |
||
535 | } |
||
536 | } |
||
537 |