Completed
Pull Request — master (#226)
by Christoffer
02:58
created

KnownArgumentNamesRule::validateField()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 2
nop 1
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Error\ValidationException;
6
use Digia\GraphQL\Language\Node\ArgumentNode;
7
use Digia\GraphQL\Language\Node\DirectiveNode;
8
use Digia\GraphQL\Language\Node\FieldNode;
9
use Digia\GraphQL\Language\Node\NameAwareInterface;
10
use Digia\GraphQL\Language\Node\NamedTypeNode;
11
use Digia\GraphQL\Language\Node\NodeInterface;
12
use Digia\GraphQL\Type\Definition\Argument;
13
use function Digia\GraphQL\Util\suggestionList;
14
use function Digia\GraphQL\Validation\unknownArgumentMessage;
15
use function Digia\GraphQL\Validation\unknownDirectiveArgumentMessage;
16
17
/**
18
 * Known argument names
19
 *
20
 * A GraphQL field is only valid if all supplied arguments are defined by
21
 * that field.
22
 */
23
class KnownArgumentNamesRule extends AbstractRule
24
{
25
    /**
26
     * @inheritdoc
27
     */
28
    protected function enterArgument(ArgumentNode $node): ?NodeInterface
29
    {
30
        $argumentDefinition = $this->context->getArgument();
31
32
        if (null === $argumentDefinition) {
33
            $argumentOf = $node->getAncestor();
34
35
            if ($argumentOf instanceof FieldNode) {
36
                return $this->validateField($node);
37
            }
38
39
            if ($argumentOf instanceof DirectiveNode) {
40
                return $this->validateDirective($node);
41
            }
42
        }
43
44
        return $node;
45
    }
46
47
    /**
48
     * @param NodeInterface $node
49
     * @return NodeInterface|null
50
     */
51
    protected function validateField(NodeInterface $node): ?NodeInterface
52
    {
53
        $fieldDefinition = $this->context->getFieldDefinition();
54
        $parentType      = $this->context->getParentType();
55
56
        if (null !== $fieldDefinition && null !== $parentType) {
57
            $options = array_map(function (Argument $argument) {
58
                return $argument->getName();
59
            }, $fieldDefinition->getArguments());
60
61
            $suggestions = suggestionList(
62
                $node instanceof NameAwareInterface
0 ignored issues
show
Bug introduced by
It seems like $node instanceof Digia\G...NameValue() : 'unknown' can also be of type null; however, parameter $input of Digia\GraphQL\Util\suggestionList() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

62
                /** @scrutinizer ignore-type */ $node instanceof NameAwareInterface
Loading history...
63
                    ? $node->getNameValue()
64
                    : 'unknown',
65
                $options
66
            );
67
68
            $this->context->reportError(
69
                new ValidationException(
70
                    unknownArgumentMessage((string)$node, (string)$fieldDefinition, (string)$parentType, $suggestions),
71
                    [$node]
72
                )
73
            );
74
        }
75
76
        return $node;
77
    }
78
79
    /**
80
     * @param NodeInterface $node
81
     * @return NodeInterface|null
82
     */
83
    protected function validateDirective(NodeInterface $node): ?NodeInterface
84
    {
85
        $directive = $this->context->getDirective();
86
87
        if (null !== $directive) {
88
            $options = array_map(function (Argument $argument) {
89
                return $argument->getName();
90
            }, $directive->getArguments());
91
92
            $suggestions = suggestionList((string)$node, $options);
93
94
            $this->context->reportError(
95
                new ValidationException(
96
                    unknownDirectiveArgumentMessage((string)$node, (string)$directive, $suggestions),
97
                    [$node]
98
                )
99
            );
100
        }
101
102
        return $node;
103
    }
104
}
105