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

Sniff to check whitespace around namespace separators #2150

Closed
jrfnl opened this issue Sep 3, 2018 · 3 comments
Closed

Sniff to check whitespace around namespace separators #2150

jrfnl opened this issue Sep 3, 2018 · 3 comments

Comments

@jrfnl
Copy link
Contributor

jrfnl commented Sep 3, 2018

While working on something else, I suddenly realized that there is no sniff to check the whitespace around namespace separator tokens \ and that some other sniffs get terribly confused and throw false positives/incorrect error messages when they encounter whitespace and/or comments within a namespace name.

namespace MyNS \  WhiteSpace\OK\ NotOK;

class ABC extends \ MyNamespace \ /* comment */ Something \    Other \
    Deeper \ ClassName
{}

I realize it is rare to encounter such code, but it is perfectly valid PHP code and PHP will just ignore the whitespace and comments and function as expected. Also see: https://3v4l.org/EFgCp

While not explicit in the textual guidelines, using whitespace and comments within a namespace name does appear to be forbidden in PSR-2 based on all the example code.

The sniffs which currently throw false positives when they encounter such code - based on a cursory analysis - are PSR2.Classes.ClassDeclaration and Squiz.Classes.ClassDeclaration.

@gsherwood Do you think its a good idea to add a new sniff for this ? I'm thinking Generic.WhiteSpace.NamespaceSeparatorSpacing with a public $ignoreNewlines property which defaults to false.
And if so, should it be added to any of the existing rulesets ?

I'd be happy to create the sniff and fix the other two I mentioned above (I actually already started to do so), but figured this warranted an issue to discuss before I opened a PR.

@gsherwood
Copy link
Member

A similar issue was fixed in PSR2.Namespaces.NamespaceDeclaration as part of bug #2095

A sniff sounds like a good idea. Fixes would be great.

@jrfnl
Copy link
Contributor Author

jrfnl commented Sep 6, 2018

That might well have been the issue which inspired me to look into the handling of this. Expect some PRs soon.

@jrfnl
Copy link
Contributor Author

jrfnl commented Sep 21, 2020

I'm going to close this issue.

I did work on this at the time, but it was more complicated than I anticipated.

All the same, as whitespace and comments within namespaced names will now become a parse error in PHP 8.0, a sniff for this was needed for the PHPCompatibility PHPCS standard anyway and with the benefit of two more years of sniff writing, I have finished the sniff and will pull it to PHPCompatibility in the near future.

It will be available as part of the PHPCompatibility 10.0.0 release.

@jrfnl jrfnl closed this as completed Sep 21, 2020
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

No branches or pull requests

2 participants