Completed
Pull Request — master (#45)
by Christoffer
02:12
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\AST\Node\FragmentSpreadNode;
7
use Digia\GraphQL\Language\AST\Node\NodeInterface;
8
9
class KnownFragmentNamesRule extends AbstractRule
10
{
11
    /**
12
     * @inheritdoc
13
     */
14
    public function enterNode(NodeInterface $node): ?NodeInterface
15
    {
16
        if ($node instanceof FragmentSpreadNode) {
17
            $fragmentName = $node->getNameValue();
18
            $fragment = $this->context->getFragment($fragmentName);
19
20
            if (null === $fragment) {
21
                $this->context->reportError(
22
                    new ValidationException(unknownFragmentMessage($fragmentName), [$node->getName()])
23
                );
24
            }
25
        }
26
27
        return $node;
28
    }
29
}
30