Failed Conditions
Push — master ( 48b44f...392b56 )
by Vladimir
04:42
created

VariablesInAllowedPosition   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 70.59%

Importance

Changes 0
Metric Value
wmc 17
eloc 34
dl 0
loc 76
ccs 24
cts 34
cp 0.7059
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A badVarPosMessage() 0 4 1
A effectiveType() 0 3 3
A varTypeAllowedForType() 0 15 6
B getVisitor() 0 37 7
1
<?php
2
namespace GraphQL\Validator\Rules;
3
4
use GraphQL\Error\Error;
5
use GraphQL\Language\AST\NodeKind;
6
use GraphQL\Language\AST\OperationDefinitionNode;
7
use GraphQL\Language\AST\VariableDefinitionNode;
8
use GraphQL\Type\Definition\ListOfType;
9
use GraphQL\Type\Definition\NonNull;
10
use GraphQL\Utils\TypeComparators;
11
use GraphQL\Utils\TypeInfo;
12
use GraphQL\Validator\ValidationContext;
13
14
class VariablesInAllowedPosition extends AbstractValidationRule
15
{
16 9
    static function badVarPosMessage($varName, $varType, $expectedType)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
17
    {
18 9
        return "Variable \"\$$varName\" of type \"$varType\" used in position expecting ".
19 9
        "type \"$expectedType\".";
20
    }
21
22
    public $varDefMap;
23
24 121
    public function getVisitor(ValidationContext $context)
25
    {
26
        return [
27
            NodeKind::OPERATION_DEFINITION => [
28
                'enter' => function () {
29 121
                    $this->varDefMap = [];
30 121
                },
31
                'leave' => function(OperationDefinitionNode $operation) use ($context) {
32 121
                    $usages = $context->getRecursiveVariableUsages($operation);
33
34 121
                    foreach ($usages as $usage) {
35 31
                        $node = $usage['node'];
36 31
                        $type = $usage['type'];
37 31
                        $varName = $node->name->value;
38 31
                        $varDef = isset($this->varDefMap[$varName]) ? $this->varDefMap[$varName] : null;
39
40 31
                        if ($varDef && $type) {
41
                            // A var type is allowed if it is the same or more strict (e.g. is
42
                            // a subtype of) than the expected type. It can be more strict if
43
                            // the variable type is non-null when the expected type is nullable.
44
                            // If both are list types, the variable item type can be more strict
45
                            // than the expected item type (contravariant).
46 29
                            $schema = $context->getSchema();
47 29
                            $varType = TypeInfo::typeFromAST($schema, $varDef->type);
48
49 29
                            if ($varType && !TypeComparators::isTypeSubTypeOf($schema, $this->effectiveType($varType, $varDef), $type)) {
50 9
                                $context->reportError(new Error(
51 9
                                    self::badVarPosMessage($varName, $varType, $type),
52 31
                                    [$varDef, $node]
53
                                ));
54
                            }
55
                        }
56
                    }
57 121
                }
58
            ],
59
            NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $varDefNode) {
60 31
                $this->varDefMap[$varDefNode->variable->name->value] = $varDefNode;
61 121
            }
62
        ];
63
    }
64
65
    // A var type is allowed if it is the same or more strict than the expected
66
    // type. It can be more strict if the variable type is non-null when the
67
    // expected type is nullable. If both are list types, the variable item type can
68
    // be more strict than the expected item type.
69
    private function varTypeAllowedForType($varType, $expectedType)
70
    {
71
        if ($expectedType instanceof NonNull) {
72
            if ($varType instanceof NonNull) {
73
                return $this->varTypeAllowedForType($varType->getWrappedType(), $expectedType->getWrappedType());
74
            }
75
            return false;
76
        }
77
        if ($varType instanceof NonNull) {
78
            return $this->varTypeAllowedForType($varType->getWrappedType(), $expectedType);
79
        }
80
        if ($varType instanceof ListOfType && $expectedType instanceof ListOfType) {
81
            return $this->varTypeAllowedForType($varType->getWrappedType(), $expectedType->getWrappedType());
82
        }
83
        return $varType === $expectedType;
84
    }
85
86
    // If a variable definition has a default value, it's effectively non-null.
87 29
    private function effectiveType($varType, $varDef)
88
    {
89 29
        return (!$varDef->defaultValue || $varType instanceof NonNull) ? $varType : new NonNull($varType);
90
    }
91
92
}
93