Passed
Push — master ( 7cf87e...f83c5a )
by Christoffer
02:19
created

KnownArgumentNamesRule::enterNode()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 35
nc 7
nop 1
dl 0
loc 54
rs 7.4119
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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