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

Tokenizer/PHP: more context sensitive keyword fixes #3797

Conversation

jrfnl
Copy link
Contributor

@jrfnl jrfnl commented Apr 13, 2023

PHPCS re-tokenizes the self, parent, true, false and null keywords to a PHPCS native token.

This re-tokenization did not take the following situations into account:

  • Those keywords being used as function names when the function is declared to return by reference.
  • Those keywords being used as a function call.

Additionally, the PHP native T_STATIC token would not be (re-)tokenized to T_STRING when used as a function call, though it was tokenized correctly when used as a method call. While using the static keyword for a global function declaration is illegal in PHP, the tokenization in PHPCS should still be consistent.

This commit fixes those issues.

Includes additional unit tests.

These issues were discovered while investigating issue PHPCompatibility/PHPCompatibility#1489

jrfnl added a commit to PHPCompatibility/PHPCompatibility that referenced this pull request Apr 18, 2023
Turns out PHPCS does not tokenize the `parent` keyword correctly as `T_STRING` when used as a function name in a function declaration which returns by reference, nor when that function name is subsequently called.
A fix has been pulled for this upstream: squizlabs/PHP_CodeSniffer#3797

In the mean time, this commit fixes the issue for this sniff in a PHPCS cross-version compatible manner.

Includes unit tests.

Fixes 1489
PHPCS re-tokenizes the `self`, `parent`, `true`, `false` and `null` keywords to a PHPCS native token.

This re-tokenization did not take the following situations into account:
* Those keywords being used as function names when the function is declared to return by reference.
* Those keywords being used as a function call.

Additionally, the PHP native `T_STATIC` token would not be (re-)tokenized to `T_STRING` when used as a function call, though it was tokenized correctly when used as a method call..
While using the `static` keyword for a global function declaration is illegal in PHP, the tokenization in PHPCS should still be consistent.

This commit fixes those issues.

Includes additional unit tests.
@jrfnl jrfnl force-pushed the feature/tokenizer-php-more-context-sensitive-keyword-issues branch from a4d02dd to cb764c7 Compare April 19, 2023 05:25
@jrfnl
Copy link
Contributor Author

jrfnl commented Apr 19, 2023

Note: I've updated the commit to contain yet more unit tests.

@gsherwood gsherwood added this to Idea Bank in PHPCS v3 Development via automation May 4, 2023
@gsherwood gsherwood added this to the 3.8.0 milestone May 4, 2023
gsherwood added a commit that referenced this pull request May 4, 2023
@gsherwood gsherwood merged commit bf40ea2 into squizlabs:master May 4, 2023
26 of 27 checks passed
PHPCS v3 Development automation moved this from Idea Bank to Ready for Release May 4, 2023
@jrfnl jrfnl deleted the feature/tokenizer-php-more-context-sensitive-keyword-issues branch May 4, 2023 06:31
@jrfnl
Copy link
Contributor Author

jrfnl commented Dec 8, 2023

FYI: this fix is included in today's PHP_CodeSniffer 3.8.0 release.

As per #3932, development on PHP_CodeSniffer will continue in the PHPCSStandards/PHP_CodeSniffer repository. If you want to stay informed, you may want to start "watching" that repo (or watching releases from that repo).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PHPCS v3 Development
Ready for Release
Development

Successfully merging this pull request may close these issues.

None yet

2 participants