Skip to content

Commit

Permalink
minor #6172 DX: Tokens::insertSlices - groom code and fix tests (kera…
Browse files Browse the repository at this point in the history
…dus)

This PR was squashed before being merged into the master branch (closes #6172).

Discussion
----------

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

follows up #6171

Commits
-------

8b0df85 DX: Tokens::insertSlices - groom code and fix tests
  • Loading branch information
SpacePossum committed Dec 15, 2021
2 parents 00096f0 + 8b0df85 commit d81884c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/ci.yml
Expand Up @@ -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'
Expand Down
15 changes: 7 additions & 8 deletions src/Tokenizer/Tokens.php
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down
9 changes: 4 additions & 5 deletions tests/Tokenizer/TokensTest.php
Expand Up @@ -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
Expand All @@ -1397,11 +1396,11 @@ public function provideInsertSlicesAtMultiplePlacesCases(): \Generator
<<<'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' => [
Expand Down

0 comments on commit d81884c

Please sign in to comment.