Passed
Pull Request — master (#376)
by
unknown
08:22
created

TokensList::getPrevious()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 0
dl 0
loc 12
ccs 5
cts 5
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 2288
    public function __construct(array $tokens = [], $count = -1)
48
    {
49 2288
        if (empty($tokens)) {
50 2272
            return;
51
        }
52
53 92
        $this->tokens = $tokens;
54 92
        $this->count = $count === -1 ? count($tokens) : $count;
55
    }
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 36
    public static function build($list)
65
    {
66 36
        if (is_string($list)) {
67 4
            return $list;
68
        }
69
70 32
        if ($list instanceof self) {
71 4
            $list = $list->tokens;
72
        }
73
74 32
        $ret = '';
75 32
        if (is_array($list)) {
0 ignored issues
show
introduced by
The condition is_array($list) is always true.
Loading history...
76 32
            foreach ($list as $tok) {
77 32
                $ret .= $tok->token;
78
            }
79
        }
80
81 32
        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
    }
95
96
    /**
97
     * Gets the next token. Skips any irrelevant token (whitespaces and
98
     * comments).
99
     *
100
     * @return Token|null
101
     */
102 576
    public function getNext()
103
    {
104 576
        for (; $this->idx < $this->count; ++$this->idx) {
105
            if (
106 576
                ($this->tokens[$this->idx]->type !== Token::TYPE_WHITESPACE)
107 576
                && ($this->tokens[$this->idx]->type !== Token::TYPE_COMMENT)
108
            ) {
109 576
                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
     * @return Token|null
0 ignored issues
show
introduced by
Method \PhpMyAdmin\SqlParser\TokensList::getPrevious() has useless @return annotation.
Loading history...
121
     */
122
    public function getPrevious(): ?Token
123 208
    {
124
        for (; $this->idx > 0; --$this->idx) {
125 208
            if (
126 208
                ($this->tokens[$this->idx]->type !== Token::TYPE_WHITESPACE)
127 208
                && ($this->tokens[$this->idx]->type !== Token::TYPE_COMMENT)
128
            ) {
129
                return $this->tokens[$this->idx--];
130
            }
131 8
        }
132
133
        return null;
134
    }
135
136
    /**
137
     * Gets the next token.
138
     *
139
     * @param int $type the type
140
     *
141
     * @return Token|null
142 2256
     */
143
    public function getNextOfType($type)
144 2256
    {
145 2256
        for (; $this->idx < $this->count; ++$this->idx) {
146 400
            if ($this->tokens[$this->idx]->type === $type) {
147
                return $this->tokens[$this->idx++];
148
            }
149
        }
150 2256
151
        return null;
152
    }
153
154
    /**
155
     * Gets the next token.
156
     *
157
     * @param int    $type  the type of the token
158
     * @param string $value the value of the token
159
     *
160
     * @return Token|null
161 3
     */
162 1
    public function getNextOfTypeAndValue($type, $value)
163
    {
164 4
        for (; $this->idx < $this->count; ++$this->idx) {
165 4
            if (($this->tokens[$this->idx]->type === $type) && ($this->tokens[$this->idx]->value === $value)) {
166
                return $this->tokens[$this->idx++];
167 4
            }
168
        }
169
170
        return null;
171
    }
172
173
    /**
174
     * Sets an value inside the container.
175
     *
176
     * @param int|null $offset the offset to be set
177
     * @param Token    $value  the token to be saved
178 21
     *
179 7
     * @return void
180
     */
181 28
    #[\ReturnTypeWillChange]
182
    public function offsetSet($offset, $value)
183
    {
184
        if ($offset === null) {
185
            $this->tokens[$this->count++] = $value;
186
        } else {
187
            $this->tokens[$offset] = $value;
188
        }
189
    }
190
191 3
    /**
192 1
     * Gets a value from the container.
193
     *
194 4
     * @param int $offset the offset to be returned
195
     *
196
     * @return Token|null
197
     */
198
    #[\ReturnTypeWillChange]
199
    public function offsetGet($offset)
200
    {
201
        return $offset < $this->count ? $this->tokens[$offset] : null;
202
    }
203
204 3
    /**
205 1
     * Checks if an offset was previously set.
206
     *
207 4
     * @param int $offset the offset to be checked
208 4
     *
209 4
     * @return bool
210 4
     */
211
    #[\ReturnTypeWillChange]
212
    public function offsetExists($offset)
213 4
    {
214
        return $offset < $this->count;
215
    }
216
217
    /**
218
     * Unsets the value of an offset.
219
     *
220
     * @param int $offset the offset to be unset
221
     *
222
     * @return void
223
     */
224
    #[\ReturnTypeWillChange]
225
    public function offsetUnset($offset)
226
    {
227
        unset($this->tokens[$offset]);
228
        --$this->count;
229
        for ($i = $offset; $i < $this->count; ++$i) {
230
            $this->tokens[$i] = $this->tokens[$i + 1];
231
        }
232
233
        unset($this->tokens[$this->count]);
234
    }
235
}
236