| Conditions | 16 |
| Paths | 20 |
| Total Lines | 75 |
| Code Lines | 34 |
| 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 |
||
| 23 | function valueFromAST(ValueNodeInterface $node, InputTypeInterface $type, array $variables) |
||
| 24 | { |
||
| 25 | if ($type instanceof NonNullType) { |
||
| 26 | if ($node instanceof NullValueNode) { |
||
| 27 | // Invalid: intentionally return no value. |
||
| 28 | return; |
||
| 29 | } |
||
| 30 | |||
| 31 | return valueFromAST($node, $type->getOfType(), $variables); |
||
|
|
|||
| 32 | } |
||
| 33 | |||
| 34 | if ($node instanceof NullValueNode) { |
||
| 35 | return null; |
||
| 36 | } |
||
| 37 | |||
| 38 | if ($node instanceof VariableNode) { |
||
| 39 | $variableName = $node->getNameValue(); |
||
| 40 | |||
| 41 | if (!isset($variables[$variableName])) { |
||
| 42 | // No valid return value. |
||
| 43 | return; |
||
| 44 | } |
||
| 45 | |||
| 46 | // Note: we're not doing any checking that this variable is correct. We're |
||
| 47 | // assuming that this query has been validated and the variable usage here |
||
| 48 | // is of the correct type. |
||
| 49 | return $variables[$variableName]; |
||
| 50 | } |
||
| 51 | |||
| 52 | if ($type instanceof ListType) { |
||
| 53 | $itemType = $type->getOfType(); |
||
| 54 | |||
| 55 | if ($node instanceof ListValueNode) { |
||
| 56 | $coercedValues = []; |
||
| 57 | |||
| 58 | foreach ($node->getValues() as $value) { |
||
| 59 | if (isMissingVariable($value, $variables)) { |
||
| 60 | // If an array contains a missing variable, it is either coerced to |
||
| 61 | // null or if the item type is non-null, it considered invalid. |
||
| 62 | if ($itemType instanceof NonNullType) { |
||
| 63 | return; // Invalid: intentionally return no value. |
||
| 64 | } |
||
| 65 | $coercedValues[] = null; |
||
| 66 | } else { |
||
| 67 | $itemValue = valueFromAST($value, $itemType, $variables); |
||
| 68 | if (!$itemValue) { |
||
| 69 | return; // Invalid: intentionally return no value. |
||
| 70 | } |
||
| 71 | $coercedValues[] = $itemValue; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | return $coercedValues; |
||
| 76 | } |
||
| 77 | |||
| 78 | $coercedValue = valueFromAST($node, $itemType, $variables); |
||
| 79 | if (!$coercedValue) { |
||
| 80 | return; // Invalid: intentionally return no value. |
||
| 81 | } |
||
| 82 | return [$coercedValue]; |
||
| 83 | } |
||
| 84 | |||
| 85 | if ($type instanceof InputObjectType) { |
||
| 86 | // TODO: |
||
| 87 | } |
||
| 88 | |||
| 89 | if ($type instanceof EnumType) { |
||
| 90 | // TODO: |
||
| 91 | } |
||
| 92 | |||
| 93 | if ($type instanceof ScalarType) { |
||
| 94 | // TODO: |
||
| 95 | } |
||
| 96 | |||
| 97 | throw new \Exception(sprintf('Unknown type: %s', $type)); |
||
| 98 | } |
||
| 109 |