GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#1)
by Šimon
04:15
created

VariablesInAllowedPosition::badVarPosMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
crap 1
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 121
            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