Completed
Push — master ( 974258...005b1a )
by Vladimir
19:57 queued 16:19
created

KnownArgumentNames::getVisitor()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 51
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 41.0452

Importance

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

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 104
    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 51
                $argDef = $context->getArgument();
34 51
                if ($argDef !== null) {
35 51
                    return;
36
                }
37
38
                $argumentOf = $ancestors[count($ancestors) - 1];
39
                if ($argumentOf instanceof FieldNode) {
40
                    $fieldDef   = $context->getFieldDef();
41
                    $parentType = $context->getParentType();
42
                    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
                        $context->reportError(new Error(
44
                            self::unknownArgMessage(
45
                                $node->name->value,
46
                                $fieldDef->name,
47
                                $parentType->name,
48
                                Utils::suggestionList(
49
                                    $node->name->value,
50
                                    array_map(
51
                                        static function ($arg) {
52
                                            return $arg->name;
53
                                        },
54
                                        $fieldDef->args
55
                                    )
56
                                )
57
                            ),
58
                            [$node]
59
                        ));
60
                    }
61
                } elseif ($argumentOf instanceof DirectiveNode) {
62
                    $directive = $context->getDirective();
63
                    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
                        $context->reportError(new Error(
65
                            self::unknownDirectiveArgMessage(
66
                                $node->name->value,
67
                                $directive->name,
68
                                Utils::suggestionList(
69
                                    $node->name->value,
70
                                    array_map(
71
                                        static function ($arg) {
72
                                            return $arg->name;
73
                                        },
74
                                        $directive->args
75
                                    )
76
                                )
77
                            ),
78
                            [$node]
79
                        ));
80
                    }
81
                }
82 104
            },
83
        ];
84
    }
85
86
    /**
87
     * @param string[] $suggestedArgs
88
     */
89
    public static function unknownArgMessage($argName, $fieldName, $typeName, array $suggestedArgs)
90
    {
91
        $message = sprintf('Unknown argument "%s" on field "%s" of type "%s".', $argName, $fieldName, $typeName);
92
        if (! empty($suggestedArgs)) {
93
            $message .= sprintf(' Did you mean %s?', Utils::quotedOrList($suggestedArgs));
94
        }
95
96
        return $message;
97
    }
98
99
    /**
100
     * @param string[] $suggestedArgs
101
     */
102
    public static function unknownDirectiveArgMessage($argName, $directiveName, array $suggestedArgs)
103
    {
104
        $message = sprintf('Unknown argument "%s" on directive "@%s".', $argName, $directiveName);
105
        if (! empty($suggestedArgs)) {
106
            $message .= sprintf(' Did you mean %s?', Utils::quotedOrList($suggestedArgs));
107
        }
108
109
        return $message;
110
    }
111
}
112