Passed
Push — master ( 0efc01...ef206b )
by Maurício
02:53
created

TokensList   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Test Coverage

Coverage 98.08%

Importance

Changes 0
Metric Value
eloc 44
c 0
b 0
f 0
dl 0
loc 188
ccs 51
cts 52
cp 0.9808
rs 10
wmc 27

10 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetGet() 0 3 2
A offsetSet() 0 6 2
A offsetExists() 0 3 1
A offsetUnset() 0 9 2
A getNextOfTypeAndValue() 0 9 4
A getNextOfType() 0 9 3
A add() 0 3 1
A __construct() 0 12 3
A build() 0 18 5
A getNext() 0 12 4
1
<?php
2
/**
3
 * Defines an array of tokens and utility functions to iterate through it.
4
 */
5
6
declare(strict_types=1);
7
8
namespace PhpMyAdmin\SqlParser;
9
10
use ArrayAccess;
11
12
use function count;
13
use function is_array;
14
use function is_string;
15
16
/**
17
 * A structure representing a list of tokens.
18
 */
19
class TokensList implements ArrayAccess
20
{
21
    /**
22
     * The array of tokens.
23
     *
24
     * @var array
25
     */
26
    public $tokens = [];
27
28
    /**
29
     * The count of tokens.
30
     *
31
     * @var int
32
     */
33
    public $count = 0;
34
35
    /**
36
     * The index of the next token to be returned.
37
     *
38
     * @var int
39
     */
40
    public $idx = 0;
41
42
    /**
43
     * @param array $tokens the initial array of tokens
44
     * @param int   $count  the count of tokens in the initial array
45
     */
46 2000
    public function __construct(array $tokens = [], $count = -1)
47
    {
48 2000
        if (empty($tokens)) {
49 1984
            return;
50
        }
51
52 20
        $this->tokens = $tokens;
53 20
        if ($count !== -1) {
54
            return;
55
        }
56
57 20
        $this->count = count($tokens);
58 20
    }
59
60
    /**
61
     * Builds an array of tokens by merging their raw value.
62
     *
63
     * @param string|Token[]|TokensList $list the tokens to be built
64
     *
65
     * @return string
66
     */
67 28
    public static function build($list)
68
    {
69 28
        if (is_string($list)) {
70 4
            return $list;
71
        }
72
73 24
        if ($list instanceof self) {
74 4
            $list = $list->tokens;
75
        }
76
77 24
        $ret = '';
78 24
        if (is_array($list)) {
0 ignored issues
show
introduced by
The condition is_array($list) is always true.
Loading history...
79 24
            foreach ($list as $tok) {
80 24
                $ret .= $tok->token;
81
            }
82
        }
83
84 24
        return $ret;
85
    }
86
87
    /**
88
     * Adds a new token.
89
     *
90
     * @param Token $token token to be added in list
91
     */
92 4
    public function add(Token $token)
93
    {
94 4
        $this->tokens[$this->count++] = $token;
95 4
    }
96
97
    /**
98
     * Gets the next token. Skips any irrelevant token (whitespaces and
99
     * comments).
100
     *
101
     * @return Token|null
102
     */
103 332
    public function getNext()
104
    {
105 332
        for (; $this->idx < $this->count; ++$this->idx) {
106
            if (
107 332
                ($this->tokens[$this->idx]->type !== Token::TYPE_WHITESPACE)
108 332
                && ($this->tokens[$this->idx]->type !== Token::TYPE_COMMENT)
109
            ) {
110 332
                return $this->tokens[$this->idx++];
111
            }
112
        }
113
114 4
        return null;
115
    }
116
117
    /**
118
     * Gets the next token.
119
     *
120
     * @param int $type the type
121
     *
122
     * @return Token|null
123
     */
124 180
    public function getNextOfType($type)
125
    {
126 180
        for (; $this->idx < $this->count; ++$this->idx) {
127 180
            if ($this->tokens[$this->idx]->type === $type) {
128 180
                return $this->tokens[$this->idx++];
129
            }
130
        }
131
132 8
        return null;
133
    }
134
135
    /**
136
     * Gets the next token.
137
     *
138
     * @param int    $type  the type of the token
139
     * @param string $value the value of the token
140
     *
141
     * @return Token|null
142
     */
143 1968
    public function getNextOfTypeAndValue($type, $value)
144
    {
145 1968
        for (; $this->idx < $this->count; ++$this->idx) {
146 1968
            if (($this->tokens[$this->idx]->type === $type) && ($this->tokens[$this->idx]->value === $value)) {
147 344
                return $this->tokens[$this->idx++];
148
            }
149
        }
150
151 1968
        return null;
152
    }
153
154
    /**
155
     * Sets an value inside the container.
156
     *
157
     * @param int   $offset the offset to be set
158
     * @param Token $value  the token to be saved
159
     */
160 4
    public function offsetSet($offset, $value)
161
    {
162 4
        if ($offset === null) {
0 ignored issues
show
introduced by
The condition $offset === null is always false.
Loading history...
163 4
            $this->tokens[$this->count++] = $value;
164
        } else {
165 4
            $this->tokens[$offset] = $value;
166
        }
167 4
    }
168
169
    /**
170
     * Gets a value from the container.
171
     *
172
     * @param int $offset the offset to be returned
173
     *
174
     * @return Token
175
     */
176 28
    public function offsetGet($offset)
177
    {
178 28
        return $offset < $this->count ? $this->tokens[$offset] : null;
179
    }
180
181
    /**
182
     * Checks if an offset was previously set.
183
     *
184
     * @param int $offset the offset to be checked
185
     *
186
     * @return bool
187
     */
188 4
    public function offsetExists($offset)
189
    {
190 4
        return $offset < $this->count;
191
    }
192
193
    /**
194
     * Unsets the value of an offset.
195
     *
196
     * @param int $offset the offset to be unset
197
     */
198 4
    public function offsetUnset($offset)
199
    {
200 4
        unset($this->tokens[$offset]);
201 4
        --$this->count;
202 4
        for ($i = $offset; $i < $this->count; ++$i) {
203 4
            $this->tokens[$i] = $this->tokens[$i + 1];
204
        }
205
206 4
        unset($this->tokens[$this->count]);
207 4
    }
208
}
209