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

enterOperationDefinition()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
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
    protected function enterOperationDefinition(OperationDefinitionNode $node): ?NodeInterface
26
    {
27
        $operationName = $node->getNameValue();
28
29
        if (null !== $operationName) {
30
            if (isset($this->knownOperationNames[$operationName])) {
31
                $this->context->reportError(
32
                    new ValidationException(
33
                        duplicateOperationMessage($operationName),
34
                        [$this->knownOperationNames[$operationName], $node->getName()]
35
                    )
36
                );
37
            } else {
38
                $this->knownOperationNames[$operationName] = $node->getName();
39
            }
40
        }
41
42
        return $node;
43
    }
44
}
45