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

FragmentsOnCompositeTypesRule::enterNode()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 4
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Error\GraphQLError;
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(
18
        NodeInterface $node,
19
        $key = null,
20
        ?NodeInterface $parent = null,
21
        array $path = []
22
    ): ?NodeInterface {
23
        if ($node instanceof InlineFragmentNode) {
24
            $this->validateFragementNode($node, function (NodeInterface $node) {
25
                /** @noinspection PhpUndefinedMethodInspection */
26
                return inlineFragmentOnNonCompositeErrorMessage($node->getTypeCondition()->getNameValue());
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

26
                return inlineFragmentOnNonCompositeErrorMessage($node->/** @scrutinizer ignore-call */ getTypeCondition()->getNameValue());
Loading history...
27
            });
28
        }
29
30
        if ($node instanceof FragmentDefinitionNode) {
31
            $this->validateFragementNode($node, function (NodeInterface $node) {
32
                /** @noinspection PhpUndefinedMethodInspection */
33
                return fragmentOnNonCompositeMessage($node->getNameValue(), $node->getTypeCondition()->getNameValue());
0 ignored issues
show
Bug introduced by
The method getNameValue() 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\DirectiveNode or Digia\GraphQL\Language\AST\Node\ArgumentNode or Digia\GraphQL\Language\AST\Node\ObjectFieldNode or Digia\GraphQL\Language\AST\Node\FragmentSpreadNode or Digia\GraphQL\Language\A...\EnumTypeDefinitionNode or Digia\GraphQL\Language\A...EnumValueDefinitionNode or Digia\GraphQL\Language\A...DirectiveDefinitionNode or Digia\GraphQL\Language\A...nputValueDefinitionNode or Digia\GraphQL\Language\A...ode\FieldDefinitionNode or Digia\GraphQL\Language\A...\VariableDefinitionNode or Digia\GraphQL\Language\A...DefinitionNodeInterface or Digia\GraphQL\Language\A...DefinitionNodeInterface or Digia\GraphQL\Language\A...erfaceTypeExtensionNode or Digia\GraphQL\Language\A...ScalarTypeExtensionNode or Digia\GraphQL\Language\A...\UnionTypeExtensionNode or Digia\GraphQL\Language\A...ObjectTypeExtensionNode or Digia\GraphQL\Language\A...e\EnumTypeExtensionNode or Digia\GraphQL\Language\A...ObjectTypeExtensionNode or Digia\GraphQL\Language\AST\Node\VariableNode or Digia\GraphQL\Language\AST\Node\FieldNode or Digia\GraphQL\Language\AST\Node\NamedTypeNode. ( Ignorable by Annotation )

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

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