Failed Conditions
Push — master ( 7ff3e9...e7de06 )
by Vladimir
04:33 queued 01:54
created

LoneSchemaDefinition   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 7
eloc 17
dl 0
loc 26
ccs 12
cts 15
cp 0.8
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getVisitor() 0 24 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Validator\Rules;
6
7
use GraphQL\Error\Error;
8
use GraphQL\Language\AST\NodeKind;
9
use GraphQL\Language\AST\SchemaDefinitionNode;
10
use GraphQL\Validator\ValidationContext;
11
12
/**
13
 * Lone Schema definition
14
 *
15
 * A GraphQL document is only valid if it contains only one schema definition.
16
 */
17
class LoneSchemaDefinition extends ValidationRule
18
{
19 52
    public function getVisitor(ValidationContext $context)
20
    {
21 52
        $oldSchema      = $context->getSchema();
22 52
        $alreadyDefined = $oldSchema !== null ? (
23 52
            $oldSchema->getAstNode() ||
24 52
            $oldSchema->getQueryType() ||
25 1
            $oldSchema->getMutationType() ||
26 52
            $oldSchema->getSubscriptionType()
27 52
        ) : false;
28
29 52
        $schemaDefinitionsCount = 0;
30
31
        return [
32
            NodeKind::SCHEMA_DEFINITION => static function (SchemaDefinitionNode $node) use ($alreadyDefined, $context, &$schemaDefinitionsCount) {
33 1
                if ($alreadyDefined !== false) {
34
                    $context->reportError(new Error('Cannot define a new schema within a schema extension.', $node));
35
                    return;
36
                }
37
38 1
                if ($schemaDefinitionsCount > 0) {
39
                    $context->reportError(new Error('Must provide only one schema definition.', $node));
40
                }
41
42 1
                ++$schemaDefinitionsCount;
43 52
            },
44
        ];
45
    }
46
}
47