From c2eded3621483ed5c25f9849f3fd80d01f2f7741 Mon Sep 17 00:00:00 2001 From: Dariusz Ruminski Date: Tue, 14 Dec 2021 13:32:07 +0100 Subject: [PATCH] check each slice if it's outside, not only first one --- src/Tokenizer/Tokens.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Tokenizer/Tokens.php b/src/Tokenizer/Tokens.php index 01a68b95dce..df05dc89f19 100644 --- a/src/Tokenizer/Tokens.php +++ b/src/Tokenizer/Tokens.php @@ -858,13 +858,6 @@ public function insertSlices(array $slices): void $this->setSize($oldSize + $itemsCount); krsort($slices); - - $firstIndex = array_key_first($slices); - - if ($firstIndex > $oldSize) { - throw new \OutOfBoundsException('Cannot insert outside of collection.'); - } - $insertBound = $oldSize - 1; // since we only move already existing items around, we directly call into SplFixedArray::offset* methods. @@ -874,6 +867,10 @@ public function insertSlices(array $slices): void throw new \OutOfBoundsException(sprintf('Invalid index "%s".', $index)); } + if ($index > $oldSize) { + throw new \OutOfBoundsException(sprintf('Cannot insert index "%s" outside of collection.', $firstIndex)); + } + $slice = \is_array($slice) || $slice instanceof self ? $slice : [$slice]; $sliceCount = \count($slice);