Passed
Pull Request — master (#145)
by Christoffer
02:48 queued 25s
created

DocumentASTBuilder   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 20
dl 0
loc 69
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
D parseDefinition() 0 29 17
A supportsBuilder() 0 3 1
A build() 0 16 2
1
<?php
2
3
namespace Digia\GraphQL\Language\ASTBuilder;
4
5
use Digia\GraphQL\Error\SyntaxErrorException;
6
use Digia\GraphQL\Language\KeywordEnum;
7
use Digia\GraphQL\Language\LexerInterface;
8
use Digia\GraphQL\Language\Node\NodeKindEnum;
9
use Digia\GraphQL\Language\TokenKindEnum;
10
11
class DocumentASTBuilder extends AbstractASTBuilder
12
{
13
    /**
14
     * @param LexerInterface $lexer
15
     * @return bool
16
     */
17
    public function supportsBuilder(string $kind): bool
18
    {
19
        return $kind === ASTKindEnum::DOCUMENT;
20
    }
21
22
    /**
23
     * @inheritdoc
24
     * @throws SyntaxErrorException
25
     */
26
    public function build(LexerInterface $lexer, array $params): ?array
27
    {
28
        $start = $lexer->getToken();
29
30
        $this->expect($lexer, TokenKindEnum::SOF);
31
32
        $definitions = [];
33
34
        do {
35
            $definitions[] = $this->parseDefinition($lexer);
36
        } while (!$this->skip($lexer, TokenKindEnum::EOF));
37
38
        return [
39
            'kind'        => NodeKindEnum::DOCUMENT,
40
            'definitions' => $definitions,
41
            'loc'         => $this->buildLocation($lexer, $start),
42
        ];
43
    }
44
45
    /**
46
     * @param LexerInterface $lexer
47
     * @return array
48
     * @throws SyntaxErrorException
49
     * @throws \ReflectionException
50
     */
51
    protected function parseDefinition(LexerInterface $lexer): array
52
    {
53
        if ($this->peek($lexer, TokenKindEnum::NAME)) {
54
            switch ($lexer->getTokenValue()) {
55
                case KeywordEnum::QUERY:
56
                case KeywordEnum::MUTATION:
57
                case KeywordEnum::SUBSCRIPTION:
58
                case KeywordEnum::FRAGMENT:
59
                    return $this->buildAST(ASTKindEnum::EXECUTABLE_DEFINITION, $lexer);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->buildAST(D...BLE_DEFINITION, $lexer) 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...
60
                case KeywordEnum::SCHEMA:
61
                case KeywordEnum::SCALAR:
62
                case KeywordEnum::TYPE:
63
                case KeywordEnum::INTERFACE:
64
                case KeywordEnum::UNION:
65
                case KeywordEnum::ENUM:
66
                case KeywordEnum::INPUT:
67
                case KeywordEnum::EXTEND:
68
                case KeywordEnum::DIRECTIVE:
69
                    // Note: The schema definition language is an experimental addition.
70
                    return $this->buildAST(ASTKindEnum::TYPE_SYSTEM_DEFINITION, $lexer);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->buildAST(D...TEM_DEFINITION, $lexer) 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...
71
            }
72
        } elseif ($this->peek($lexer, TokenKindEnum::BRACE_L)) {
73
            return $this->buildAST(ASTKindEnum::EXECUTABLE_DEFINITION, $lexer);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->buildAST(D...BLE_DEFINITION, $lexer) 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...
74
        } elseif ($this->peekDescription($lexer)) {
75
            // Note: The schema definition language is an experimental addition.
76
            return $this->buildAST(ASTKindEnum::TYPE_SYSTEM_DEFINITION, $lexer);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->buildAST(D...TEM_DEFINITION, $lexer) 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...
77
        }
78
79
        throw $this->unexpected($lexer);
80
    }
81
}
82