Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is very much of value as test pass locally but not on gitlab, knowing the PHP version including patch

Copy link
Member Author

@keradus keradus Dec 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should add php -v before each PHP command. and if so, putting it in one place is enough.
And we have it https://github.com/FriendsOfPHP/PHP-CS-Fixer/runs/4521649824?check_suite_focus=true#step:4:15

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not for each PHP command is it? only for unique phpunit runs.
If you see no value in having the build details than sure remove it.

It will likely be part of each draft PR in the future that want to do fixes for upcoming PHP version so the author knows the build details of nightly builds etc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SpacePossum , all commands within single build are using the same PHP installation. so yes, you can scroll to top of log of given build to see exact PHP version

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand;
https://github.com/FriendsOfPHP/PHP-CS-Fixer/runs/4516979071?check_suite_focus=true
shows the PHP version PHPUnit is going to use one time,
than here;
https://github.com/FriendsOfPHP/PHP-CS-Fixer/runs/4516979363?check_suite_focus=true
shows the PHP version PHPUnit is going to use one time,
how is this before each PHP command ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

==> Setup PHP
✓ PHP Found PHP 8.1.0

vs.

PHP 8.1.0 (cli) (built: Nov 28 2021 04:13:56) (NTS)

it is not the same, the second shows more, which is especially handy when running on nightly build.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

each job starts with Setup PHP, then each command within this job is using it.

For example you showed, I don't see anything different. For nightly build, I would love to see example.

If adding php -v at all, I believe we should not put it inside PHPUnit step, but make a dedicated step like PHP info after Setup PHP step.

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));
SpacePossum marked this conversation as resolved.
Show resolved Hide resolved
// 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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

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