Passed
Push — master ( c47e08...96e565 )
by Quang
02:51
created

Parser::parseTypeAST()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 9.4285
1
<?php
2
3
namespace Digia\GraphQL\Language;
4
5
use Digia\GraphQL\Error\SyntaxErrorException;
6
use Digia\GraphQL\Language\ASTBuilder\ASTDirectorInterface;
7
use Digia\GraphQL\Language\ASTBuilder\ASTKindEnum;
8
use Digia\GraphQL\Language\Node\NodeInterface;
9
use Digia\GraphQL\Language\NodeBuilder\NodeDirectorInterface;
10
11
class Parser implements ParserInterface
12
{
13
    use LexerUtilsTrait;
14
15
    /**
16
     * @var ASTDirectorInterface
17
     */
18
    protected $astDirector;
19
20
    /**
21
     * @var NodeDirectorInterface
22
     */
23
    protected $nodeDirector;
24
25
    /**
26
     * Parser constructor.
27
     * @param ASTDirectorInterface  $astDirector
28
     * @param NodeDirectorInterface $nodeDirector
29
     */
30
    public function __construct(ASTDirectorInterface $astDirector, NodeDirectorInterface $nodeDirector)
31
    {
32
        $this->astDirector  = $astDirector;
33
        $this->nodeDirector = $nodeDirector;
34
    }
35
36
    /**
37
     * @inheritdoc
38
     * @return NodeInterface
39
     */
40
    public function parse(LexerInterface $lexer): NodeInterface
41
    {
42
        return $this->nodeDirector->build($this->parseAST($lexer));
43
    }
44
45
    /**
46
     * @inheritdoc
47
     * @return NodeInterface
48
     */
49
    public function parseValue(LexerInterface $lexer): NodeInterface
50
    {
51
        return $this->nodeDirector->build($this->parseValueAST($lexer));
52
    }
53
54
    /**
55
     * @inheritdoc
56
     * @return NodeInterface
57
     */
58
    public function parseType(LexerInterface $lexer): NodeInterface
59
    {
60
        return $this->nodeDirector->build($this->parseTypeAST($lexer));
61
    }
62
63
    /**
64
     * @param LexerInterface $lexer
65
     * @return array
66
     * @throws SyntaxErrorException
67
     * @throws \ReflectionException
68
     */
69
    protected function parseAST(LexerInterface $lexer): array
70
    {
71
        return $this->astDirector->build(ASTKindEnum::DOCUMENT, $lexer);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->astDirecto...Enum::DOCUMENT, $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...
72
    }
73
74
    /**
75
     * @param LexerInterface $lexer
76
     * @return array
77
     * @throws SyntaxErrorException
78
     */
79
    protected function parseValueAST(LexerInterface $lexer): array
80
    {
81
        $this->expect($lexer, TokenKindEnum::SOF);
82
        $value = $this->astDirector->build(ASTKindEnum::VALUE_LITERAL, $lexer, ['isConst' => false]);
83
        $this->expect($lexer, TokenKindEnum::EOF);
84
85
        return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value 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...
86
    }
87
88
    /**
89
     * @param LexerInterface $lexer
90
     * @return array
91
     * @throws SyntaxErrorException
92
     */
93
    protected function parseTypeAST(LexerInterface $lexer): array
94
    {
95
        $this->expect($lexer, TokenKindEnum::SOF);
96
        $type = $this->astDirector->build(ASTKindEnum::TYPE_REFERENCE, $lexer);
97
        $this->expect($lexer, TokenKindEnum::EOF);
98
99
        return $type;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $type 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...
100
    }
101
}
102