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