Completed
Push — master ( 60b30e...1b58b3 )
by Nico
01:34
created

TokenStream   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
wmc 18
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 91
ccs 44
cts 46
cp 0.9565
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 1
A next() 0 4 1
A valid() 0 4 1
C current() 0 25 7
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\Rules;
11
12
use Closure;
13
use Iterator;
14
use nicoSWD\Rules\Core\CallableUserFunction;
15
use nicoSWD\Rules\Tokens;
16
use nicoSWD\Rules\AST\Nodes;
17
18
class TokenStream implements Iterator
19
{
20
    /** @var Stack */
21
    protected $stack;
22
23
    /** @var AST */
24
    private $ast;
25
26 220
    public function create(Stack $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
        $current = $this->stack->current();
48
49 218
        switch (get_class($current)) {
50
            default:
51 208
                return $current;
52 218
            case Tokens\TokenString::class:
53 218
            case Tokens\TokenEncapsedString::class:
54 216
            case Tokens\TokenRegex::class:
55 168
                $current = new Nodes\NodeString($this);
56 168
                break;
57 214
            case Tokens\TokenOpeningArray::class:
58 52
                $current = new Nodes\NodeArray($this);
59 52
                break;
60 214
            case Tokens\TokenVariable::class:
61 90
                $current = new Nodes\NodeVariable($this);
62 90
                break;
63 212
            case Tokens\TokenFunction::class:
64 26
                $current = new Nodes\NodeFunction($this);
65 26
                break;
66
        }
67
68 202
        return $current->getNode();
69
    }
70
71
    public function key(): int
72
    {
73
       return $this->stack->key();
74
    }
75
76 220
    public function rewind()
77
    {
78 220
        $this->stack->rewind();
79 220
    }
80
81 90
    public function getVariable(string $name): Tokens\BaseToken
82
    {
83
        try {
84 90
            return $this->ast->getVariable($name);
85 4
        } catch (Exceptions\UndefinedVariableException $e) {
86 4
            throw Exceptions\ParserException::undefinedVariable($name, $this->stack->current());
87
        }
88
    }
89
90 26
    public function getFunction(string $functionName): Closure
91
    {
92 26
        return $this->ast->getFunction($functionName);
93
    }
94
95 108
    public function getMethod(string $methodName, Tokens\BaseToken $token): CallableUserFunction
96
    {
97
        try {
98 108
            return $this->ast->getMethod($methodName, $token);
99 4
        } catch (Exceptions\UndefinedMethodException $e) {
100 4
            throw Exceptions\ParserException::undefinedMethod($methodName, $this->stack->current());
101
        }
102
    }
103
104 202
    public function getStack(): Stack
105
    {
106 202
        return $this->stack;
107
    }
108
}
109