Passed
Push — master ( 8d8336...ff76ee )
by William
291:59 queued 237:03
created

TokensList::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 2016
    public function __construct(array $tokens = [], $count = -1)
47
    {
48 2016
        if (empty($tokens)) {
49 2000
            return;
50
        }
51
52 32
        $this->tokens = $tokens;
53 32
        $this->count = $count === -1 ? count($tokens) : $count;
54 32
    }
55
56
    /**
57
     * Builds an array of tokens by merging their raw value.
58
     *
59
     * @param string|Token[]|TokensList $list the tokens to be built
60
     *
61
     * @return string
62
     */
63 28
    public static function build($list)
64
    {
65 28
        if (is_string($list)) {
66 4
            return $list;
67
        }
68
69 24
        if ($list instanceof self) {
70 4
            $list = $list->tokens;
71
        }
72
73 24
        $ret = '';
74 24
        if (is_array($list)) {
0 ignored issues
show
introduced by
The condition is_array($list) is always true.
Loading history...
75 24
            foreach ($list as $tok) {
76 24
                $ret .= $tok->token;
77
            }
78
        }
79
80 24
        return $ret;
81
    }
82
83
    /**
84
     * Adds a new token.
85
     *
86
     * @param Token $token token to be added in list
87
     */
88 4
    public function add(Token $token)
89
    {
90 4
        $this->tokens[$this->count++] = $token;
91 4
    }
92
93
    /**
94
     * Gets the next token. Skips any irrelevant token (whitespaces and
95
     * comments).
96
     *
97
     * @return Token|null
98
     */
99 348
    public function getNext()
100
    {
101 348
        for (; $this->idx < $this->count; ++$this->idx) {
102
            if (
103 348
                ($this->tokens[$this->idx]->type !== Token::TYPE_WHITESPACE)
104 348
                && ($this->tokens[$this->idx]->type !== Token::TYPE_COMMENT)
105
            ) {
106 348
                return $this->tokens[$this->idx++];
107
            }
108
        }
109
110 4
        return null;
111
    }
112
113
    /**
114
     * Gets the next token.
115
     *
116
     * @param int $type the type
117
     *
118
     * @return Token|null
119
     */
120 184
    public function getNextOfType($type)
121
    {
122 184
        for (; $this->idx < $this->count; ++$this->idx) {
123 184
            if ($this->tokens[$this->idx]->type === $type) {
124 180
                return $this->tokens[$this->idx++];
125
            }
126
        }
127
128 12
        return null;
129
    }
130
131
    /**
132
     * Gets the next token.
133
     *
134
     * @param int    $type  the type of the token
135
     * @param string $value the value of the token
136
     *
137
     * @return Token|null
138
     */
139 1984
    public function getNextOfTypeAndValue($type, $value)
140
    {
141 1984
        for (; $this->idx < $this->count; ++$this->idx) {
142 1984
            if (($this->tokens[$this->idx]->type === $type) && ($this->tokens[$this->idx]->value === $value)) {
143 360
                return $this->tokens[$this->idx++];
144
            }
145
        }
146
147 1984
        return null;
148
    }
149
150
    /**
151
     * Sets an value inside the container.
152
     *
153
     * @param int   $offset the offset to be set
154
     * @param Token $value  the token to be saved
155
     */
156 4
    public function offsetSet($offset, $value)
157
    {
158 4
        if ($offset === null) {
0 ignored issues
show
introduced by
The condition $offset === null is always false.
Loading history...
159 4
            $this->tokens[$this->count++] = $value;
160
        } else {
161 4
            $this->tokens[$offset] = $value;
162
        }
163 4
    }
164
165
    /**
166
     * Gets a value from the container.
167
     *
168
     * @param int $offset the offset to be returned
169
     *
170
     * @return Token
171
     */
172 28
    public function offsetGet($offset)
173
    {
174 28
        return $offset < $this->count ? $this->tokens[$offset] : null;
175
    }
176
177
    /**
178
     * Checks if an offset was previously set.
179
     *
180
     * @param int $offset the offset to be checked
181
     *
182
     * @return bool
183
     */
184 4
    public function offsetExists($offset)
185
    {
186 4
        return $offset < $this->count;
187
    }
188
189
    /**
190
     * Unsets the value of an offset.
191
     *
192
     * @param int $offset the offset to be unset
193
     */
194 4
    public function offsetUnset($offset)
195
    {
196 4
        unset($this->tokens[$offset]);
197 4
        --$this->count;
198 4
        for ($i = $offset; $i < $this->count; ++$i) {
199 4
            $this->tokens[$i] = $this->tokens[$i + 1];
200
        }
201
202 4
        unset($this->tokens[$this->count]);
203 4
    }
204
}
205