66
77use ArrayAccess ;
88
9+ use function array_splice ;
910use function count ;
1011use function in_array ;
1112use function is_array ;
@@ -213,25 +214,29 @@ public function getNextOfTypeAndFlag(int $type, int $flag): ?Token
213214 }
214215
215216 /**
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.
217221 *
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.
219223 * @param Token $value the token to be saved
220224 *
221225 * @return void
222226 */
223227 #[\ReturnTypeWillChange]
224228 public function offsetSet ($ offset , $ value )
225229 {
226- if ($ offset === null ) {
230+ if ($ offset === null || $ offset >= $ this -> count ) {
227231 $ this ->tokens [$ this ->count ++] = $ value ;
228- } else {
232+ } elseif ( $ offset >= 0 ) {
229233 $ this ->tokens [$ offset ] = $ value ;
230234 }
231235 }
232236
233237 /**
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.
235240 *
236241 * @param int $offset the offset to be returned
237242 *
@@ -240,11 +245,12 @@ public function offsetSet($offset, $value)
240245 #[\ReturnTypeWillChange]
241246 public function offsetGet ($ offset )
242247 {
243- return $ offset < $ this ->count ? $ this ->tokens [$ offset ] : null ;
248+ return $ this ->offsetExists ( $ offset ) ? $ this ->tokens [$ offset ] : null ;
244249 }
245250
246251 /**
247252 * 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.
248254 *
249255 * @param int $offset the offset to be checked
250256 *
@@ -253,11 +259,11 @@ public function offsetGet($offset)
253259 #[\ReturnTypeWillChange]
254260 public function offsetExists ($ offset )
255261 {
256- return $ offset < $ this ->count ;
262+ return $ offset >= 0 && $ offset < $ this ->count ;
257263 }
258264
259265 /**
260- * Unsets the value of an offset.
266+ * Unsets the value of an offset, if the offset exists .
261267 *
262268 * @param int $offset the offset to be unset
263269 *
@@ -266,12 +272,11 @@ public function offsetExists($offset)
266272 #[\ReturnTypeWillChange]
267273 public function offsetUnset ($ offset )
268274 {
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 ;
273277 }
274278
275- unset($ this ->tokens [$ this ->count ]);
279+ array_splice ($ this ->tokens , $ offset , 1 );
280+ --$ this ->count ;
276281 }
277282}
0 commit comments