From 8b0df851060a990198ec3e2127824b8c1646b23e Mon Sep 17 00:00:00 2001 From: Dariusz Ruminski Date: Tue, 14 Dec 2021 11:22:31 +0000 Subject: [PATCH] DX: Tokens::insertSlices - groom code and fix tests --- .github/workflows/ci.yml | 4 +--- src/Tokenizer/Tokens.php | 15 +++++++-------- tests/Tokenizer/TokensTest.php | 9 ++++----- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 84faf63f0cc..ac4642ad666 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -124,9 +124,7 @@ jobs: env: PHP_CS_FIXER_IGNORE_ENV: ${{ matrix.PHP_CS_FIXER_IGNORE_ENV }} FAST_LINT_TEST_CASES: ${{ matrix.FAST_LINT_TEST_CASES }} - run: | - php -v - vendor/bin/phpunit ${{ matrix.phpunit-flags }} + run: vendor/bin/phpunit ${{ matrix.phpunit-flags }} - name: Upload coverage results to Coveralls if: matrix.calculate-code-coverage == 'yes' diff --git a/src/Tokenizer/Tokens.php b/src/Tokenizer/Tokens.php index 56ccb8348bd..15044fef033 100644 --- a/src/Tokenizer/Tokens.php +++ b/src/Tokenizer/Tokens.php @@ -858,14 +858,14 @@ public function insertSlices(array $slices): void $this->setSize($oldSize + $itemsCount); krsort($slices); + $farthestSliceIndex = key($slices); - $firstIndex = key($slices); - - if (!\is_int($firstIndex) || $firstIndex > $oldSize) { - throw new \OutOfBoundsException(sprintf('Invalid index "%s".', $firstIndex)); + // We check only the farthest index, if it's within the size of collection, other indices will be valid too. + if (!\is_int($farthestSliceIndex) || $farthestSliceIndex > $oldSize) { + throw new \OutOfBoundsException(sprintf('Cannot insert index "%s" outside of collection.', $farthestSliceIndex)); } - $insertBound = $oldSize - 1; + $previousSliceIndex = $oldSize; // since we only move already existing items around, we directly call into SplFixedArray::offset* methods. // that way we get around additional overhead this class adds with overridden offset* methods. @@ -877,12 +877,11 @@ public function insertSlices(array $slices): void $slice = \is_array($slice) || $slice instanceof self ? $slice : [$slice]; $sliceCount = \count($slice); - for ($i = $insertBound; $i >= $index; --$i) { + for ($i = $previousSliceIndex - 1; $i >= $index; --$i) { parent::offsetSet($i + $itemsCount, parent::offsetGet($i)); } - // adjust $insertBound as tokens between this index and the next index in loop - $insertBound = $index - 1; + $previousSliceIndex = $index; $itemsCount -= $sliceCount; foreach ($slice as $indexItem => $item) { diff --git a/tests/Tokenizer/TokensTest.php b/tests/Tokenizer/TokensTest.php index 38c1242488b..4d5988c9f2a 100644 --- a/tests/Tokenizer/TokensTest.php +++ b/tests/Tokenizer/TokensTest.php @@ -1387,8 +1387,7 @@ public function testInsertSlicesAtMultiplePlaces(string $expected, array $slices 16 => $slices, 6 => $slices, ]); - - static::assertSame($expected, $tokens->generateCode()); + static::assertTokens(Tokens::fromCode($expected), $tokens); } public function provideInsertSlicesAtMultiplePlacesCases(): \Generator @@ -1397,11 +1396,11 @@ public function provideInsertSlicesAtMultiplePlacesCases(): \Generator <<<'EOF' [