Passed
Pull Request — master (#58)
by Christoffer
02:15
created

KnownArgumentNamesRule   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B enterNode() 0 54 8
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\NodeInterface;
10
use Digia\GraphQL\Type\Definition\Argument;
11
use function Digia\GraphQL\Util\suggestionList;
12
use function Digia\GraphQL\Validation\unknownArgumentMessage;
13
use function Digia\GraphQL\Validation\unknownDirectiveArgumentMessage;
14
15
/**
16
 * Known argument names
17
 *
18
 * A GraphQL field is only valid if all supplied arguments are defined by
19
 * that field.
20
 */
21
class KnownArgumentNamesRule extends AbstractRule
22
{
23
    /**
24
     * @inheritdoc
25
     */
26
    public function enterNode(NodeInterface $node): ?NodeInterface
27
    {
28
        if ($node instanceof ArgumentNode) {
29
            $argumentDefinition = $this->validationContext->getArgument();
30
31
            if (null === $argumentDefinition) {
32
                $argumentOf = $node->getAncestor();
33
                if ($argumentOf instanceof FieldNode) {
34
                    $fieldDefinition = $this->validationContext->getFieldDefinition();
35
                    $parentType      = $this->validationContext->getParentType();
36
37
                    if (null !== $fieldDefinition && null !== $parentType) {
38
                        $this->validationContext->reportError(
39
                            new ValidationException(
40
                                unknownArgumentMessage(
41
                                    (string)$node,
42
                                    (string)$fieldDefinition,
43
                                    (string)$parentType,
44
                                    suggestionList(
45
                                        $node->getNameValue(),
46
                                        array_map(function (Argument $argument) {
47
                                            return $argument->getName();
48
                                        }, $fieldDefinition->getArguments())
49
                                    )
50
                                ),
51
                                [$node]
52
                            )
53
                        );
54
                    }
55
                } elseif ($argumentOf instanceof DirectiveNode) {
56
                    $directive = $this->validationContext->getDirective();
57
58
                    if (null !== $directive) {
59
                        $this->validationContext->reportError(
60
                            new ValidationException(
61
                                unknownDirectiveArgumentMessage(
62
                                    (string)$node,
63
                                    (string)$directive,
64
                                    suggestionList(
65
                                        (string)$node,
66
                                        array_map(function (Argument $argument) {
67
                                            return $argument->getName();
68
                                        }, $directive->getArguments())
69
                                    )
70
                                ),
71
                                [$node]
72
                            )
73
                        );
74
                    }
75
                }
76
            }
77
        }
78
79
        return $node;
80
    }
81
}
82