Completed
Pull Request — master (#148)
by Christoffer
04:19
created

SelectionSetASTBuilder::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
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 SelectionSetASTBuilder extends AbstractASTBuilder
11
{
12
    /**
13
     * @inheritdoc
14
     */
15
    public function supportsBuilder(string $kind): bool
16
    {
17
        return $kind === ASTKindEnum::SELECTION_SET;
18
    }
19
20
    /**
21
     * @inheritdoc
22
     */
23
    public function build(LexerInterface $lexer, array $params): ?array
24
    {
25
        $start = $lexer->getToken();
26
27
        return [
28
            'kind'       => NodeKindEnum::SELECTION_SET,
29
            'selections' => $this->many(
30
                $lexer,
31
                TokenKindEnum::BRACE_L,
32
                [$this, 'parseSelection'],
33
                TokenKindEnum::BRACE_R
34
            ),
35
            'loc'        => $this->buildLocation($lexer, $start),
36
        ];
37
    }
38
39
    /**
40
     * @param LexerInterface $lexer
41
     * @return array
42
     * @throws SyntaxErrorException
43
     */
44
    protected function parseSelection(LexerInterface $lexer): array
45
    {
46
        return $this->peek($lexer, TokenKindEnum::SPREAD)
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->peek($lexe...indEnum::FIELD, $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...
47
            ? $this->buildAST(ASTKindEnum::FRAGMENT, $lexer)
48
            : $this->buildAST(ASTKindEnum::FIELD, $lexer);
49
    }
50
}
51