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

SelectionSetASTBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 39
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsBuilder() 0 3 1
A parseSelection() 0 5 2
A build() 0 13 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 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