Completed
Pull Request — master (#110)
by Christoffer
03:33
created

ExecutableDefinitionsRule   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 22
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A enterDocument() 0 17 4
1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Error\ValidationException;
6
use Digia\GraphQL\Language\Node\DocumentNode;
7
use Digia\GraphQL\Language\Node\ExecutableDefinitionNodeInterface;
8
use Digia\GraphQL\Language\Node\NodeInterface;
9
use Digia\GraphQL\Language\Node\SchemaDefinitionNode;
10
use function Digia\GraphQL\Validation\nonExecutableDefinitionMessage;
11
12
/**
13
 * Executable definitions
14
 *
15
 * A GraphQL document is only valid for execution if all definitions are either
16
 * operation or fragment definitions.
17
 */
18
class ExecutableDefinitionsRule extends AbstractRule
19
{
20
    /**
21
     * @inheritdoc
22
     */
23
    protected function enterDocument(DocumentNode $node): ?NodeInterface
24
    {
25
        foreach ($node->getDefinitions() as $definition) {
26
            if (!$definition instanceof ExecutableDefinitionNodeInterface) {
27
                /** @noinspection PhpUndefinedMethodInspection */
28
                $this->context->reportError(
29
                    new ValidationException(
30
                        nonExecutableDefinitionMessage(
31
                            $definition instanceof SchemaDefinitionNode ? 'schema' : $definition->getNameValue()
32
                        ),
33
                        [$definition]
34
                    )
35
                );
36
            }
37
        }
38
39
        return $node;
40
    }
41
}
42