Completed
Pull Request — master (#45)
by Christoffer
02:08
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\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->getClosestAncestor();
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