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

ExecutableDefinitionRule   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B enterNode() 0 25 5
1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Error\GraphQLError;
6
use Digia\GraphQL\Language\AST\Node\DocumentNode;
7
use Digia\GraphQL\Language\AST\Node\ExecutableDefinitionNodeInterface;
8
use Digia\GraphQL\Language\AST\Node\NamedTypeNode;
9
use Digia\GraphQL\Language\AST\Node\NodeInterface;
10
use Digia\GraphQL\Language\AST\Node\SchemaDefinitionNode;
11
12
class ExecutableDefinitionRule 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 DocumentNode) {
24
            /** @var NamedTypeNode $definition */
25
            foreach ($node->getDefinitions() as $definition) {
26
                if (!$definition instanceof ExecutableDefinitionNodeInterface) {
27
                    $this->context->reportError(
28
                        new GraphQLError(
29
                            nonExecutableDefinitionMessage(
30
                                $definition instanceof SchemaDefinitionNode ? 'schema' : $definition->getNameValue()
31
                            ),
32
                            [$definition]
33
                        )
34
                    );
35
                }
36
            }
37
38
            return null;
39
        }
40
41
        return $node;
42
    }
43
}
44