Passed
Push — master ( e9fa3d...22a26f )
by William
03:07
created

TokensList::getPrevious()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

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