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

KnownFragmentNames::getVisitor()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

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