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

FragmentsOnCompositeTypesRule   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A enterNode() 0 17 3
A validateFragementNode() 0 9 4
1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Error\ValidationException;
6
use Digia\GraphQL\Language\AST\Node\FragmentDefinitionNode;
7
use Digia\GraphQL\Language\AST\Node\InlineFragmentNode;
8
use Digia\GraphQL\Language\AST\Node\NodeInterface;
9
use Digia\GraphQL\Type\Definition\CompositeTypeInterface;
10
use function Digia\GraphQL\Util\typeFromAST;
11
12
13
/**
14
 * Fragments on composite type
15
 *
16
 * Fragments use a type condition to determine if they apply, since fragments
17
 * can only be spread into a composite type (object, interface, or union), the
18
 * type condition must also be a composite type.
19
 */
20
class FragmentsOnCompositeTypesRule extends AbstractRule
21
{
22
    /**
23
     * @inheritdoc
24
     */
25
    public function enterNode(NodeInterface $node): ?NodeInterface
26
    {
27
        if ($node instanceof InlineFragmentNode) {
28
            $this->validateFragementNode($node, function (NodeInterface $node) {
29
                /** @noinspection PhpUndefinedMethodInspection */
30
                return inlineFragmentOnNonCompositeMessage((string)$node->getTypeCondition());
0 ignored issues
show
Bug introduced by
The method getTypeCondition() does not exist on Digia\GraphQL\Language\AST\Node\NodeInterface. It seems like you code against a sub-type of Digia\GraphQL\Language\AST\Node\NodeInterface such as Digia\GraphQL\Language\AST\Node\InlineFragmentNode or Digia\GraphQL\Language\A...\FragmentDefinitionNode. ( Ignorable by Annotation )

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

30
                return inlineFragmentOnNonCompositeMessage((string)$node->/** @scrutinizer ignore-call */ getTypeCondition());
Loading history...
31
            });
32
        }
33
34
        if ($node instanceof FragmentDefinitionNode) {
35
            $this->validateFragementNode($node, function (NodeInterface $node) {
36
                /** @noinspection PhpUndefinedMethodInspection */
37
                return fragmentOnNonCompositeMessage((string)$node, (string)$node->getTypeCondition());
38
            });
39
        }
40
41
        return $node;
42
    }
43
44
    /**
45
     * @param NodeInterface|InlineFragmentNode|FragmentDefinitionNode $node
46
     * @param callable                                                $errorMessageFunction
47
     * @throws \Exception
48
     * @throws \TypeError
49
     */
50
    protected function validateFragementNode(NodeInterface $node, callable $errorMessageFunction)
51
    {
52
        $typeCondition = $node->getTypeCondition();
53
54
        if (null !== $typeCondition) {
55
            $type = typeFromAST($this->validationContext->getSchema(), $typeCondition);
56
57
            if (null !== $type && !($type instanceof CompositeTypeInterface)) {
58
                $this->validationContext->reportError(new ValidationException($errorMessageFunction($node), [$typeCondition]));
59
            }
60
        }
61
    }
62
}
63