Completed
Push — master ( a01b08...b72ba3 )
by Vladimir
16s queued 14s
created

KnownArgumentNames::getVisitor()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 51
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 7

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 51
ccs 35
cts 35
cp 1
rs 8.4106
c 0
b 0
f 0
cc 7
nc 1
nop 1
crap 7

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
declare(strict_types=1);
4
5
namespace GraphQL\Validator\Rules;
6
7
use GraphQL\Error\Error;
8
use GraphQL\Language\AST\ArgumentNode;
9
use GraphQL\Language\AST\DirectiveNode;
10
use GraphQL\Language\AST\FieldNode;
11
use GraphQL\Language\AST\Node;
12
use GraphQL\Language\AST\NodeKind;
13
use GraphQL\Language\AST\NodeList;
14
use GraphQL\Utils\Utils;
15
use GraphQL\Validator\ValidationContext;
16
use function array_map;
17
use function count;
18
use function sprintf;
19
20
/**
21
 * Known argument names
22
 *
23
 * A GraphQL field is only valid if all supplied arguments are defined by
24
 * that field.
25
 */
26
class KnownArgumentNames extends ValidationRule
27
{
28 127
    public function getVisitor(ValidationContext $context)
29
    {
30
        return [
31
            NodeKind::ARGUMENT => static function (ArgumentNode $node, $key, $parent, $path, $ancestors) use ($context) {
32
                /** @var NodeList|Node[] $ancestors */
33 66
                $argDef = $context->getArgument();
34 66
                if ($argDef !== null) {
35 60
                    return;
36
                }
37
38 7
                $argumentOf = $ancestors[count($ancestors) - 1];
39 7
                if ($argumentOf instanceof FieldNode) {
40 5
                    $fieldDef   = $context->getFieldDef();
41 5
                    $parentType = $context->getParentType();
42 5
                    if ($fieldDef && $parentType) {
0 ignored issues
show
introduced by
$parentType is of type GraphQL\Type\Definition\Type, thus it always evaluated to true.
Loading history...
43 4
                        $context->reportError(new Error(
44 4
                            self::unknownArgMessage(
45 4
                                $node->name->value,
46 4
                                $fieldDef->name,
47 4
                                $parentType->name,
48 4
                                Utils::suggestionList(
49 4
                                    $node->name->value,
50 4
                                    array_map(
51
                                        static function ($arg) {
52 4
                                            return $arg->name;
53 4
                                        },
54 4
                                        $fieldDef->args
55
                                    )
56
                                )
57
                            ),
58 5
                            [$node]
59
                        ));
60
                    }
61 2
                } elseif ($argumentOf instanceof DirectiveNode) {
62 2
                    $directive = $context->getDirective();
63 2
                    if ($directive) {
0 ignored issues
show
introduced by
$directive is of type GraphQL\Type\Definition\Directive, thus it always evaluated to true.
Loading history...
64 2
                        $context->reportError(new Error(
65 2
                            self::unknownDirectiveArgMessage(
66 2
                                $node->name->value,
67 2
                                $directive->name,
68 2
                                Utils::suggestionList(
69 2
                                    $node->name->value,
70 2
                                    array_map(
71
                                        static function ($arg) {
72 2
                                            return $arg->name;
73 2
                                        },
74 2
                                        $directive->args
75
                                    )
76
                                )
77
                            ),
78 2
                            [$node]
79
                        ));
80
                    }
81
                }
82 127
            },
83
        ];
84
    }
85
86
    /**
87
     * @param string[] $suggestedArgs
88
     */
89 4
    public static function unknownArgMessage($argName, $fieldName, $typeName, array $suggestedArgs)
90
    {
91 4
        $message = sprintf('Unknown argument "%s" on field "%s" of type "%s".', $argName, $fieldName, $typeName);
92 4
        if (! empty($suggestedArgs)) {
93 1
            $message .= sprintf(' Did you mean %s?', Utils::quotedOrList($suggestedArgs));
94
        }
95
96 4
        return $message;
97
    }
98
99
    /**
100
     * @param string[] $suggestedArgs
101
     */
102 2
    public static function unknownDirectiveArgMessage($argName, $directiveName, array $suggestedArgs)
103
    {
104 2
        $message = sprintf('Unknown argument "%s" on directive "@%s".', $argName, $directiveName);
105 2
        if (! empty($suggestedArgs)) {
106 1
            $message .= sprintf(' Did you mean %s?', Utils::quotedOrList($suggestedArgs));
107
        }
108
109 2
        return $message;
110
    }
111
}
112