-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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.0 | Proposal for handling "namespaced names as single token" #3041
Comments
@jrfnl I've kinda dropped out of development activities for the time being. I'm not sure I have much to contribute at the moment. I think the proposal looks good. |
In PHP 8.0 identifier name tokenization will change as outline in the [accepted RFC "Treat namespaced names as single token"(https://wiki.php.net/rfc/namespaced_names_as_token). When the PHP 8.0 identifier name tokenization is used, the target token to find for some tests will need to be a different token - for instance: `T_STRING` vs `T_FULLY_QUALIFIED_NAME` -. Along the same lines, the expected token positions in the return value of various functions will also be different when the PHP < 8.0 tokenization is used as certain tokens will be "squashed" into one token. This adds a test helper method to allow tests to "know" whether or not to expect the PHP 8.0 identifier name tokenization, so the test setup/expectations can be adjusted based on the expected tokenization. The method is based on the _current reality_. At this time the PHP 8 tokenization should be expected on all PHPCS versions when run on PHP 8. Refs: * https://wiki.php.net/rfc/namespaced_names_as_token * [Proposal for handling this PHP 8 change in PHPCS](squizlabs/PHP_CodeSniffer#3041) * [Open PR for the PHPCS 3.x branch to "undo" the PHP 8 tokenization](squizlabs/PHP_CodeSniffer#3063)
In PHP 8.0 identifier name tokenization will change as outline in the [accepted RFC "Treat namespaced names as single token"](https://wiki.php.net/rfc/namespaced_names_as_token). When the PHP 8.0 identifier name tokenization is used, the target token to find for some tests will need to be a different token - for instance: `T_STRING` vs `T_FULLY_QUALIFIED_NAME` -. Along the same lines, the expected token positions in the return value of various functions will also be different when the PHP < 8.0 tokenization is used as certain tokens will be "squashed" into one token. This adds a test helper method to allow tests to "know" whether or not to expect the PHP 8.0 identifier name tokenization, so the test setup/expectations can be adjusted based on the expected tokenization. The method is based on the _current reality_. At this time the PHP 8 tokenization should be expected on all PHPCS versions when run on PHP 8. Refs: * https://wiki.php.net/rfc/namespaced_names_as_token * [Proposal for handling this PHP 8 change in PHPCS](squizlabs/PHP_CodeSniffer#3041) * [Open PR for the PHPCS 3.x branch to "undo" the PHP 8 tokenization](squizlabs/PHP_CodeSniffer#3063)
@gsherwood Should this ticket be re-opened for the PHPCS 4.x part ? Only the 3.x part has been fixed so far. Once the union types PR (#3032) is merged in PHPCS 3.6.0 and cherrypicked to 4.x, I will pull the PR for this change to the 4.x branch. (would conflict otherwise) |
I'll move it to the 4.0 project. |
After some mucking around, I would really need this to be 2 tickets to organise things properly. So have just moved it to 4.0 instead of keeping it in both. |
Upstream PR 3063 which was merged in PHPCS 3.5.7 effectively "undoes" the PHP 8.0 tokenization for identifier names for PHPCS 3.x, while it also includes backfilling the PHP 8.0 token constants. In PHPCS 4.x the PHP 8.0 tokenization will be backfilled instead. This commit annotates this change in the `Collections::nameTokens()` method and updates the unit tests for various token collections to handle this correctly as well. Refs: * squizlabs/PHP_CodeSniffer#3041 * squizlabs/PHP_CodeSniffer#3063
Upstream PR 3063 which was merged in PHPCS 3.5.7 effectively "undoes" the PHP 8.0 tokenization for identifier names for PHPCS 3.x, while it also includes backfilling the PHP 8.0 token constants. In PHPCS 4.x the PHP 8.0 tokenization will be backfilled instead, but that PR has not yet been pulled. This commit annotates the current change in the `Collections::nameTokens()` method and updates the unit tests for various token collections to handle this correctly as well. Refs: * squizlabs/PHP_CodeSniffer#3041 * squizlabs/PHP_CodeSniffer#3063
PR #3155 has now been pulled to address the changes as proposed for PHPCS 4.x. |
PR has been merged but leaving In Progress until changelog is written |
Forgot to add the changelog entry for this, but have done so now. |
What changes in PHP 8
From the RFC:
Ref: https://wiki.php.net/rfc/namespaced_names_as_token
TL;DR
So, in short, PHP 8.0 introduces 3 new tokens for identifiers, on top of the existing
T_STRING
:T_NAME_FULLY_QUALIFIED
T_NAME_RELATIVE
T_NAME_QUALIFIED
Perspective on this as a sniff author
In the long run, I think these new tokens will make writing sniffs analyzing function calls, class instantiation and the likes much easier, especially when namespace/use statement context needs to be taken into account when matching identifiers.
However, this is also a big breaking change which would require a significant change in how any such sniff is analyzing code.
A significant part of the current test failures against the
nightly
build for PHPCS itself can be traced back to this particular change. Same for several external standards which I examined.Proposal for how to handle this change in PHPCS
I'd like to propose the following and would like to invite other sniff authors to give their opinion on this as well:
PHPCS 3.x
For PHPCS 3.x, basically "undo" the change, disregarding any (no longer reserved) keywords.
In practice, this would mean splitting the above tokens on the namespace separator
\
and tokenizing the name parts asT_STRING
(aside from a potentialnamespace
keyword which would need to be tokenized asT_NAMESPACE
).PHPCS 4.x
For PHPCS 4.x, backfill the PHP 8.0 tokenization for the supported PHP versions < 8.0.
Reasoning
Opinions ?
/cc @gsherwood @photodude @umherirrender @TomasVotruba @sirbrillig @michalbundyra @djoos @louisl @kukulich @carusogabriel @klausi @dereuromark @michalbundyra @jmarcil @weierophinney (and please feel free to ping anyone I missed)
The text was updated successfully, but these errors were encountered: