6
6
7
7
use ArrayAccess ;
8
8
9
+ use function array_splice ;
9
10
use function count ;
10
11
use function in_array ;
11
12
use function is_array ;
@@ -213,25 +214,29 @@ public function getNextOfTypeAndFlag(int $type, int $flag): ?Token
213
214
}
214
215
215
216
/**
216
- * Sets an value inside the container.
217
+ * Sets a Token inside the list of tokens.
218
+ * When defined, offset must be positive otherwise the offset is ignored.
219
+ * If the offset is not defined (like in array_push) or if it is greater than the number of Tokens already stored,
220
+ * the Token is appended to the list of tokens.
217
221
*
218
- * @param int|null $offset the offset to be set
222
+ * @param int|null $offset the offset to be set. Must be positive otherwise, nothing will be stored.
219
223
* @param Token $value the token to be saved
220
224
*
221
225
* @return void
222
226
*/
223
227
#[\ReturnTypeWillChange]
224
228
public function offsetSet ($ offset , $ value )
225
229
{
226
- if ($ offset === null ) {
230
+ if ($ offset === null || $ offset >= $ this -> count ) {
227
231
$ this ->tokens [$ this ->count ++] = $ value ;
228
- } else {
232
+ } elseif ( $ offset >= 0 ) {
229
233
$ this ->tokens [$ offset ] = $ value ;
230
234
}
231
235
}
232
236
233
237
/**
234
- * Gets a value from the container.
238
+ * Gets a Token from the list of tokens.
239
+ * If the offset is negative or above the number of tokens set in the list, will return null.
235
240
*
236
241
* @param int $offset the offset to be returned
237
242
*
@@ -240,11 +245,12 @@ public function offsetSet($offset, $value)
240
245
#[\ReturnTypeWillChange]
241
246
public function offsetGet ($ offset )
242
247
{
243
- return $ offset < $ this ->count ? $ this ->tokens [$ offset ] : null ;
248
+ return $ this ->offsetExists ( $ offset ) ? $ this ->tokens [$ offset ] : null ;
244
249
}
245
250
246
251
/**
247
252
* Checks if an offset was previously set.
253
+ * If the offset is negative or above the number of tokens set in the list, will return false.
248
254
*
249
255
* @param int $offset the offset to be checked
250
256
*
@@ -253,11 +259,11 @@ public function offsetGet($offset)
253
259
#[\ReturnTypeWillChange]
254
260
public function offsetExists ($ offset )
255
261
{
256
- return $ offset < $ this ->count ;
262
+ return $ offset >= 0 && $ offset < $ this ->count ;
257
263
}
258
264
259
265
/**
260
- * Unsets the value of an offset.
266
+ * Unsets the value of an offset, if the offset exists .
261
267
*
262
268
* @param int $offset the offset to be unset
263
269
*
@@ -266,12 +272,11 @@ public function offsetExists($offset)
266
272
#[\ReturnTypeWillChange]
267
273
public function offsetUnset ($ offset )
268
274
{
269
- unset($ this ->tokens [$ offset ]);
270
- --$ this ->count ;
271
- for ($ i = $ offset ; $ i < $ this ->count ; ++$ i ) {
272
- $ this ->tokens [$ i ] = $ this ->tokens [$ i + 1 ];
275
+ if (! $ this ->offsetExists ($ offset )) {
276
+ return ;
273
277
}
274
278
275
- unset($ this ->tokens [$ this ->count ]);
279
+ array_splice ($ this ->tokens , $ offset , 1 );
280
+ --$ this ->count ;
276
281
}
277
282
}
0 commit comments