Total Complexity | 62 |
Total Lines | 508 |
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 | // because there is no undefined value in PHP so that we throw an exception |
||
96 | |||
97 | throw new ExecutionException( |
||
98 | sprintf('Argument "%s" has invalid value %s.', $argumentName, $argumentNode), |
||
99 | [$argumentNode->getValue()], |
||
100 | null, null, null, $ex |
||
101 | ); |
||
102 | } |
||
103 | |||
104 | $coercedValues[$argumentName] = $coercedValue; |
||
105 | } |
||
106 | } |
||
107 | |||
108 | return $coercedValues; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Prepares an object map of argument values given a directive definition |
||
113 | * and a AST node which may contain directives. Optionally also accepts a map |
||
114 | * of variable values. |
||
115 | * |
||
116 | * If the directive does not exist on the node, returns null. |
||
117 | * |
||
118 | * @param Directive $directive |
||
119 | * @param mixed $node |
||
120 | * @param array $variableValues |
||
121 | * @return array|null |
||
122 | * @throws ExecutionException |
||
123 | * @throws InvalidTypeException |
||
124 | * @throws InvariantException |
||
125 | */ |
||
126 | public function coerceDirectiveValues( |
||
127 | Directive $directive, |
||
128 | $node, |
||
129 | array $variableValues = [] |
||
130 | ): ?array { |
||
131 | $directiveNode = $node->hasDirectives() |
||
132 | ? find($node->getDirectives(), function (NameAwareInterface $value) use ($directive) { |
||
133 | return $value->getNameValue() === $directive->getName(); |
||
134 | }) : null; |
||
135 | |||
136 | if (null !== $directiveNode) { |
||
137 | return $this->coerceArgumentValues($directive, $directiveNode, $variableValues); |
||
138 | } |
||
139 | |||
140 | return null; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Prepares an object map of variableValues of the correct type based on the |
||
145 | * provided variable definitions and arbitrary input. If the input cannot be |
||
146 | * parsed to match the variable definitions, a GraphQLError will be thrown. |
||
147 | * |
||
148 | * @param Schema $schema |
||
149 | * @param array|VariableDefinitionNode[] $variableDefinitionNodes |
||
150 | * @param $input |
||
151 | * @return CoercedValue |
||
152 | * @throws \Exception |
||
153 | */ |
||
154 | public function coerceVariableValues(Schema $schema, array $variableDefinitionNodes, array $inputs): CoercedValue |
||
155 | { |
||
156 | $coercedValues = []; |
||
157 | $errors = []; |
||
158 | |||
159 | foreach ($variableDefinitionNodes as $variableDefinitionNode) { |
||
160 | $variableName = $variableDefinitionNode->getVariable()->getNameValue(); |
||
161 | $variableType = typeFromAST($schema, $variableDefinitionNode->getType()); |
||
162 | |||
163 | $type = $variableType; |
||
164 | if ($variableType instanceof WrappingTypeInterface) { |
||
165 | $type = $variableType->getOfType(); |
||
166 | } |
||
167 | |||
168 | if (!$type instanceof InputTypeInterface) { |
||
169 | $errors[] = new GraphQLException( |
||
170 | sprintf( |
||
171 | 'Variable "%s" expected value of type %s which cannot be used as an input type.', |
||
172 | $variableName, |
||
173 | $variableDefinitionNode->getType()->getName() |
||
174 | ), |
||
175 | [$variableDefinitionNode->getType()] |
||
176 | ); |
||
177 | } else { |
||
178 | if (!isset($inputs[$variableName])) { |
||
179 | if ($variableType instanceof NonNullType) { |
||
180 | throw new GraphQLException('NonNullType'); |
||
181 | } elseif ($variableDefinitionNode->getDefaultValue() !== null) { |
||
182 | $coercedValues[$variableName] = valueFromAST( |
||
183 | $variableDefinitionNode->getDefaultValue(), |
||
184 | $variableType |
||
185 | ); |
||
186 | } |
||
187 | } else { |
||
188 | $value = $inputs[$variableName]; |
||
189 | $coercedValue = $this->coerceValue($value, $variableType, $variableDefinitionNode); |
||
190 | if ($coercedValue->hasErrors()) { |
||
191 | $messagePrelude = sprintf( |
||
192 | 'Variable "$%s" got invalid value %s', |
||
193 | $variableName, json_encode |
||
194 | ($value) |
||
195 | ); |
||
196 | foreach ($coercedValue->getErrors() as $error) { |
||
197 | $errors[] = $this->buildCoerceException( |
||
198 | $messagePrelude, |
||
199 | $variableDefinitionNode, |
||
200 | [], |
||
201 | $error->getMessage(), |
||
202 | $error |
||
203 | ); |
||
204 | } |
||
205 | } else { |
||
206 | $coercedValues[$variableName] = $coercedValue->getValue(); |
||
207 | } |
||
208 | } |
||
209 | } |
||
210 | } |
||
211 | |||
212 | return new CoercedValue($coercedValues, $errors); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Returns either a value which is valid for the provided type or a list of |
||
217 | * encountered coercion errors. |
||
218 | * |
||
219 | * @param mixed|array $value |
||
220 | * @param $type |
||
221 | * @param $blameNode |
||
222 | * @param array $path |
||
223 | * @return CoercedValue |
||
224 | * @throws InvariantException |
||
225 | * @throws GraphQLException |
||
226 | */ |
||
227 | private function coerceValue($value, $type, $blameNode, ?array $path = null): CoercedValue |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * @param $value |
||
258 | * @param NonNullType $type |
||
259 | * @param NodeInterface $blameNode |
||
260 | * @param array|null $path |
||
261 | * @return CoercedValue |
||
262 | * @throws GraphQLException |
||
263 | * @throws InvariantException |
||
264 | */ |
||
265 | protected function coerceValueForNonNullType( |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Scalars determine if a value is valid via parseValue(), which can |
||
285 | * throw to indicate failure. If it throws, maintain a reference to |
||
286 | * the original error. |
||
287 | * |
||
288 | * @param $value |
||
289 | * @param ScalarType $type |
||
290 | * @param NodeInterface $blameNode |
||
291 | * @param array|null $path |
||
292 | * @return CoercedValue |
||
293 | */ |
||
294 | protected function coerceValueForScalarType( |
||
295 | $value, |
||
296 | ScalarType $type, |
||
297 | NodeInterface $blameNode, |
||
298 | ?array $path |
||
299 | ): CoercedValue { |
||
300 | try { |
||
301 | $parseResult = $type->parseValue($value); |
||
302 | if (empty($parseResult)) { |
||
303 | return new CoercedValue(null, [ |
||
304 | new GraphQLException(sprintf('Expected type %s', $type->getName())) |
||
305 | ]); |
||
306 | } |
||
307 | return new CoercedValue($parseResult, null); |
||
308 | } catch (\Exception $ex) { |
||
309 | return new CoercedValue(null, [ |
||
310 | $this->buildCoerceException( |
||
311 | sprintf('Expected type %s', $type->getName()), |
||
312 | $blameNode, |
||
313 | $path, |
||
314 | $ex->getMessage(), |
||
315 | $ex |
||
316 | ) |
||
317 | ]); |
||
318 | } |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * @param $value |
||
323 | * @param EnumType $type |
||
324 | * @param NodeInterface $blameNode |
||
325 | * @param array|null $path |
||
326 | * @return CoercedValue |
||
327 | * @throws InvariantException |
||
328 | */ |
||
329 | protected function coerceValueForEnumType( |
||
352 | ]); |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @param $value |
||
357 | * @param ListType $type |
||
358 | * @param NodeInterface $blameNode |
||
359 | * @param array|null $path |
||
360 | * @return CoercedValue |
||
361 | * @throws GraphQLException |
||
362 | * @throws InvariantException |
||
363 | */ |
||
364 | protected function coerceValueForInputObjectType( |
||
365 | $value, |
||
366 | InputObjectType $type, |
||
367 | NodeInterface $blameNode, |
||
368 | ?array $path |
||
369 | ): CoercedValue { |
||
370 | $errors = []; |
||
371 | $coercedValues = []; |
||
372 | $fields = $type->getFields(); |
||
373 | |||
374 | // Ensure every defined field is valid. |
||
375 | foreach ($fields as $field) { |
||
376 | if (empty($value[$field->getName()])) { |
||
377 | if (!empty($field->getDefaultValue())) { |
||
378 | $coercedValue[$field->getName()] = $field->getDefaultValue(); |
||
379 | } elseif ($field->getType() instanceof NonNullType) { |
||
380 | $errors[] = new GraphQLException( |
||
381 | sprintf( |
||
382 | "Field %s of required type %s! was not provided.", |
||
383 | $this->printPath(array_merge($path ?? [], [$field->getName()])), |
||
384 | $field->getType()->getOfType() |
||
385 | ) |
||
386 | ); |
||
387 | } |
||
388 | } else { |
||
389 | $fieldValue = $value[$field->getName()]; |
||
390 | $coercedValue = $this->coerceValue( |
||
391 | $fieldValue, |
||
392 | $field->getType(), |
||
393 | $blameNode, |
||
394 | [$path, $field->getName()] // new path |
||
395 | ); |
||
396 | |||
397 | if ($coercedValue->hasErrors()) { |
||
398 | $errors = array_merge($errors, $coercedValue->getErrors()); |
||
399 | } elseif (empty($errors)) { |
||
400 | $coercedValues[$field->getName()] = $coercedValue->getValue(); |
||
401 | } |
||
402 | } |
||
403 | } |
||
404 | |||
405 | // Ensure every provided field is defined. |
||
406 | foreach ($value as $fieldName => $fieldValue) { |
||
407 | if (!isset($fields[$fieldName])) { |
||
408 | $suggestions = suggestionList($fieldName, array_keys($fields)); |
||
409 | $didYouMean = (!empty($suggestions)) |
||
410 | ? 'did you mean' . implode(',', $suggestions) |
||
411 | : null; |
||
412 | |||
413 | $errors[] = $this->buildCoerceException( |
||
414 | sprintf('Field "%s" is not defined by type %s', $fieldName, $type->getName()), |
||
415 | $blameNode, |
||
416 | $path, |
||
417 | $didYouMean |
||
418 | ); |
||
419 | } |
||
420 | } |
||
421 | |||
422 | return new CoercedValue($coercedValues, $errors); |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * @param $value |
||
427 | * @param $type |
||
428 | * @return CoercedValue |
||
429 | * @throws GraphQLException |
||
430 | * @throws InvariantException |
||
431 | */ |
||
432 | protected function coerceValueForListType( |
||
433 | $value, |
||
434 | ListType $type, |
||
435 | NodeInterface $blameNode, |
||
436 | ?array $path |
||
437 | ): CoercedValue { |
||
438 | $itemType = $type->getOfType(); |
||
439 | |||
440 | if (is_array($value) || $value instanceof \Traversable) { |
||
441 | $errors = []; |
||
442 | $coercedValues = []; |
||
443 | foreach ($value as $index => $itemValue) { |
||
444 | $coercedValue = $this->coerceValue($itemValue, $itemType, $blameNode, [$path, $index]); |
||
445 | |||
446 | if ($coercedValue->hasErrors()) { |
||
447 | $errors = array_merge($errors, $coercedValue->getErrors()); |
||
448 | } else { |
||
449 | $coercedValues[] = $coercedValue->getValue(); |
||
450 | } |
||
451 | } |
||
452 | |||
453 | return new CoercedValue($coercedValues, $errors); |
||
454 | } |
||
455 | |||
456 | // Lists accept a non-list value as a list of one. |
||
457 | $coercedValue = $this->coerceValue($value, $itemType, $blameNode); |
||
458 | |||
459 | return new CoercedValue([$coercedValue->getValue()], $coercedValue->getErrors()); |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * @param string $message |
||
464 | * @param NodeInterface $blameNode |
||
465 | * @param array|null $path |
||
466 | * @param null|string $subMessage |
||
467 | * @param GraphQLException|null $origin $originalException |
||
468 | * @return GraphQLException |
||
469 | */ |
||
470 | protected function buildCoerceException( |
||
488 | ); |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * @param array|null $path |
||
493 | * @return string |
||
494 | */ |
||
495 | protected function printPath(?array $path) |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * @param VariableNode $variableNode |
||
506 | * @param TypeInterface $argumentType |
||
507 | * @param string $argumentName |
||
508 | * @param array $variableValues |
||
509 | * @param mixed $defaultValue |
||
510 | * @return mixed |
||
511 | * @throws ExecutionException |
||
512 | */ |
||
513 | protected function coerceValueForVariableNode( |
||
542 | ); |
||
543 | } |
||
544 | } |
||
545 | } |
||
546 |