Completed
Push — master ( 918596...ffa014 )
by Christoffer
02:29 queued 12s
created

KnownFragmentNamesRule::enterNode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Error\ValidationException;
6
use Digia\GraphQL\Language\Node\FragmentSpreadNode;
7
use Digia\GraphQL\Language\Node\NodeInterface;
8
use function Digia\GraphQL\Validation\unknownFragmentMessage;
9
10
/**
11
 * Known fragment names
12
 *
13
 * A GraphQL document is only valid if all `...Fragment` fragment spreads refer
14
 * to fragments defined in the same document.
15
 */
16
class KnownFragmentNamesRule extends AbstractRule
17
{
18
    /**
19
     * @inheritdoc
20
     */
21
    public function enterNode(NodeInterface $node): ?NodeInterface
22
    {
23
        if ($node instanceof FragmentSpreadNode) {
24
            $fragmentName = $node->getNameValue();
25
            $fragment     = $this->validationContext->getFragment($fragmentName);
26
27
            if (null === $fragment) {
28
                $this->validationContext->reportError(
29
                    new ValidationException(unknownFragmentMessage($fragmentName), [$node->getName()])
30
                );
31
            }
32
        }
33
34
        return $node;
35
    }
36
}
37