Skip to content

Conversation

@MarkBaker
Copy link
Contributor

@MarkBaker MarkBaker commented Nov 20, 2021

Added support for Explicit Octal Notation (introduced in PHP 8.1) in the tokenizer.

When running phpcs against PHP < 8.1, an expression like

$foo = 0o777;

separates the 0o777 into two tokens; a T_LNUMBER with a value "0" and a T_STRING with a value "o777".

These need to be treated as single T_LNUMBER token with a value of "0o777" to be processed correctly by phpcs

MarkBaker added 3 commits November 20, 2021 18:50
…o777") from two separate T_NUMBER and T_STRING tokens into a single T_NUMBER token with the correct value
…ence (e.g."0o777") from two separate T_NUMBER and T_STRING tokens into a single T_NUMBER token with the correct value
…ill works correctly with Explicit octal values when they have to be backfilled
@MarkBaker MarkBaker changed the title PHP 8.1 support for Explicit Octal Notation Support for PHP 8.1 Explicit Octal Notation in the Tokenizer Nov 20, 2021
@kukulich kukulich mentioned this pull request Nov 20, 2021
8 tasks
@gsherwood gsherwood added this to the 3.7.0 milestone Nov 21, 2021
{
$tokens = self::$phpcsFile->getTokens();

$number = $this->getTargetToken($testData['marker'], [T_LNUMBER, T_DNUMBER, T_STRING]);
Copy link
Member

Choose a reason for hiding this comment

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

Are we only looking for T_LNUMBER tokens here or are there test cases to add where T_DNUMBER and T_STRING would be valid to find? Might need some tests to ensure some strings that look octal are not actually being touched ?

<?php

/* testExplicitOctal declaration */
$foo = 0o137041;
Copy link
Contributor

Choose a reason for hiding this comment

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

Again: what about a number with uppercase o ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also wondering if any test could be conceived where the token stream should not be changed (i.e. guard against false positives).

gsherwood added a commit that referenced this pull request Dec 17, 2021
gsherwood added a commit that referenced this pull request Dec 17, 2021
gsherwood added a commit that referenced this pull request Dec 17, 2021
@gsherwood gsherwood merged commit d174d8c into squizlabs:master Dec 17, 2021
@gsherwood
Copy link
Member

Thanks a lot for the contribution and for making those requested changes.

I made a couple of minor changes to the tests but didn't add anything about a case where the token should not be changed (can't think of a case off the top of my head). If anyone has any, please post here and I'll add the test case in.

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
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.

3 participants