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

LoneSchemaDefinition::getVisitor()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.392

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 24
ccs 12
cts 15
cp 0.8
rs 8.8333
c 0
b 0
f 0
cc 7
nc 8
nop 1
crap 7.392
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