Passed
Pull Request — master (#145)
by Christoffer
03:59 queued 01:32
created

UnionTypeExtensionASTBuilder::build()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 14
nc 2
nop 2
1
<?php
2
3
namespace Digia\GraphQL\Language\ASTBuilder;
4
5
use Digia\GraphQL\Language\KeywordEnum;
6
use Digia\GraphQL\Language\LexerInterface;
7
use Digia\GraphQL\Language\Node\NodeKindEnum;
8
use Digia\GraphQL\Language\TokenKindEnum;
9
10
class UnionTypeExtensionASTBuilder extends AbstractASTBuilder
11
{
12
    /**
13
     * @inheritdoc
14
     */
15
    public function supportsBuilder(string $kind): bool
16
    {
17
        return $kind === ASTKindEnum::UNION_TYPE_EXTENSION;
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::EXTEND);
28
        $this->expectKeyword($lexer, KeywordEnum::UNION);
29
30
        $name       = $this->buildAST(ASTKindEnum::NAME, $lexer);
31
        $directives = $this->buildAST(ASTKindEnum::DIRECTIVES, $lexer);
32
        $types      = $this->buildAST(ASTKindEnum::UNION_MEMBER_TYPES, $lexer);
33
34
        if (count($directives) === 0 && count($types) === 0) {
35
            throw $this->unexpected($lexer);
36
        }
37
38
        return [
39
            'kind'       => NodeKindEnum::UNION_TYPE_EXTENSION,
40
            'name'       => $name,
41
            'directives' => $directives,
42
            'types'      => $types,
43
            'loc'        => $this->buildLocation($lexer, $start),
44
        ];
45
    }
46
}
47