Completed
Push — master ( ffffde...8cec06 )
by Nico
01:31
created

TokenStream::getVariable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 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 Iterator;
15
use nicoSWD\Rule\Parser\Exception\ParserException;
16
use nicoSWD\Rule\Grammar\CallableUserFunction;
17
use nicoSWD\Rule\TokenStream\Token\BaseToken;
18
19
class TokenStream implements Iterator
20
{
21
    /** @var ArrayIterator */
22
    protected $stack;
23
    /** @var AST */
24
    private $ast;
25
26 218
    public function create(ArrayIterator $stack, AST $ast)
27
    {
28 218
        $stream = new self();
29 218
        $stream->stack = $stack;
30 218
        $stream->ast = $ast;
31
32 218
        return $stream;
33
    }
34
35 210
    public function next()
36
    {
37 210
        $this->stack->next();
38 210
    }
39
40 218
    public function valid(): bool
41
    {
42 218
        return $this->stack->valid();
43
    }
44
45 216
    public function current(): BaseToken
46
    {
47 216
        return $this->getCurrentToken()->createNode($this);
48
    }
49
50
    public function key(): int
51
    {
52
        return $this->stack->key();
53
    }
54
55 218
    public function rewind()
56
    {
57 218
        $this->stack->rewind();
58 218
    }
59
60 200
    public function getStack(): ArrayIterator
61
    {
62 200
        return $this->stack;
63
    }
64
65 216
    private function getCurrentToken(): BaseToken
66
    {
67 216
        return $this->stack->current();
68
    }
69
70 90
    public function getVariable(string $variableName): BaseToken
71
    {
72
        try {
73 90
            return $this->ast->getVariable($variableName);
74 4
        } catch (Exception\UndefinedVariableException $e) {
75 4
            throw ParserException::undefinedVariable($variableName, $this->getCurrentToken());
76
        }
77
    }
78
79 26
    public function getFunction(string $functionName): Closure
80
    {
81
        try {
82 26
            return $this->ast->getFunction($functionName);
83 4
        } catch (Exception\UndefinedFunctionException $e) {
84 4
            throw ParserException::undefinedFunction($functionName, $this->getCurrentToken());
85
        }
86
    }
87
88 108
    public function getMethod(string $methodName, BaseToken $token): CallableUserFunction
89
    {
90
        try {
91 108
            return $this->ast->getMethod($methodName, $token);
92 4
        } catch (Exception\UndefinedMethodException $e) {
93 4
            throw ParserException::undefinedMethod($methodName, $this->getCurrentToken());
94
        }
95
    }
96
}
97