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

DocumentASTBuilder::parseDefinition()   D

Complexity

Conditions 17
Paths 17

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 4.9807
c 0
b 0
f 0
cc 17
eloc 22
nc 17
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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