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

FragmentDefinitionASTBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsBuilder() 0 3 1
A build() 0 19 1
A parseFragmentName() 0 7 2
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
10
class FragmentDefinitionASTBuilder extends AbstractASTBuilder
11
{
12
    /**
13
     * @inheritdoc
14
     */
15
    public function supportsBuilder(string $kind): bool
16
    {
17
        return $kind === ASTKindEnum::FRAGMENT_DEFINITION;
18
    }
19
20
    /**
21
     * @inheritdoc
22
     */
23
    public function build(LexerInterface $lexer, array $params): ?array
24
    {
25
        $start = $lexer->getToken();
26
27
        $this->expectKeyword($lexer, KeywordEnum::FRAGMENT);
28
29
        $parseTypeCondition = function (LexerInterface $lexer) {
30
            $this->expectKeyword($lexer, 'on');
31
            return $this->buildAST(ASTKindEnum::NAMED_TYPE, $lexer);
32
        };
33
34
        return [
35
            'kind'                => NodeKindEnum::FRAGMENT_DEFINITION,
36
            'name'                => $this->parseFragmentName($lexer),
37
            'variableDefinitions' => $this->buildAST(ASTKindEnum::VARIABLE_DEFINITION, $lexer),
38
            'typeCondition'       => $parseTypeCondition($lexer),
39
            'directives'          => $this->buildAST(ASTKindEnum::DIRECTIVES, $lexer),
40
            'selectionSet'        => $this->buildAST(ASTKindEnum::SELECTION_SET, $lexer),
41
            'loc'                 => $this->buildLocation($lexer, $start),
42
        ];
43
    }
44
45
    /**
46
     * @param LexerInterface $lexer
47
     * @return array
48
     * @throws SyntaxErrorException
49
     */
50
    protected function parseFragmentName(LexerInterface $lexer): array
51
    {
52
        if ($lexer->getTokenValue() === 'on') {
53
            throw $this->unexpected($lexer);
54
        }
55
56
        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...
57
    }
58
}
59