Failed Conditions
Push — master ( a4f39b...12ee90 )
by Vladimir
11:17
created

LoneSchemaDefinition   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 19
c 1
b 0
f 0
dl 0
loc 39
ccs 19
cts 19
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getSDLVisitor() 0 27 7
A schemaDefinitionNotAloneMessage() 0 3 1
A canNotDefineSchemaWithinExtensionMessage() 0 3 1
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\SDLValidationContext;
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 1
    public static function schemaDefinitionNotAloneMessage()
20
    {
21 1
        return 'Must provide only one schema definition.';
22
    }
23
24 2
    public static function canNotDefineSchemaWithinExtensionMessage()
25
    {
26 2
        return 'Cannot define a new schema within a schema extension.';
27
    }
28
29 210
    public function getSDLVisitor(SDLValidationContext $context)
30
    {
31 210
        $oldSchema      = $context->getSchema();
32 210
        $alreadyDefined = $oldSchema !== null
33
            ? (
34 68
                $oldSchema->getAstNode() ||
35 67
                $oldSchema->getQueryType() ||
36 3
                $oldSchema->getMutationType() ||
37 68
                $oldSchema->getSubscriptionType()
38
            )
39 210
            : false;
40
41 210
        $schemaDefinitionsCount = 0;
42
43
        return [
44
            NodeKind::SCHEMA_DEFINITION => static function (SchemaDefinitionNode $node) use ($alreadyDefined, $context, &$schemaDefinitionsCount) {
45 34
                if ($alreadyDefined !== false) {
46 2
                    $context->reportError(new Error(self::canNotDefineSchemaWithinExtensionMessage(), $node));
47
48 2
                    return;
49
                }
50
51 33
                if ($schemaDefinitionsCount > 0) {
52 1
                    $context->reportError(new Error(self::schemaDefinitionNotAloneMessage(), $node));
53
                }
54
55 33
                ++$schemaDefinitionsCount;
56 210
            },
57
        ];
58
    }
59
}
60