| Conditions | 10 |
| Paths | 10 |
| Total Lines | 64 |
| Code Lines | 38 |
| 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 |
||
| 55 | public function coerceArgumentValues($objectType, $field, array $variableValues = []): array |
||
| 56 | { |
||
| 57 | $coercedValues = []; |
||
| 58 | $argumentDefinitions = $objectType->getArguments(); |
||
| 59 | $argumentNodes = $field->getArguments(); |
||
| 60 | |||
| 61 | if (empty($argumentDefinitions) || empty($argumentNodes)) { |
||
| 62 | return $coercedValues; |
||
| 63 | } |
||
| 64 | |||
| 65 | $argumentNodeMap = keyMap($argumentNodes, function (ArgumentNode $value) { |
||
| 66 | return $value->getNameValue(); |
||
| 67 | }); |
||
| 68 | |||
| 69 | foreach ($argumentDefinitions as $argumentDefinition) { |
||
| 70 | $argumentName = $argumentDefinition->getName(); |
||
| 71 | $argumentType = $argumentDefinition->getType(); |
||
| 72 | /** @var ArgumentNode $argumentNode */ |
||
| 73 | $argumentNode = $argumentNodeMap[$argumentName]; |
||
| 74 | $defaultValue = $argumentDefinition->getDefaultValue(); |
||
| 75 | |||
| 76 | if (null === $argumentNode) { |
||
| 77 | if (null === $defaultValue) { |
||
| 78 | $coercedValues[$argumentName] = $defaultValue; |
||
| 79 | } elseif (!$argumentType instanceof NonNullType) { |
||
| 80 | throw new ExecutionException( |
||
| 81 | sprintf('Argument "%s" of required type "%s" was not provided.', $argumentName, $argumentType), |
||
| 82 | [$field] |
||
| 83 | ); |
||
| 84 | } |
||
| 85 | } elseif ($argumentNode instanceof VariableNode) { |
||
| 86 | $coercedValues[$argumentName] = $this->coerceValueForVariableNode( |
||
| 87 | $argumentNode, |
||
| 88 | $argumentType, |
||
| 89 | $argumentName, |
||
| 90 | $variableValues, |
||
| 91 | $defaultValue |
||
| 92 | ); |
||
| 93 | } else { |
||
| 94 | $coercedValue = null; |
||
|
|
|||
| 95 | |||
| 96 | try { |
||
| 97 | $coercedValue = $this->coerceValueFromAST($argumentNode->getValue(), $argumentType, |
||
| 98 | $variableValues); |
||
| 99 | } catch (CoercingException $ex) { |
||
| 100 | // Value nodes that cannot be resolved should be treated as invalid values |
||
| 101 | // therefore we catch the exception and leave the `$coercedValue` as `null`. |
||
| 102 | } |
||
| 103 | |||
| 104 | if (null === $coercedValue) { |
||
| 105 | // Note: ValuesOfCorrectType validation should catch this before |
||
| 106 | // execution. This is a runtime check to ensure execution does not |
||
| 107 | // continue with an invalid argument value. |
||
| 108 | throw new ExecutionException( |
||
| 109 | sprintf('Argument "%s" has invalid value %s.', $argumentName, $argumentNode), |
||
| 110 | [$argumentNode->getValue()] |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | |||
| 114 | $coercedValues[$argumentName] = $coercedValue; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | return $coercedValues; |
||
| 119 | } |
||
| 208 |