Browse Source

DX: Tokens::insertSlices - groom code and fix tests

Dariusz Ruminski 3 years ago
parent
commit
8b0df85106
3 changed files with 12 additions and 16 deletions
  1. 1 3
      .github/workflows/ci.yml
  2. 7 8
      src/Tokenizer/Tokens.php
  3. 4 5
      tests/Tokenizer/TokensTest.php

+ 1 - 3
.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'

+ 7 - 8
src/Tokenizer/Tokens.php

@@ -858,14 +858,14 @@ class Tokens extends \SplFixedArray
         $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 @@ class Tokens extends \SplFixedArray
             $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) {

+ 4 - 5
tests/Tokenizer/TokensTest.php

@@ -1387,8 +1387,7 @@ EOF;
             16 => $slices,
             6 => $slices,
         ]);
-
-        static::assertSame($expected, $tokens->generateCode());
+        static::assertTokens(Tokens::fromCode($expected), $tokens);
     }
 
     public function provideInsertSlicesAtMultiplePlacesCases(): \Generator
@@ -1397,11 +1396,11 @@ EOF;
             <<<'EOF'
 <?php
 
-$after =  get_class($after);
-$before =  get_class($before);
+$after = /*foo*/get_class($after);
+$before = /*foo*/get_class($before);
 EOF
             ,
-            [new Token([T_WHITESPACE, ' '])],
+            [new Token([T_COMMENT, '/*foo*/'])],
         ];
 
         yield 'two slice count' => [