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

PHP 8.1 | Tokenizer/PHP: bug fix for overeager explicit octal notation backfill #3552

Merged

Conversation

jrfnl
Copy link
Contributor

@jrfnl jrfnl commented Feb 28, 2022

Follow up on #3481. /cc @MarkBaker

Just like for all other type of integer notations, if a numeric literal separator is used, it is not allowed between the prefix and the actual number.

// This is fine.
$b = 0b1_0;
$o = 0o6_3;

// This is an invalid use of the numeric literal separator.
$b = 0b_10;
$o = 0o_63;

This PR fixes the backfill for explicit octal notation to NOT backfill these type of invalid sequences as the inconsistent tokenization across PHP versions which that causes, can create havoc in sniffs.

Includes adding additional unit tests.

@gsherwood Greg - can this PR please be earmarked for PHPCS 3.7.0 to prevent the incorrect tokenization getting into a released version ?

…n backfill

Follow up on 3481.

Just like for all other type of integer notations, if a numeric literal separator is used, it is not allowed between the prefix and the actual number.
```php
// This is fine.
$b = 0b1_0;
$o = 0o6_3;

// This is an invalid use of the numeric literal separator.
$b = 0b_10;
$o = 0o_63;
```

This PR fixes the backfill for explicit octal notation to NOT backfill these type of invalid sequences as the inconsistent tokenization across PHP versions which that causes, can create havoc in sniffs.

Includes adding additional unit tests.
@jrfnl jrfnl force-pushed the feature/tokenizer-php-octal-notation-bugfix branch from e0d4295 to 72a66aa Compare February 28, 2022 04:13
jrfnl added a commit to PHPCSStandards/PHPCSUtils that referenced this pull request Feb 28, 2022
This add support for the explicit octal notation using a `0o`/`0O` prefix as supported in PHP as of PHP 8.1.

The only method which needed adjusting was the `Numbers::getCompleteNumber()` method, which now backfills the tokenization when needed.

For the `Numbers::isOctalInt()` method, a small tweak to the regex was all that was needed.

As for the `Numbers::getDecimalValue()` method: this already handled the conversion correctly due to the use of the PHP native `octdec()` function.

From the RFC:
> Surprisingly PHP already has support for this notation when using the `octdec()` and `base_convert()` functions.

Includes adding unit tests to safeguard support in all relevant methods in the class.

Refs:
* https://wiki.php.net/rfc/explicit_octal_notation
* php/php-src#6360
* squizlabs/PHP_CodeSniffer#3481
* squizlabs/PHP_CodeSniffer#3552
@gsherwood gsherwood added this to the 3.7.0 milestone Mar 10, 2022
gsherwood added a commit that referenced this pull request Mar 18, 2022
@gsherwood gsherwood merged commit 11810de into squizlabs:master Mar 18, 2022
@gsherwood
Copy link
Member

Thanks a lot for finding and fixing this before release.

@jrfnl jrfnl deleted the feature/tokenizer-php-octal-notation-bugfix branch March 18, 2022 09:08
@jrfnl
Copy link
Contributor Author

jrfnl commented Mar 18, 2022

Thanks for merging this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants