Completed
Pull Request — master (#102)
by Christoffer
04:14 queued 01:07
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
<?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
    public function enterNode(NodeInterface $node): ?NodeInterface
25
    {
26
        if ($node instanceof SelectionSetNode || $node instanceof FragmentDefinitionNode) {
27
            return null;
28
        }
29
30
        if ($node instanceof VariableDefinitionNode) {
31
            $variable     = $node->getVariable();
32
            $variableName = $variable->getNameValue();
33
            $defaultValue = $node->getDefaultValue();
34
            $type         = $this->validationContext->getInputType();
35
36
            if (null !== $defaultValue && $type instanceof NonNullType) {
37
                $this->validationContext->reportError(
38
                    new ValidationException(
39
                        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

39
                        variableDefaultValueNotAllowedMessage($variableName, $type, /** @scrutinizer ignore-type */ $type->getOfType()),
Loading history...
40
                        [$defaultValue]
41
                    )
42
                );
43
            }
44
45
            return null; // do not traverse further.
46
        }
47
48
        return $node;
49
    }
50
}
51