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

LoneAnonymousOperationRule::enterNode()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 6
nop 1
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
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