Conditions | 24 |
Paths | 149 |
Total Lines | 135 |
Code Lines | 85 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
219 | private function coerceValue($value, $type, $blameNode, ?array $path = []) |
||
220 | { |
||
221 | if ($type instanceof NonNullType) { |
||
222 | if (empty($value)) { |
||
223 | throw new GraphQLException( |
||
224 | sprintf('Expected non-nullable type %s not to be null', (string)$type), |
||
225 | $blameNode, |
||
226 | $path |
||
227 | ); |
||
228 | } |
||
229 | return $this->coerceValue($value, $type->getOfType(), $blameNode, $path); |
||
230 | } |
||
231 | |||
232 | if (empty($value)) { |
||
233 | return ['value' => null, 'errors' => null]; |
||
234 | } |
||
235 | |||
236 | if ($type instanceof ScalarType) { |
||
237 | try { |
||
238 | $parseResult = $type->parseValue($value); |
||
239 | if (empty($parseResult)) { |
||
240 | return [ |
||
241 | 'errors' => new GraphQLException(sprintf('Expected type %s', $type->getName())), |
||
242 | 'coerced' => null |
||
243 | ]; |
||
244 | } |
||
245 | return [ |
||
246 | 'errors' => null, |
||
247 | 'coerced' => $parseResult |
||
248 | ]; |
||
249 | } catch (\Exception $ex) { |
||
250 | return [ |
||
251 | 'errors' => new GraphQLException(sprintf('Expected type %s', $type->getName())), |
||
252 | 'coerced' => null |
||
253 | ]; |
||
254 | } |
||
255 | } |
||
256 | |||
257 | if ($type instanceof EnumType) { |
||
258 | if (is_string($value)) { |
||
259 | $enumValue = $type->getValue($value); |
||
260 | if ($enumValue !== null) { |
||
261 | return [ |
||
262 | 'value' => $enumValue, |
||
263 | 'errors' => null |
||
264 | ]; |
||
265 | } |
||
266 | } |
||
267 | $suggestions = suggestionList((string)$value, array_map(function (EnumValue $enumValue) { |
||
268 | return $enumValue->getName(); |
||
269 | }, $type->getValues())); |
||
270 | |||
271 | //@TODO throw proper error |
||
272 | } |
||
273 | |||
274 | if ($type instanceof ListType) { |
||
275 | $itemType = $type->getOfType(); |
||
276 | if (is_array($value) || $value instanceof \Traversable) { |
||
277 | $errors = []; |
||
278 | $coercedValue = []; |
||
279 | foreach ($value as $index => $itemValue) { |
||
280 | $coercedItem = $this->coerceValue( |
||
281 | $itemValue, |
||
282 | $itemType, |
||
283 | $blameNode, |
||
284 | [$path, $index] |
||
285 | ); |
||
286 | |||
287 | if (!empty($coercedItem['errors'])) { |
||
288 | $errors = array_merge($errors, $coercedItem['errors']); |
||
289 | } else { |
||
290 | $coercedValue[] = $coercedItem['coerced']; |
||
291 | } |
||
292 | } |
||
293 | |||
294 | return [ |
||
295 | 'errors' => $errors ?? [], |
||
296 | 'coerced' => $coercedValue ?? [] |
||
297 | ]; |
||
298 | } |
||
299 | } |
||
300 | |||
301 | if ($type instanceof InputObjectType) { |
||
302 | $errors = []; |
||
303 | $coercedValue = []; |
||
304 | $fields = $type->getFields(); |
||
305 | |||
306 | // Ensure every defined field is valid. |
||
307 | foreach ($fields as $field) { |
||
308 | if (!isset($value[$field->getName()])) { |
||
309 | if (!empty($field->getDefaultValue())) { |
||
310 | $coercedValue[$field->getName()] = $field->getDefaultValue(); |
||
311 | } elseif ($type instanceof NonNullType) { |
||
312 | $errors[] = new GraphQLException( |
||
313 | sprintf( |
||
314 | "Field %s of required type %s was not provided", |
||
315 | implode(",", array_merge($path, [$field->getName()])), |
||
316 | $type->getName() |
||
317 | ) |
||
318 | ); |
||
319 | } |
||
320 | } else { |
||
321 | $fieldValue = $value[$field->getName()]; |
||
322 | $coercedField = $this->coerceValue( |
||
323 | $fieldValue, |
||
324 | $field->getType(), |
||
325 | $blameNode, |
||
326 | [$path, $field->getName()] // new path |
||
327 | ); |
||
328 | |||
329 | if ($coercedField['errors']) { |
||
330 | $errors = array_merge($errors, $coercedField['errors']); |
||
331 | } elseif (empty($errors)) { |
||
332 | $coercedValue[$field->getName()] = $coercedField['coerced']; |
||
333 | } |
||
334 | } |
||
335 | } |
||
336 | |||
337 | // Ensure every provided field is defined. |
||
338 | foreach ($value as $fieldName => $fieldValue) { |
||
339 | if ($fields[$fieldName] === null) { |
||
340 | $suggestion = suggestionList($fieldName, array_keys($fields)); |
||
341 | $errors[] = new GraphQLException( |
||
342 | sprintf('Field "%s" is not defined by type %s', $fieldName, $type->getName()) |
||
343 | ); |
||
344 | } |
||
345 | } |
||
346 | |||
347 | return [ |
||
348 | 'errors' => $errors ?? [], |
||
349 | 'coerced' => $coercedValue ?? [] |
||
350 | ]; |
||
351 | } |
||
352 | |||
353 | throw new GraphQLException('Unexpected type.'); |
||
354 | } |
||
398 |