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

FragmentsOnCompositeTypesRule::enterNode()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 1
dl 0
loc 17
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\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
class FragmentsOnCompositeTypesRule extends AbstractRule
13
{
14
    /**
15
     * @inheritdoc
16
     */
17
    public function enterNode(NodeInterface $node): ?NodeInterface
18
    {
19
        if ($node instanceof InlineFragmentNode) {
20
            $this->validateFragementNode($node, function (NodeInterface $node) {
21
                /** @noinspection PhpUndefinedMethodInspection */
22
                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

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