Completed
Push — master ( 658a52...097ee5 )
by Nico
01:31
created

AST::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 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\BaseToken;
14
use nicoSWD\Rules\Tokens\TokenEncapsedString;
15
use nicoSWD\Rules\Tokens\TokenFactory;
16
use nicoSWD\Rules\Tokens\TokenFunction;
17
use nicoSWD\Rules\Tokens\TokenOpeningArray;
18
use nicoSWD\Rules\Tokens\TokenRegex;
19
use nicoSWD\Rules\Tokens\TokenString;
20
use nicoSWD\Rules\Tokens\TokenVariable;
21
use nicoSWD\Rules\AST\Nodes\NodeArray;
22
use nicoSWD\Rules\AST\Nodes\NodeFunction;
23
use nicoSWD\Rules\AST\Nodes\NodeString;
24
use nicoSWD\Rules\AST\Nodes\NodeVariable;
25
26
final class AST implements Iterator
27
{
28
    /**
29
     * @var Parser
30
     */
31
    public $parser;
32
33
    /**
34
     * @var Stack
35
     */
36
    protected $stack;
37
38
    /**
39
     * @var mixed[]
40
     */
41
    protected $variables = [];
42
43
    /**
44
     * @param Stack  $stack
45
     * @param Parser $parser
46
     */
47 226
    public function __construct(Stack $stack, Parser $parser)
48
    {
49 226
        $this->stack = $stack;
50 226
        $this->variables = $parser->variables;
51 226
        $this->parser = $parser;
52 226
    }
53
54 216
    public function next()
55
    {
56 216
        $this->stack->next();
57 216
    }
58
59 226
    public function valid() : bool
60
    {
61 226
        return $this->stack->valid();
62
    }
63
64 224
    public function current()
65
    {
66 224
        $current = $this->stack->current();
67
68 224
        switch (get_class($current)) {
69
            default:
70 214
                return $current;
71 224
            case TokenString::class:
72 224
            case TokenEncapsedString::class:
73 222
            case TokenRegex::class:
74 168
                $current = new NodeString($this);
75 168
                break;
76 220
            case TokenOpeningArray::class:
77 52
                $current = new NodeArray($this);
78 52
                break;
79 220
            case TokenVariable::class:
80 96
                $current = new NodeVariable($this);
81 96
                break;
82 218
            case TokenFunction::class:
83 30
                $current = new NodeFunction($this);
84 30
                break;
85
        }
86
87 208
        return $current->getNode();
88
    }
89
90
    /**
91
     * @codeCoverageIgnore
92
     */
93
    public function key() : int
94
    {
95
        return $this->stack->key();
96
    }
97
98 226
    public function rewind()
99
    {
100 226
        $this->stack->rewind();
101 226
    }
102
103
    /**
104
     * @throws Exceptions\ParserException
105
     */
106 96
    public function getVariable(string $name) : BaseToken
107
    {
108 96
        if (!array_key_exists($name, $this->variables)) {
109 4
            $token = $this->stack->current();
110
111 4
            throw new Exceptions\ParserException(sprintf(
112 4
                'Undefined variable "%s" at position %d on line %d',
113 4
                $name,
114 4
                $token->getPosition(),
115 4
                $token->getLine()
116
            ));
117
        }
118
119 92
        return TokenFactory::createFromPHPType($this->variables[$name]);
120
    }
121
122 208
    public function getStack() : Stack
123
    {
124 208
        return $this->stack;
125
    }
126
}
127