Completed
Push — master ( 317706...046a0a )
by Nico
01:26
created

TokenStream   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 75
ccs 30
cts 32
cp 0.9375
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A next() 0 4 1
A valid() 0 4 1
A current() 0 4 1
A key() 0 4 1
A rewind() 0 4 1
A getStack() 0 4 1
A getCurrentToken() 0 4 1
A getVariable() 0 8 2
A getFunction() 0 8 2
A getMethod() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @license     http://opensource.org/licenses/mit-license.php MIT
7
 * @link        https://github.com/nicoSWD
8
 * @author      Nicolas Oelgart <[email protected]>
9
 */
10
namespace nicoSWD\Rule\TokenStream;
11
12
use ArrayIterator;
13
use Closure;
14
use nicoSWD\Rule\Parser\Exception\ParserException;
15
use nicoSWD\Rule\Grammar\CallableUserFunction;
16
use nicoSWD\Rule\TokenStream\Token\BaseToken;
17
18
class TokenStream extends ArrayIterator
19
{
20
    /** @var ArrayIterator */
21
    private $stack;
22
    /** @var AST */
23
    private $ast;
24
25 218
    public function __construct(ArrayIterator $stack, AST $ast)
26
    {
27 218
        $this->stack = $stack;
28 218
        $this->ast = $ast;
29 218
    }
30
31 206
    public function next()
32
    {
33 206
        $this->stack->next();
34 206
    }
35
36 218
    public function valid(): bool
37
    {
38 218
        return $this->stack->valid();
39
    }
40
41 216
    public function current(): BaseToken
42
    {
43 216
        return $this->getCurrentToken()->createNode($this);
44
    }
45
46
    public function key(): int
47
    {
48
        return $this->stack->key();
49
    }
50
51 218
    public function rewind()
52
    {
53 218
        $this->stack->rewind();
54 218
    }
55
56 200
    public function getStack(): ArrayIterator
57
    {
58 200
        return $this->stack;
59
    }
60
61 216
    private function getCurrentToken(): BaseToken
62
    {
63 216
        return $this->stack->current();
64
    }
65
66 90
    public function getVariable(string $variableName): BaseToken
67
    {
68
        try {
69 90
            return $this->ast->getVariable($variableName);
70 4
        } catch (Exception\UndefinedVariableException $e) {
71 4
            throw ParserException::undefinedVariable($variableName, $this->getCurrentToken());
72
        }
73
    }
74
75 26
    public function getFunction(string $functionName): Closure
76
    {
77
        try {
78 26
            return $this->ast->getFunction($functionName);
79 4
        } catch (Exception\UndefinedFunctionException $e) {
80 4
            throw ParserException::undefinedFunction($functionName, $this->getCurrentToken());
81
        }
82
    }
83
84 108
    public function getMethod(string $methodName, BaseToken $token): CallableUserFunction
85
    {
86
        try {
87 108
            return $this->ast->getMethod($methodName, $token);
88 4
        } catch (Exception\UndefinedMethodException $e) {
89 4
            throw ParserException::undefinedMethod($methodName, $this->getCurrentToken());
90
        }
91
    }
92
}
93