Completed
Pull Request — master (#4)
by Nico
02:45 queued 31s
created

AST   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 92.5%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 8
dl 0
loc 100
ccs 37
cts 40
cp 0.925
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A valid() 0 4 1
A __construct() 0 6 1
A next() 0 4 1
B current() 0 24 6
A key() 0 4 1
A rewind() 0 4 1
A getVariable() 0 15 2
A getStack() 0 4 1
1
<?php
2
3
/**
4
 * @license     http://opensource.org/licenses/mit-license.php MIT
5
 * @link        https://github.com/nicoSWD
6
 * @author      Nicolas Oelgart <[email protected]>
7
 */
8
declare(strict_types=1);
9
10
namespace nicoSWD\Rules;
11
12
use Iterator;
13
use nicoSWD\Rules\Tokens\{
14
    BaseToken,
15
    TokenFactory,
16
    TokenFunction,
17
    TokenOpeningArray,
18
    TokenRegex,
19
    TokenString,
20
    TokenVariable
21
};
22
use nicoSWD\Rules\AST\Nodes\{
23
    NodeArray,
24
    NodeFunction,
25
    NodeString,
26
    NodeVariable
27
};
28
29
final class AST implements Iterator
30
{
31
    /**
32
     * @var Parser
33
     */
34
    public $parser;
35
36
    /**
37
     * @var Stack
38
     */
39
    protected $stack;
40
41
    /**
42
     * @var mixed[]
43
     */
44
    protected $variables = [];
45
46
    /**
47
     * @param Stack  $stack
48
     * @param Parser $parser
49
     */
50 220
    public function __construct(Stack $stack, Parser $parser)
51
    {
52 220
        $this->stack = $stack;
53 220
        $this->variables = $parser->variables;
54 220
        $this->parser = $parser;
55 220
    }
56
57 210
    public function next()
58
    {
59 210
        $this->stack->next();
60 210
    }
61
62 220
    public function valid() : bool
63
    {
64 220
        return $this->stack->valid();
65
    }
66
67 218
    public function current()
68
    {
69 218
        $current = $this->stack->current();
70
71
        switch (true) {
72
            default:
73 208
                return $current;
74 218
            case $current instanceof TokenString:
75
            case $current instanceof TokenRegex:
76 168
                $current = new NodeString($this);
77 168
                break;
78
            case $current instanceof TokenOpeningArray:
79 52
                $current = new NodeArray($this);
80 52
                break;
81
            case $current instanceof TokenVariable:
82 94
                $current = new NodeVariable($this);
83 94
                break;
84 208
            case $current instanceof TokenFunction:
85 24
                $current = new NodeFunction($this);
86 24
                break;
87
        }
88
89 202
        return $current->getNode();
90
    }
91
92
    /**
93
     * @codeCoverageIgnore
94
     */
95
    public function key() : int
96
    {
97
        return $this->stack->key();
98
    }
99
100 220
    public function rewind()
101
    {
102 220
        $this->stack->rewind();
103 220
    }
104
105
    /**
106
     * @throws Exceptions\ParserException
107
     */
108 94
    public function getVariable(string $name) : BaseToken
109
    {
110 94
        if (!array_key_exists($name, $this->variables)) {
111 4
            $token = $this->stack->current();
112
113 4
            throw new Exceptions\ParserException(sprintf(
114 4
                'Undefined variable "%s" at position %d on line %d',
115
                $name,
116 4
                $token->getPosition(),
117 4
                $token->getLine()
118
            ));
119
        }
120
121 90
        return TokenFactory::createFromPHPType($this->variables[$name]);
122
    }
123
124 202
    public function getStack() : Stack
125
    {
126 202
        return $this->stack;
127
    }
128
}
129