KnownFragmentNamesRule   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 17
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A enterFragmentSpread() 0 12 2
1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Language\Node\FragmentSpreadNode;
6
use Digia\GraphQL\Language\Visitor\VisitorResult;
7
use Digia\GraphQL\Validation\ValidationException;
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 enterFragmentSpread(FragmentSpreadNode $node): VisitorResult
22
    {
23
        $fragmentName = $node->getNameValue();
24
        $fragment     = $this->context->getFragment($fragmentName);
0 ignored issues
show
Bug introduced by
It seems like $fragmentName can also be of type null; however, parameter $name of Digia\GraphQL\Validation...nterface::getFragment() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
        $fragment     = $this->context->getFragment(/** @scrutinizer ignore-type */ $fragmentName);
Loading history...
25
26
        if (null === $fragment) {
27
            $this->context->reportError(
28
                new ValidationException(unknownFragmentMessage($fragmentName), [$node->getName()])
0 ignored issues
show
Bug introduced by
It seems like $fragmentName can also be of type null; however, parameter $fragmentName of Digia\GraphQL\Validation\unknownFragmentMessage() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

28
                new ValidationException(unknownFragmentMessage(/** @scrutinizer ignore-type */ $fragmentName), [$node->getName()])
Loading history...
29
            );
30
        }
31
32
        return new VisitorResult($node);
33
    }
34
}
35