Completed
Push — master ( a26615...ffffde )
by Nico
01:34
created

TokenStream   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.75%

Importance

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

10 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 1
A next() 0 4 1
A valid() 0 4 1
A current() 0 11 2
A key() 0 4 1
A rewind() 0 4 1
A getVariable() 0 8 2
A getFunction() 0 4 1
A getMethod() 0 8 2
A getStack() 0 4 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 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;
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 208
    public function next()
36
    {
37 208
        $this->stack->next();
38 208
    }
39
40 218
    public function valid(): bool
41
    {
42 218
        return $this->stack->valid();
43
    }
44
45 216
    public function current()
46
    {
47 216
        if ($this->stack->valid()) {
48
            /** @var Token\BaseToken $token */
49 216
            $token = $this->stack->current();
50
51 216
            return $token->createNode($this);
52
        }
53
54 4
        return null;
55
    }
56
57
    public function key(): int
58
    {
59
        return $this->stack->key();
60
    }
61
62 218
    public function rewind()
63
    {
64 218
        $this->stack->rewind();
65 218
    }
66
67 90
    public function getVariable(string $name): Token\BaseToken
68
    {
69
        try {
70 90
            return $this->ast->getVariable($name);
71 4
        } catch (Exception\UndefinedVariableException $e) {
72 4
            throw ParserException::undefinedVariable($name, $this->stack->current());
73
        }
74
    }
75
76 26
    public function getFunction(string $functionName): Closure
77
    {
78 26
        return $this->ast->getFunction($functionName);
79
    }
80
81 108
    public function getMethod(string $methodName, Token\BaseToken $token): CallableUserFunction
82
    {
83
        try {
84 108
            return $this->ast->getMethod($methodName, $token);
85 4
        } catch (Exception\UndefinedMethodException $e) {
86 4
            throw ParserException::undefinedMethod($methodName, $this->stack->current());
87
        }
88
    }
89
90 200
    public function getStack(): ArrayIterator
91
    {
92 200
        return $this->stack;
93
    }
94
}
95