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

KnownFragmentNames   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 54.55%

Importance

Changes 0
Metric Value
wmc 3
eloc 10
dl 0
loc 26
ccs 6
cts 11
cp 0.5455
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getVisitor() 0 13 2
A unknownFragmentMessage() 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\FragmentSpreadNode;
9
use GraphQL\Language\AST\NodeKind;
10
use GraphQL\Validator\ValidationContext;
11
use function sprintf;
12
13
class KnownFragmentNames extends ValidationRule
14
{
15 104
    public function getVisitor(ValidationContext $context)
16
    {
17
        return [
18
            NodeKind::FRAGMENT_SPREAD => static function (FragmentSpreadNode $node) use ($context) {
19 10
                $fragmentName = $node->name->value;
20 10
                $fragment     = $context->getFragment($fragmentName);
21 10
                if ($fragment) {
22 10
                    return;
23
                }
24
25
                $context->reportError(new Error(
26
                    self::unknownFragmentMessage($fragmentName),
27
                    [$node->name]
28
                ));
29 104
            },
30
        ];
31
    }
32
33
    /**
34
     * @param string $fragName
35
     */
36
    public static function unknownFragmentMessage($fragName)
37
    {
38
        return sprintf('Unknown fragment "%s".', $fragName);
39
    }
40
}
41