Passed
Pull Request — master (#145)
by Christoffer
02:30
created

DirectiveDefinitionASTBuilder::supportsBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace Digia\GraphQL\Language\ASTBuilder;
4
5
use Digia\GraphQL\Error\SyntaxErrorException;
6
use Digia\GraphQL\Language\DirectiveLocationEnum;
7
use Digia\GraphQL\Language\KeywordEnum;
8
use Digia\GraphQL\Language\LexerInterface;
9
use Digia\GraphQL\Language\Node\NodeKindEnum;
10
use Digia\GraphQL\Language\TokenKindEnum;
11
12
class DirectiveDefinitionASTBuilder extends AbstractASTBuilder
13
{
14
    /**
15
     * @inheritdoc
16
     */
17
    public function supportsBuilder(string $kind): bool
18
    {
19
        return $kind === ASTKindEnum::DIRECTIVE_DEFINITION;
20
    }
21
22
    /**
23
     * @inheritdoc
24
     */
25
    public function build(LexerInterface $lexer, array $params): ?array
26
    {
27
        $start = $lexer->getToken();
28
29
        $description = $this->buildAST(ASTKindEnum::DESCRIPTION, $lexer);
30
31
        $this->expectKeyword($lexer, KeywordEnum::DIRECTIVE);
32
        $this->expect($lexer, TokenKindEnum::AT);
33
34
        $name      = $this->buildAST(ASTKindEnum::NAME, $lexer);
35
        $arguments = $this->buildAST(ASTKindEnum::ARGUMENTS_DEFINITION, $lexer);
36
37
        $this->expectKeyword($lexer, KeywordEnum::ON);
38
39
        $locations = $this->parseDirectiveLocations($lexer);
40
41
        return [
42
            'kind'        => NodeKindEnum::DIRECTIVE_DEFINITION,
43
            'description' => $description,
44
            'name'        => $name,
45
            'arguments'   => $arguments,
46
            'locations'   => $locations,
47
            'loc'         => $this->buildLocation($lexer, $start),
48
        ];
49
    }
50
51
    /**
52
     * @param LexerInterface $lexer
53
     * @return array
54
     * @throws SyntaxErrorException
55
     * @throws \ReflectionException
56
     */
57
    protected function parseDirectiveLocations(LexerInterface $lexer): array
58
    {
59
        $this->skip($lexer, TokenKindEnum::PIPE);
60
61
        $locations = [];
62
63
        do {
64
            $locations[] = $this->parseDirectiveLocation($lexer);
65
        } while ($this->skip($lexer, TokenKindEnum::PIPE));
66
67
        return $locations;
68
    }
69
70
    /**
71
     * @param LexerInterface $lexer
72
     * @return array
73
     * @throws SyntaxErrorException
74
     * @throws \ReflectionException
75
     */
76
    protected function parseDirectiveLocation(LexerInterface $lexer): array
77
    {
78
        $start = $lexer->getToken();
79
80
        $name = $this->buildAST(ASTKindEnum::NAME, $lexer);
81
82
        if (\in_array($name['value'], DirectiveLocationEnum::values(), true)) {
83
            return $name;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $name could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
84
        }
85
86
        throw $this->unexpected($lexer, $start);
87
    }
88
}
89