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

ExecutableDefinitionRule   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B enterNode() 0 19 5
1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Error\ValidationException;
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(NodeInterface $node): ?NodeInterface
18
    {
19
        if ($node instanceof DocumentNode) {
20
            /** @var NamedTypeNode $definition */
21
            foreach ($node->getDefinitions() as $definition) {
22
                if (!$definition instanceof ExecutableDefinitionNodeInterface) {
23
                    $this->context->reportError(
24
                        new ValidationException(
25
                            nonExecutableDefinitionMessage(
26
                                $definition instanceof SchemaDefinitionNode ? 'schema' : $definition->getNameValue()
27
                            ),
28
                            [$definition]
29
                        )
30
                    );
31
                }
32
            }
33
        }
34
35
        return $node;
36
    }
37
}
38