NodeParserChain   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 52
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addNodeParser() 0 5 1
A parse() 0 17 3
A supports() 0 10 3
1
<?php
2
namespace Xiag\Rql\Parser;
3
4
use Xiag\Rql\Parser\Exception\SyntaxErrorException;
5
6
class NodeParserChain implements NodeParserInterface
7
{
8
    /**
9
     * @var NodeParserInterface[]
10
     */
11
    protected $nodeParsers;
12
13
    /**
14
     * @param NodeParserInterface $nodeParser
15
     * @return $this
16
     */
17 64
    public function addNodeParser(NodeParserInterface $nodeParser)
18
    {
19 64
        $this->nodeParsers[] = $nodeParser;
20 64
        return $this;
21
    }
22
23
    /**
24
     * @inheritdoc
25
     */
26 64
    public function parse(TokenStream $tokenStream)
27
    {
28 64
        foreach ($this->nodeParsers as $nodeParser) {
29 64
            if ($nodeParser->supports($tokenStream)) {
30 63
                return $nodeParser->parse($tokenStream);
31
            }
32 64
        }
33
34 1
        throw new SyntaxErrorException(
35 1
            sprintf(
36 1
                'Unexpected token "%s" (%s) at position %d',
37 1
                $tokenStream->getCurrent()->getValue(),
38 1
                $tokenStream->getCurrent()->getName(),
39 1
                $tokenStream->getCurrent()->getStart()
40 1
            )
41 1
        );
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47 64
    public function supports(TokenStream $tokenStream)
48
    {
49 64
        foreach ($this->nodeParsers as $nodeParser) {
50 64
            if ($nodeParser->supports($tokenStream)) {
51 58
                return true;
52
            }
53 59
        }
54
55 6
        return false;
56
    }
57
}
58