GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ProvidedRequiredArgumentsOnDirectives   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 65.22%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 46
dl 0
loc 79
ccs 30
cts 46
cp 0.6522
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C getSDLVisitor() 0 72 15
A missingDirectiveArgMessage() 0 3 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\ArgumentNode;
9
use GraphQL\Language\AST\DirectiveDefinitionNode;
10
use GraphQL\Language\AST\DirectiveNode;
11
use GraphQL\Language\AST\NamedTypeNode;
12
use GraphQL\Language\AST\Node;
13
use GraphQL\Language\AST\NodeKind;
14
use GraphQL\Language\AST\NodeList;
15
use GraphQL\Language\AST\NonNullTypeNode;
16
use GraphQL\Type\Definition\Directive;
17
use GraphQL\Type\Definition\FieldArgument;
18
use GraphQL\Type\Definition\NonNull;
19
use GraphQL\Utils\Utils;
20
use GraphQL\Validator\SDLValidationContext;
21
use function array_filter;
22
use function is_array;
23
use function iterator_to_array;
24
25
/**
26
 * Provided required arguments on directives
27
 *
28
 * A directive is only valid if all required (non-null without a
29
 * default value) field arguments have been provided.
30
 */
31
class ProvidedRequiredArgumentsOnDirectives extends ValidationRule
32
{
33
    protected static function missingDirectiveArgMessage(string $directiveName, string $argName)
34
    {
35
        return 'Directive "' . $directiveName . '" argument "' . $argName . '" is required but ont provided.';
36
    }
37
38 207
    public function getSDLVisitor(SDLValidationContext $context)
39
    {
40 207
        $requiredArgsMap   = [];
41 207
        $schema            = $context->getSchema();
42 207
        $definedDirectives = $schema
0 ignored issues
show
introduced by
$schema is of type GraphQL\Type\Schema, thus it always evaluated to true.
Loading history...
43 64
            ? $schema->getDirectives()
44 207
            : Directive::getInternalDirectives();
45
46 207
        foreach ($definedDirectives as $directive) {
47 207
            $requiredArgsMap[$directive->name] = Utils::keyMap(
48
                array_filter($directive->args, static function (FieldArgument $arg) : bool {
49 207
                    return $arg->getType() instanceof NonNull && ! isset($arg->defaultValue);
50 207
                }),
51
                static function (FieldArgument $arg) : string {
52 206
                    return $arg->name;
53 207
                }
54
            );
55
        }
56
57 207
        $astDefinition = $context->getDocument()->definitions;
58 207
        foreach ($astDefinition as $def) {
59 207
            if (! ($def instanceof DirectiveDefinitionNode)) {
60 188
                continue;
61
            }
62
63 37
            if (is_array($def->arguments)) {
64
                $arguments = $def->arguments;
65 37
            } elseif ($def->arguments instanceof NodeList) {
66 37
                $arguments = iterator_to_array($def->arguments->getIterator());
67
            } else {
68
                $arguments = null;
69
            }
70
71 37
            $requiredArgsMap[$def->name->value] = Utils::keyMap(
72 37
                $arguments
73
                    ? array_filter($arguments, static function (Node $argument) : bool {
74 9
                        return $argument instanceof NonNullTypeNode &&
75
                            (
76
                                ! isset($argument->defaultValue) ||
77 9
                                $argument->defaultValue === null
78
                            );
79 9
                    })
80 37
                    : [],
81
                static function (NamedTypeNode $argument) : string {
82
                    return $argument->name->value;
83 37
                }
84
            );
85
        }
86
87
        return [
88
            NodeKind::DIRECTIVE => static function (DirectiveNode $directiveNode) use ($requiredArgsMap, $context) {
89 17
                $directiveName = $directiveNode->name->value;
90 17
                $requiredArgs  = $requiredArgsMap[$directiveName] ?? null;
91 17
                if (! $requiredArgs) {
92 17
                    return null;
93
                }
94
95
                $argNodes   = $directiveNode->arguments ?: [];
96
                $argNodeMap = Utils::keyMap(
97
                    $argNodes,
98
                    static function (ArgumentNode $arg) : string {
99
                        return $arg->name->value;
100
                    }
101
                );
102
103
                foreach ($requiredArgs as $argName => $arg) {
104
                    if (isset($argNodeMap[$argName])) {
105
                        continue;
106
                    }
107
108
                    $context->reportError(
109
                        new Error(static::missingDirectiveArgMessage($directiveName, $argName), [$directiveNode])
110
                    );
111
                }
112 207
            },
113
        ];
114
    }
115
}
116