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

KnownFragmentNames::getVisitor()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.2109

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 13
ccs 5
cts 8
cp 0.625
rs 9.9666
c 0
b 0
f 0
cc 2
nc 1
nop 1
crap 2.2109
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