From the RFC:
PHP distinguishes four kinds of namespaced names:
- Unqualified names like Foo, which coincide with identifiers.
- Qualified names like FooBar.
- Fully qualified names like Foo.
- Namespace-relative names like namespaceFoo.
Each of these kinds will be represented by a distinct token:
Foo; // Before: T_STRING // After: T_STRING // Rule: {LABEL} Foo\Bar; // Before: T_STRING T_NS_SEPARATOR T_STRING // After: T_NAME_QUALIFIED // Rule: {LABEL}("\\"{LABEL})+ \Foo; // Before: T_NS_SEPARATOR T_STRING // After: T_NAME_FULLY_QUALIFIED // Rule: ("\\"{LABEL})+ namespace\Foo; // Before: T_NAMESPACE T_NS_SEPARATOR T_STRING // After: T_NAME_RELATIVE // Rule: "namespace"("\\"{LABEL})+Individual namespace segments may contain reserved keywords:
// This is interpreted as T_LIST (i.e., as a reserved keyword): List // All of the following are interpreted as legal namespaced names: \List FooBar\List namespace\ListWhitespace (or comments) is not permitted between namespace separators.
Ref: https://wiki.php.net/rfc/namespaced_names_as_token
So, in short, PHP 8.0 introduces 3 new tokens for identifiers, on top of the existing T_STRING:
T_NAME_FULLY_QUALIFIEDT_NAME_RELATIVET_NAME_QUALIFIEDIn 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.
I'd like to propose the following and would like to invite other sniff authors to give their opinion on this as well:
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 as T_STRING (aside from a potential namespace keyword which would need to be tokenized as T_NAMESPACE).
For PHPCS 4.x, backfill the PHP 8.0 tokenization for the supported PHP versions < 8.0.
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)
@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.
PR #3063 addresses this for PHPCS 3.x.
I have also prepared the necessary changes for PHPCS 4.x, but PR #3066 should be reviewed & merged + merged into the PHPCS 4.x branch before I can pull the PHPCS 4.x branch for this change without it causing conflicts/leaving stray tokens behind.
@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.
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.
Most helpful comment
PR has been merged but leaving In Progress until changelog is written