Completed
Push — master ( 76acb2...4e56bc )
by Nico
02:11
created

TokenStream::valid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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