Passed
Push — master ( c47e08...96e565 )
by Quang
02:51
created

TypeReferenceASTBuilder::parseTypeReference()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
nc 4
nop 1
1
<?php
2
3
namespace Digia\GraphQL\Language\ASTBuilder;
4
5
use Digia\GraphQL\Error\SyntaxErrorException;
6
use Digia\GraphQL\Language\LexerInterface;
7
use Digia\GraphQL\Language\Node\NodeKindEnum;
8
use Digia\GraphQL\Language\TokenKindEnum;
9
10
class TypeReferenceASTBuilder extends AbstractASTBuilder
11
{
12
    /**
13
     * @inheritdoc
14
     */
15
    public function supportsBuilder(string $kind): bool
16
    {
17
        return $kind === ASTKindEnum::TYPE_REFERENCE;
18
    }
19
20
    /**
21
     * @inheritdoc
22
     */
23
    public function build(LexerInterface $lexer, array $params): ?array
24
    {
25
        return $this->parseTypeReference($lexer);
26
    }
27
28
    /**
29
     * @param LexerInterface $lexer
30
     * @return array
31
     * @throws SyntaxErrorException
32
     */
33
    protected function parseTypeReference(LexerInterface $lexer): array
34
    {
35
        $start = $lexer->getToken();
36
37
        if ($this->skip($lexer, TokenKindEnum::BRACKET_L)) {
38
            $type = $this->parseTypeReference($lexer);
39
40
            $this->expect($lexer, TokenKindEnum::BRACKET_R);
41
42
            $type = [
43
                'kind' => NodeKindEnum::LIST_TYPE,
44
                'type' => $type,
45
                'loc'  => $this->buildLocation($lexer, $start),
46
            ];
47
        } else {
48
            $type = $this->buildAST(ASTKindEnum::NAMED_TYPE, $lexer);
49
        }
50
51
        if ($this->skip($lexer, TokenKindEnum::BANG)) {
52
            return [
53
                'kind' => NodeKindEnum::NON_NULL_TYPE,
54
                'type' => $type,
55
                'loc'  => $this->buildLocation($lexer, $start),
56
            ];
57
        }
58
59
        return $type;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $type 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
    }
61
}
62