Completed
Pull Request — master (#110)
by Christoffer
02:25
created

VariablesDefaultValueAllowedRule::enterNode()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
cc 6
eloc 14
nc 4
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A VariablesDefaultValueAllowedRule::enterFragmentDefinition() 0 3 1
1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Error\ValidationException;
6
use Digia\GraphQL\Language\Node\FragmentDefinitionNode;
7
use Digia\GraphQL\Language\Node\NodeInterface;
8
use Digia\GraphQL\Language\Node\SelectionSetNode;
9
use Digia\GraphQL\Language\Node\VariableDefinitionNode;
10
use Digia\GraphQL\Type\Definition\NonNullType;
11
use function Digia\GraphQL\Validation\variableDefaultValueNotAllowedMessage;
12
13
/**
14
 * Variable's default value is allowed
15
 *
16
 * A GraphQL document is only valid if all variable default values are allowed
17
 * due to a variable not being required.
18
 */
19
class VariablesDefaultValueAllowedRule extends AbstractRule
20
{
21
    /**
22
     * @inheritdoc
23
     */
24
    protected function enterSelectionSet(SelectionSetNode $node): ?NodeInterface
25
    {
26
        return null;
27
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32
    protected function enterFragmentDefinition(FragmentDefinitionNode $node): ?NodeInterface
33
    {
34
        return null;
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    protected function enterVariableDefinition(VariableDefinitionNode $node): ?NodeInterface
41
    {
42
        $variable     = $node->getVariable();
43
        $variableName = $variable->getNameValue();
44
        $defaultValue = $node->getDefaultValue();
45
        $type         = $this->context->getInputType();
46
47
        if (null !== $defaultValue && $type instanceof NonNullType) {
48
            $this->context->reportError(
49
                new ValidationException(
50
                    variableDefaultValueNotAllowedMessage($variableName, $type, $type->getOfType()),
0 ignored issues
show
Bug introduced by
$type->getOfType() of type Digia\GraphQL\Type\Definition\TypeInterface is incompatible with the type string expected by parameter $guessedTypeName of Digia\GraphQL\Validation...alueNotAllowedMessage(). ( Ignorable by Annotation )

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

50
                    variableDefaultValueNotAllowedMessage($variableName, $type, /** @scrutinizer ignore-type */ $type->getOfType()),
Loading history...
51
                    [$defaultValue]
52
                )
53
            );
54
        }
55
56
        return null; // do not traverse further.
57
    }
58
}
59