Conditions | 13 |
Paths | 11 |
Total Lines | 63 |
Code Lines | 40 |
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 |
||
28 | function getArgumentValues($definition, NodeInterface $node, array $variableValues = []): array |
||
29 | { |
||
30 | $coercedValues = []; |
||
31 | $argumentDefinitions = $definition->getArguments(); |
||
32 | $argumentNodes = $node->getArguments(); |
||
|
|||
33 | |||
34 | if (empty($argumentDefinitions) || empty($argumentNodes)) { |
||
35 | return $coercedValues; |
||
36 | } |
||
37 | |||
38 | $argumentNodeMap = keyMap($argumentNodes, function (ArgumentNode $value) { |
||
39 | return $value->getNameValue(); |
||
40 | }); |
||
41 | |||
42 | foreach ($argumentDefinitions as $argumentDefinition) { |
||
43 | $name = $argumentDefinition->getName(); |
||
44 | $argumentType = $argumentDefinition->getType(); |
||
45 | /** @var ArgumentNode $argumentNode */ |
||
46 | $argumentNode = $argumentNodeMap[$name]; |
||
47 | $defaultValue = $argumentDefinition->getDefaultValue(); |
||
48 | |||
49 | if (null === $argumentNode) { |
||
50 | if (null === $defaultValue) { |
||
51 | $coercedValues[$name] = $defaultValue; |
||
52 | } elseif (!$argumentType instanceof NonNullType) { |
||
53 | throw new GraphQLError( |
||
54 | sprintf('Argument "%s" of required type "%s" was not provided.', $name, $argumentType), |
||
55 | [$node] |
||
56 | ); |
||
57 | } |
||
58 | } elseif ($argumentNode instanceof VariableNode) { |
||
59 | $variableName = $argumentNode->getNameValue(); |
||
60 | |||
61 | if (!empty($variableValues) && isset($variableValues[$variableName])) { |
||
62 | // Note: this does not check that this variable value is correct. |
||
63 | // This assumes that this query has been validated and the variable |
||
64 | // usage here is of the correct type. |
||
65 | $coercedValues[$name] = $variableValues[$variableName]; |
||
66 | } elseif (null !== $defaultValue) { |
||
67 | $coercedValues[$name] = $defaultValue; |
||
68 | } elseif ($argumentType instanceof NonNullType) { |
||
69 | throw new GraphQLError( |
||
70 | sprintf( |
||
71 | 'Argument "%s" of required type "%s" was provided the variable "%s" which was not provided a runtime value.', |
||
72 | $name, |
||
73 | $argumentType, |
||
74 | $variableName |
||
75 | ), |
||
76 | [$argumentNode->getValue()] |
||
77 | ); |
||
78 | } |
||
79 | } else { |
||
80 | $valueNode = $argumentNode->getValue(); |
||
81 | |||
82 | $coercedValue = valueFromAST($valueNode, $argumentType, $variableValues); |
||
83 | |||
84 | if (null === $coercedValue) { |
||
85 | // Note: ValuesOfCorrectType validation should catch this before |
||
86 | // execution. This is a runtime check to ensure execution does not |
||
87 | // continue with an invalid argument value. |
||
88 | throw new GraphQLError( |
||
89 | sprintf('Argument "%s" has invalid value %s.', $name, $valueNode), |
||
90 | [$argumentNode->getValue()] |
||
91 | ); |
||
118 |