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

UnionTypeExtensionASTBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

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

2 Methods

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