Failed Conditions
Push — master ( 177394...bf4e7d )
by Vladimir
09:19
created

ProvidedRequiredArguments   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
eloc 46
dl 0
loc 80
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C getVisitor() 0 54 15
A missingFieldArgMessage() 0 7 1
A missingDirectiveArgMessage() 0 7 1
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\DirectiveNode;
9
use GraphQL\Language\AST\FieldNode;
10
use GraphQL\Language\AST\NodeKind;
11
use GraphQL\Language\Visitor;
12
use GraphQL\Type\Definition\NonNull;
13
use GraphQL\Validator\ValidationContext;
14
use function sprintf;
15
16
class ProvidedRequiredArguments extends ValidationRule
17
{
18 133
    public function getVisitor(ValidationContext $context)
19
    {
20
        return [
21
            NodeKind::FIELD     => [
22
                'leave' => static function (FieldNode $fieldNode) use ($context) {
23 133
                    $fieldDef = $context->getFieldDef();
24
25 133
                    if (! $fieldDef) {
0 ignored issues
show
introduced by
$fieldDef is of type GraphQL\Type\Definition\FieldDefinition, thus it always evaluated to true.
Loading history...
26 9
                        return Visitor::skipNode();
27
                    }
28 128
                    $argNodes = $fieldNode->arguments ?: [];
29
30 128
                    $argNodeMap = [];
31 128
                    foreach ($argNodes as $argNode) {
32 65
                        $argNodeMap[$argNode->name->value] = $argNode;
33
                    }
34 128
                    foreach ($fieldDef->args as $argDef) {
35 88
                        $argNode = $argNodeMap[$argDef->name] ?? null;
36 88
                        if ($argNode || (! ($argDef->getType() instanceof NonNull)) || $argDef->defaultValueExists()) {
37 86
                            continue;
38
                        }
39
40 4
                        $context->reportError(new Error(
41 4
                            self::missingFieldArgMessage($fieldNode->name->value, $argDef->name, $argDef->getType()),
42 4
                            [$fieldNode]
43
                        ));
44
                    }
45 133
                },
46
            ],
47
            NodeKind::DIRECTIVE => [
48
                'leave' => static function (DirectiveNode $directiveNode) use ($context) {
49 3
                    $directiveDef = $context->getDirective();
50 3
                    if (! $directiveDef) {
0 ignored issues
show
introduced by
$directiveDef is of type GraphQL\Type\Definition\Directive, thus it always evaluated to true.
Loading history...
51 1
                        return Visitor::skipNode();
52
                    }
53 2
                    $argNodes   = $directiveNode->arguments ?: [];
54 2
                    $argNodeMap = [];
55 2
                    foreach ($argNodes as $argNode) {
56 1
                        $argNodeMap[$argNode->name->value] = $argNodes;
57
                    }
58
59 2
                    foreach ($directiveDef->args as $argDef) {
60 2
                        $argNode = $argNodeMap[$argDef->name] ?? null;
61 2
                        if ($argNode || (! ($argDef->getType() instanceof NonNull)) || $argDef->defaultValueExists()) {
62 1
                            continue;
63
                        }
64
65 1
                        $context->reportError(new Error(
66 1
                            self::missingDirectiveArgMessage(
67 1
                                $directiveNode->name->value,
68 1
                                $argDef->name,
69 1
                                $argDef->getType()
70
                            ),
71 1
                            [$directiveNode]
72
                        ));
73
                    }
74 133
                },
75
            ],
76
        ];
77
    }
78
79 4
    public static function missingFieldArgMessage($fieldName, $argName, $type)
80
    {
81 4
        return sprintf(
82 4
            'Field "%s" argument "%s" of type "%s" is required but not provided.',
83 4
            $fieldName,
84 4
            $argName,
85 4
            $type
86
        );
87
    }
88
89 1
    public static function missingDirectiveArgMessage($directiveName, $argName, $type)
90
    {
91 1
        return sprintf(
92 1
            'Directive "@%s" argument "%s" of type "%s" is required but not provided.',
93 1
            $directiveName,
94 1
            $argName,
95 1
            $type
96
        );
97
    }
98
}
99