Total Complexity | 50 |
Total Lines | 359 |
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( |
||
138 | Directive $directive, |
||
139 | $node, |
||
140 | array $variableValues = [] |
||
141 | ): ?array { |
||
142 | $directiveNode = $node->hasDirectives() |
||
143 | ? find($node->getDirectives(), function (NameAwareInterface $value) use ($directive) { |
||
144 | return $value->getNameValue() === $directive->getName(); |
||
145 | }) : null; |
||
146 | |||
147 | if (null !== $directiveNode) { |
||
148 | return $this->coerceArgumentValues($directive, $directiveNode, $variableValues); |
||
149 | } |
||
150 | |||
151 | return null; |
||
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 |
||
162 | { |
||
163 | $coercedValues = []; |
||
164 | $errors = []; |
||
165 | |||
166 | foreach ($variableDefinitionNodes as $variableDefinitionNode) { |
||
167 | $variableName = $variableDefinitionNode->getVariable()->getNameValue(); |
||
168 | $variableType = typeFromAST($schema, $variableDefinitionNode->getType()); |
||
169 | |||
170 | $type = $variableType; |
||
171 | if ($variableType instanceof WrappingTypeInterface) { |
||
172 | $type = $variableType->getOfType(); |
||
173 | } |
||
174 | |||
175 | //!isInputType(varType) |
||
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 | $coerced = $this->coerceValue($value, $variableType, $variableDefinitionNode); |
||
191 | if (!empty($coerced['errors'])) { |
||
192 | $messagePrelude = sprintf( |
||
193 | 'Variable "%s" got invalid value %s', |
||
194 | $variableName, json_encode |
||
195 | ($value) |
||
196 | ); |
||
197 | } else { |
||
198 | $coercedValues[$variableName] = $coerced['coerced']; |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | } |
||
203 | |||
204 | return [ |
||
205 | 'errors' => $errors ?? [], |
||
206 | 'coerced' => $coercedValues ?? [] |
||
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 = []) |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * @param VariableNode $variableNode |
||
356 | * @param TypeInterface $argumentType |
||
357 | * @param string $argumentName |
||
358 | * @param array $variableValues |
||
359 | * @param mixed $defaultValue |
||
360 | * @return mixed |
||
361 | * @throws ExecutionException |
||
362 | */ |
||
363 | protected function coerceValueForVariableNode( |
||
392 | ); |
||
393 | } |
||
394 | } |
||
396 |