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

KnownArgumentNames::unknownArgMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 4
crap 6
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