Completed
Pull Request — master (#45)
by Christoffer
02:04
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\AST\Node\ArgumentNode;
7
use Digia\GraphQL\Language\AST\Node\DirectiveNode;
8
use Digia\GraphQL\Language\AST\Node\FieldNode;
9
use Digia\GraphQL\Language\AST\Node\NodeInterface;
10
use Digia\GraphQL\Type\Definition\Argument;
11
use function Digia\GraphQL\Util\suggestionList;
12
13
class KnownArgumentNamesRule extends AbstractRule
14
{
15
    /**
16
     * @inheritdoc
17
     */
18
    public function enterNode(NodeInterface $node): ?NodeInterface
19
    {
20
        if ($node instanceof ArgumentNode) {
21
            $argumentDefinition = $this->context->getArgument();
22
23
            if (null === $argumentDefinition) {
24
                $argumentOf = $node->getAncestor();
25
                if ($argumentOf instanceof FieldNode) {
26
                    $fieldDefinition = $this->context->getFieldDefinition();
27
                    $parentType      = $this->context->getParentType();
28
29
                    if (null !== $fieldDefinition && null !== $parentType) {
30
                        $this->context->reportError(
31
                            new ValidationException(
32
                                unknownArgumentMessage(
33
                                    (string)$node,
34
                                    (string)$fieldDefinition,
35
                                    (string)$parentType,
36
                                    suggestionList(
37
                                        $node->getNameValue(),
38
                                        array_map(function (Argument $argument) {
39
                                            return $argument->getName();
40
                                        }, $fieldDefinition->getArguments())
41
                                    )
42
                                ),
43
                                [$node]
44
                            )
45
                        );
46
                    }
47
                } elseif ($argumentOf instanceof DirectiveNode) {
48
                    $directive = $this->context->getDirective();
49
50
                    if (null !== $directive) {
51
                        $this->context->reportError(
52
                            new ValidationException(
53
                                unknownDirectiveArgumentMessage(
54
                                    (string)$node,
55
                                    (string)$directive,
56
                                    suggestionList(
57
                                        (string)$node,
58
                                        array_map(function (Argument $argument) {
59
                                            return $argument->getName();
60
                                        }, $directive->getArguments())
61
                                    )
62
                                ),
63
                                [$node]
64
                            )
65
                        );
66
                    }
67
                }
68
            }
69
        }
70
71
        return $node;
72
    }
73
}
74