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);