Passed
Pull Request — master (#45)
by Christoffer
02:07
created

LoneAnonymousOperationRule   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B enterNode() 0 17 5
1
<?php
2
3
namespace Digia\GraphQL\Validation\Rule;
4
5
use Digia\GraphQL\Error\ValidationException;
6
use Digia\GraphQL\Language\AST\Node\DocumentNode;
7
use Digia\GraphQL\Language\AST\Node\NodeInterface;
8
use Digia\GraphQL\Language\AST\Node\OperationDefinitionNode;
9
10
class LoneAnonymousOperationRule extends AbstractRule
11
{
12
    /**
13
     * @var int
14
     */
15
    protected $operationCount = 0;
16
17
    /**
18
     * @inheritdoc
19
     */
20
    public function enterNode(NodeInterface $node): ?NodeInterface
21
    {
22
        if ($node instanceof DocumentNode) {
23
            $this->operationCount = \count(array_filter($node->getDefinitions(), function ($definition) {
24
                return $definition instanceof OperationDefinitionNode;
25
            }));
26
        }
27
28
        if ($node instanceof OperationDefinitionNode) {
29
            if (null === $node->getName() && $this->operationCount > 1) {
30
                $this->context->reportError(
31
                    new ValidationException(anonymousOperationNotAloneMessage(), [$node])
32
                );
33
            }
34
        }
35
36
        return $node;
37
    }
38
}
39