Passed
Pull Request — master (#45)
by Christoffer
02:10
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\GraphQLError;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Error\GraphQLError was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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($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

22
                return inlineFragmentOnNonCompositeMessage($node->/** @scrutinizer ignore-call */ getTypeCondition()->getNameValue());
Loading history...
23
            });
24
        }
25
26
        if ($node instanceof FragmentDefinitionNode) {
27
            $this->validateFragementNode($node, function (NodeInterface $node) {
28
                /** @noinspection PhpUndefinedMethodInspection */
29
                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

29
                return fragmentOnNonCompositeMessage($node->/** @scrutinizer ignore-call */ getNameValue(), $node->getTypeCondition()->getNameValue());
Loading history...
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 GraphQLError($errorMessageFunction($node), [$typeCondition]));
51
            }
52
        }
53
    }
54
}
55