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

UniqueArgumentNames::getSDLVisitor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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\ArgumentNode;
9
use GraphQL\Language\AST\NameNode;
10
use GraphQL\Language\AST\NodeKind;
11
use GraphQL\Language\Visitor;
12
use GraphQL\Validator\ASTValidationContext;
13
use GraphQL\Validator\SDLValidationContext;
14
use GraphQL\Validator\ValidationContext;
15
use function sprintf;
16
17
class UniqueArgumentNames extends ValidationRule
18
{
19
    /** @var NameNode[] */
20
    public $knownArgNames;
21
22 207
    public function getSDLVisitor(SDLValidationContext $context)
23
    {
24 207
        return $this->getASTVisitor($context);
25
    }
26
27 129
    public function getVisitor(ValidationContext $context)
28
    {
29 129
        return $this->getASTVisitor($context);
30
    }
31
32 329
    public function getASTVisitor(ASTValidationContext $context)
33
    {
34 329
        $this->knownArgNames = [];
35
36
        return [
37
            NodeKind::FIELD     => function () {
38 132
                $this->knownArgNames = [];
39 329
            },
40
            NodeKind::DIRECTIVE => function () {
41 24
                $this->knownArgNames = [];
42 329
            },
43
            NodeKind::ARGUMENT  => function (ArgumentNode $node) use ($context) {
44 70
                $argName = $node->name->value;
45 70
                if (! empty($this->knownArgNames[$argName])) {
46 4
                    $context->reportError(new Error(
47 4
                        self::duplicateArgMessage($argName),
48 4
                        [$this->knownArgNames[$argName], $node->name]
49
                    ));
50
                } else {
51 70
                    $this->knownArgNames[$argName] = $node->name;
52
                }
53
54 70
                return Visitor::skipNode();
55 329
            },
56
        ];
57
    }
58
59 4
    public static function duplicateArgMessage($argName)
60
    {
61 4
        return sprintf('There can be only one argument named "%s".', $argName);
62
    }
63
}
64