Completed
Push — master ( a01b08...b72ba3 )
by Vladimir
16s queued 14s
created

KnownFragmentNames   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 10
dl 0
loc 26
ccs 11
cts 11
cp 1
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 116
    public function getVisitor(ValidationContext $context)
16
    {
17
        return [
18
            NodeKind::FRAGMENT_SPREAD => static function (FragmentSpreadNode $node) use ($context) {
19 12
                $fragmentName = $node->name->value;
20 12
                $fragment     = $context->getFragment($fragmentName);
21 12
                if ($fragment) {
22 11
                    return;
23
                }
24
25 1
                $context->reportError(new Error(
26 1
                    self::unknownFragmentMessage($fragmentName),
27 1
                    [$node->name]
28
                ));
29 116
            },
30
        ];
31
    }
32
33
    /**
34
     * @param string $fragName
35
     */
36 1
    public static function unknownFragmentMessage($fragName)
37
    {
38 1
        return sprintf('Unknown fragment "%s".', $fragName);
39
    }
40
}
41