| Conditions | 10 |
| Paths | 10 |
| Total Lines | 63 |
| Code Lines | 37 |
| 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 |
||
| 34 | public function getArgumentValues($definition, ArgumentsAwareInterface $node, array $variableValues = []): array |
||
| 35 | { |
||
| 36 | $coercedValues = []; |
||
| 37 | $argumentDefinitions = $definition->getArguments(); |
||
| 38 | $argumentNodes = $node->getArguments(); |
||
| 39 | |||
| 40 | if (empty($argumentDefinitions) || empty($argumentNodes)) { |
||
| 41 | return $coercedValues; |
||
| 42 | } |
||
| 43 | |||
| 44 | $argumentNodeMap = keyMap($argumentNodes, function (ArgumentNode $value) { |
||
| 45 | return $value->getNameValue(); |
||
| 46 | }); |
||
| 47 | |||
| 48 | foreach ($argumentDefinitions as $argumentDefinition) { |
||
| 49 | $argumentName = $argumentDefinition->getName(); |
||
| 50 | $argumentType = $argumentDefinition->getType(); |
||
| 51 | /** @var ArgumentNode $argumentNode */ |
||
| 52 | $argumentNode = $argumentNodeMap[$argumentName]; |
||
| 53 | $defaultValue = $argumentDefinition->getDefaultValue(); |
||
| 54 | |||
| 55 | if (null === $argumentNode) { |
||
| 56 | if (null === $defaultValue) { |
||
| 57 | $coercedValues[$argumentName] = $defaultValue; |
||
| 58 | } elseif (!$argumentType instanceof NonNullType) { |
||
| 59 | throw new ExecutionException( |
||
| 60 | sprintf('Argument "%s" of required type "%s" was not provided.', $argumentName, $argumentType), |
||
| 61 | [$node] |
||
| 62 | ); |
||
| 63 | } |
||
| 64 | } elseif ($argumentNode instanceof VariableNode) { |
||
| 65 | $coercedValues[$argumentName] = $this->coerceValueForVariableNode( |
||
| 66 | $argumentNode, |
||
| 67 | $argumentType, |
||
| 68 | $argumentName, |
||
| 69 | $variableValues, |
||
| 70 | $defaultValue |
||
| 71 | ); |
||
| 72 | } else { |
||
| 73 | $coercedValue = null; |
||
|
|
|||
| 74 | |||
| 75 | try { |
||
| 76 | $coercedValue = valueFromAST($argumentNode->getValue(), $argumentType, $variableValues); |
||
| 77 | } catch (CoercingException $ex) { |
||
| 78 | // Value nodes that cannot be resolved should be treated as invalid values |
||
| 79 | // therefore we catch the exception and leave the `$coercedValue` as `null`. |
||
| 80 | } |
||
| 81 | |||
| 82 | if (null === $coercedValue) { |
||
| 83 | // Note: ValuesOfCorrectType validation should catch this before |
||
| 84 | // execution. This is a runtime check to ensure execution does not |
||
| 85 | // continue with an invalid argument value. |
||
| 86 | throw new ExecutionException( |
||
| 87 | sprintf('Argument "%s" has invalid value %s.', $argumentName, $argumentNode), |
||
| 88 | [$argumentNode->getValue()] |
||
| 89 | ); |
||
| 90 | } |
||
| 91 | |||
| 92 | $coercedValues[$argumentName] = $coercedValue; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | return $coercedValues; |
||
| 97 | } |
||
| 167 |