Parser   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 48
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A parseTokenStream() 0 15 2
A unfoldStack() 0 10 2
1
<?php
2
3
4
namespace CondParse;
5
6
7
use CondParse\Exception\ParserException;
8
9
class Parser implements ParserInterface
10
{
11
    /** @var TokenParser */
12
    private $tokenParser;
13
14 14
    public function __construct(TokenParser $tokenParser = null)
15
    {
16 14
        $this->tokenParser = $tokenParser ?: new TokenParser;
17 14
    }
18
19
    /**
20
     * @param \Traversable $tokenStream
21
     * @param TokenMap $tokenMap
22
     * @return OperandInterface
23
     * @throws ParserException
24
     */
25 14
    public function parseTokenStream(\Traversable $tokenStream, TokenMap $tokenMap)
26
    {
27 14
        $operandStack = new OperandStack();
28 14
        $operatorStack = new \SplStack();
29
30 14
        foreach ($tokenStream as $lexerToken) {
31 13
            $this->tokenParser->parseToken(
32 13
                new TokenParserParameter($lexerToken, $operandStack, $operatorStack, $tokenMap)
33 13
            );
34 14
        }
35
36 14
        $this->unfoldStack($tokenMap, $operatorStack, $operandStack);
37
38 14
        return $operandStack->top();
39
    }
40
41
    /**
42
     * @param TokenMap $tokenMap
43
     * @param \SplStack $operatorStack
44
     * @param OperandStack $operandStack
45
     */
46 14
    protected function unfoldStack(TokenMap $tokenMap, $operatorStack, $operandStack)
47
    {
48 14
        while (!$operatorStack->isEmpty()) {
49 8
            $operandStack->push(
50
                $tokenMap
51 8
                    ->buildOperand($operatorStack->pop())
52 8
                    ->consumeTokens($operandStack)
53 8
            );
54 8
        }
55 14
    }
56
}
57