Completed
Pull Request — master (#45)
by Christoffer
02:13
created

KnownFragmentNamesRule   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 10
c 0
b 0
f 0
wmc 3

1 Method

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