Completed
Pull Request — master (#19)
by Christoffer
02:01
created

valueFromAST()   C

Complexity

Conditions 16
Paths 20

Size

Total Lines 75
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 34
nc 20
nop 3
dl 0
loc 75
rs 5.3027
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace Digia\GraphQL\Language;
4
5
use Digia\GraphQL\Language\AST\Node\Contract\ValueNodeInterface;
6
use Digia\GraphQL\Language\AST\Node\ListValueNode;
7
use Digia\GraphQL\Language\AST\Node\NullValueNode;
8
use Digia\GraphQL\Language\AST\Node\VariableNode;
9
use Digia\GraphQL\Type\Definition\Contract\InputTypeInterface;
10
use Digia\GraphQL\Type\Definition\EnumType;
11
use Digia\GraphQL\Type\Definition\InputObjectType;
12
use Digia\GraphQL\Type\Definition\ListType;
13
use Digia\GraphQL\Type\Definition\NonNullType;
14
use Digia\GraphQL\Type\Definition\ScalarType;
15
16
/**
17
 * @param ValueNodeInterface $node
18
 * @param InputTypeInterface $type
19
 * @param array              $variables
20
 * @return mixed|null
21
 * @throws \Exception
22
 */
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);
0 ignored issues
show
Bug introduced by
$type->getOfType() of type Digia\GraphQL\Type\Defin...\Contract\TypeInterface is incompatible with the type Digia\GraphQL\Type\Defin...ract\InputTypeInterface expected by parameter $type of Digia\GraphQL\Language\valueFromAST(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
        return valueFromAST($node, /** @scrutinizer ignore-type */ $type->getOfType(), $variables);
Loading history...
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));
0 ignored issues
show
Bug introduced by
$type of type Digia\GraphQL\Type\Defin...ract\InputTypeInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
    throw new \Exception(sprintf('Unknown type: %s', /** @scrutinizer ignore-type */ $type));
Loading history...
98
}
99
100
/**
101
 * @param ValueNodeInterface $node
102
 * @param array              $variables
103
 * @return bool
104
 */
105
function isMissingVariable(ValueNodeInterface $node, array $variables): bool
106
{
107
    return $node instanceof VariableNode && isset($variables[$node->getNameValue()]);
108
}
109