Failed Conditions
Push — master ( a4f39b...12ee90 )
by Vladimir
11:17
created

UniqueDirectivesPerLocation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 16
c 0
b 0
f 0
dl 0
loc 40
ccs 18
cts 18
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSDLVisitor() 0 3 1
A getASTVisitor() 0 19 4
A duplicateDirectiveMessage() 0 3 1
A getVisitor() 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\DirectiveNode;
9
use GraphQL\Language\AST\Node;
10
use GraphQL\Validator\ASTValidationContext;
11
use GraphQL\Validator\SDLValidationContext;
12
use GraphQL\Validator\ValidationContext;
13
use function sprintf;
14
15
class UniqueDirectivesPerLocation extends ValidationRule
16
{
17 125
    public function getVisitor(ValidationContext $context)
18
    {
19 125
        return $this->getASTVisitor($context);
20
    }
21
22 208
    public function getSDLVisitor(SDLValidationContext $context)
23
    {
24 208
        return $this->getASTVisitor($context);
25
    }
26
27 326
    public function getASTVisitor(ASTValidationContext $context)
28
    {
29
        return [
30
            'enter' => static function (Node $node) use ($context) {
31 326
                if (! isset($node->directives)) {
32 326
                    return;
33
                }
34
35 310
                $knownDirectives = [];
36 310
                foreach ($node->directives as $directive) {
37
                    /** @var DirectiveNode $directive */
38 26
                    $directiveName = $directive->name->value;
39 26
                    if (isset($knownDirectives[$directiveName])) {
40 5
                        $context->reportError(new Error(
41 5
                            self::duplicateDirectiveMessage($directiveName),
42 5
                            [$knownDirectives[$directiveName], $directive]
43
                        ));
44
                    } else {
45 26
                        $knownDirectives[$directiveName] = $directive;
46
                    }
47
                }
48 326
            },
49
        ];
50
    }
51
52 5
    public static function duplicateDirectiveMessage($directiveName)
53
    {
54 5
        return sprintf('The directive "%s" can only be used once at this location.', $directiveName);
55
    }
56
}
57