Passed
Pull Request — master (#376)
by
unknown
02:41
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 2296
    public function __construct(array $tokens = [], $count = -1)
48
    {
49 2296
        if (empty($tokens)) {
50 2276
            return;
51
        }
52
53 96
        $this->tokens = $tokens;
54 96
        $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 40
    public static function build($list)
65
    {
66 40
        if (is_string($list)) {
67 4
            return $list;
68
        }
69
70 36
        if ($list instanceof self) {
71 4
            $list = $list->tokens;
72
        }
73
74 36
        $ret = '';
75 36
        if (is_array($list)) {
0 ignored issues
show
introduced by
The condition is_array($list) is always true.
Loading history...
76 36
            foreach ($list as $tok) {
77 32
                $ret .= $tok->token;
78
            }
79
        }
80
81 36
        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 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
     * @return Token|null
0 ignored issues
show
introduced by
Method \PhpMyAdmin\SqlParser\TokensList::getPrevious() has useless @return annotation.
Loading history...
121
     */
122 12
    public function getPrevious(): ?Token
123
    {
124 12
        for (; $this->idx > 0; --$this->idx) {
125
            if (
126 12
                ($this->tokens[$this->idx]->type !== Token::TYPE_WHITESPACE)
127 12
                && ($this->tokens[$this->idx]->type !== Token::TYPE_COMMENT)
128
            ) {
129 12
                return $this->tokens[$this->idx--];
130
            }
131
        }
132
133 4
        return null;
134
    }
135
136
    /**
137
     * Gets the next token.
138
     *
139
     * @param int $type the type
140
     *
141
     * @return Token|null
142
     */
143 208
    public function getNextOfType($type)
144
    {
145 208
        for (; $this->idx < $this->count; ++$this->idx) {
146 208
            if ($this->tokens[$this->idx]->type === $type) {
147 208
                return $this->tokens[$this->idx++];
148
            }
149
        }
150
151 8
        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
     */
162 2260
    public function getNextOfTypeAndValue($type, $value)
163
    {
164 2260
        for (; $this->idx < $this->count; ++$this->idx) {
165 2260
            if (($this->tokens[$this->idx]->type === $type) && ($this->tokens[$this->idx]->value === $value)) {
166 400
                return $this->tokens[$this->idx++];
167
            }
168
        }
169
170 2260
        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
     *
179
     * @return void
180
     */
181 2
    #[\ReturnTypeWillChange]
182 2
    public function offsetSet($offset, $value)
183
    {
184 4
        if ($offset === null) {
185 4
            $this->tokens[$this->count++] = $value;
186
        } else {
187 4
            $this->tokens[$offset] = $value;
188
        }
189
    }
190
191
    /**
192
     * Gets a value from the container.
193
     *
194
     * @param int $offset the offset to be returned
195
     *
196
     * @return Token|null
197
     */
198 14
    #[\ReturnTypeWillChange]
199 14
    public function offsetGet($offset)
200
    {
201 28
        return $offset < $this->count ? $this->tokens[$offset] : null;
202
    }
203
204
    /**
205
     * Checks if an offset was previously set.
206
     *
207
     * @param int $offset the offset to be checked
208
     *
209
     * @return bool
210
     */
211 2
    #[\ReturnTypeWillChange]
212 2
    public function offsetExists($offset)
213
    {
214 4
        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 2
    #[\ReturnTypeWillChange]
225 2
    public function offsetUnset($offset)
226
    {
227 4
        unset($this->tokens[$offset]);
228 4
        --$this->count;
229 4
        for ($i = $offset; $i < $this->count; ++$i) {
230 4
            $this->tokens[$i] = $this->tokens[$i + 1];
231
        }
232
233 4
        unset($this->tokens[$this->count]);
234
    }
235
}
236