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

FragmentASTBuilder::build()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 18
nc 3
nop 2
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 FragmentASTBuilder extends AbstractASTBuilder
11
{
12
    /**
13
     * @inheritdoc
14
     */
15
    public function supportsBuilder(string $kind): bool
16
    {
17
        return $kind === ASTKindEnum::FRAGMENT;
18
    }
19
20
    /**
21
     * @inheritdoc
22
     */
23
    public function build(LexerInterface $lexer, array $params): ?array
24
    {
25
        $start = $lexer->getToken();
26
27
        $this->expect($lexer, TokenKindEnum::SPREAD);
28
29
        $tokenValue = $lexer->getTokenValue();
30
31
        if ($tokenValue !== 'on' && $this->peek($lexer, TokenKindEnum::NAME)) {
32
            return [
33
                'kind'       => NodeKindEnum::FRAGMENT_SPREAD,
34
                'name'       => $this->parseFragmentName($lexer),
35
                'directives' => $this->buildAST(ASTKindEnum::DIRECTIVES, $lexer),
36
                'loc'        => $this->buildLocation($lexer, $start),
37
            ];
38
        }
39
40
        if ($tokenValue === 'on') {
41
            $lexer->advance();
42
43
            $typeCondition = $this->buildAST(ASTKindEnum::NAMED_TYPE, $lexer);
44
        }
45
46
        return [
47
            'kind'          => NodeKindEnum::INLINE_FRAGMENT,
48
            'typeCondition' => $typeCondition ?? null,
49
            'directives'    => $this->buildAST(ASTKindEnum::DIRECTIVES, $lexer),
50
            'selectionSet'  => $this->buildAST(ASTKindEnum::SELECTION_SET, $lexer),
51
            'loc'           => $this->buildLocation($lexer, $start),
52
        ];
53
    }
54
55
    /**
56
     * @param LexerInterface $lexer
57
     * @return array
58
     * @throws SyntaxErrorException
59
     */
60
    protected function parseFragmentName(LexerInterface $lexer): array
61
    {
62
        if ($lexer->getTokenValue() === 'on') {
63
            throw $this->unexpected($lexer);
64
        }
65
66
        return $this->buildAST(ASTKindEnum::NAME, $lexer);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->buildAST(D...KindEnum::NAME, $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...
67
    }
68
}
69