Completed
Push — master ( ab51f6...55af3e )
by Christoffer
02:36
created

UniqueOperationNamesRule::enterNode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Error\ValidationException;
6
use Digia\GraphQL\Language\Node\NodeInterface;
7
use Digia\GraphQL\Language\Node\OperationDefinitionNode;
8
use function Digia\GraphQL\Validation\duplicateOperationMessage;
9
10
/**
11
 * Unique operation names
12
 *
13
 * A GraphQL document is only valid if all defined operations have unique names.
14
 */
15
class UniqueOperationNamesRule extends AbstractRule
16
{
17
    /**
18
     * @var string[]
19
     */
20
    protected $knownOperationNames = [];
21
22
    /**
23
     * @inheritdoc
24
     */
25
    public function enterNode(NodeInterface $node): ?NodeInterface
26
    {
27
        if ($node instanceof OperationDefinitionNode) {
28
            $operationName = $node->getNameValue();
29
30
            if (null !== $operationName) {
31
                if (isset($this->knownOperationNames[$operationName])) {
32
                    $this->validationContext->reportError(
33
                        new ValidationException(
34
                            duplicateOperationMessage($operationName),
35
                            [$this->knownOperationNames[$operationName], $node->getName()]
36
                        )
37
                    );
38
                } else {
39
                    $this->knownOperationNames[$operationName] = $node->getName();
40
                }
41
            }
42
        }
43
44
        return $node;
45
    }
46
}
47