-
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
PSR12.Operators.OperatorSpacing interprets pipe character in catch declaration as binary operator #3663
Comments
This one is tricky to fix due to PSR12 and PHP itself. The try/catch/finally example in PSR12 is this (https://www.php-fig.org/psr/psr-12/#56-try-catch-finally): <?php
try {
// try body
} catch (FirstThrowableType $e) {
// catch body
} catch (OtherThrowableType | AnotherThrowableType $e) {
// catch body
} finally {
// finally body
} Note the spaces around the pipe character. This obviously pre-dates PHP8 union types, so the pipe character here has been treated as a bitwise OR in PHPCS because a union types concept didn't exist. PSR12 wants spaces around it, so PHPCS is functioning correctly as per PSR12. With union types making it into PHP8, it would probably be more accurate to re-tokenize the pipe character in a catch to So where I've landed on this is
Keen to hear people's thoughts on that. |
That's my bad, I guess. We encountered this issue after updating FriendsOfPHP/PHP-CS-Fixer, which removed the spaces, but I haven't double checked the standard. As far as I'm concerned this issue can be closed. |
Hi! What's the workaround for this, having both php_cs and php-cs-fixer on the same projet? Ignore php_cs locally ? |
First thing to ask if you are using both: why are you using both ? |
because they have complementary rulesets and especially complementary fixers. But I think it's beside the point here. The rules here are not compliant with the standard, that's the only thing to ask here: how can we make this work? I agree with @gsherwood 's statement: if this is a BC break it should wait for v4. In the meantime I'm ignoring phpcs for the conflicting lines. |
I believe this needs clarification from FIG / PSR PER about what the spacing rules are for the |
I've opened an issue with PER to ask for clarification: php-fig/per-coding-style#67 |
Describe the bug
PSR12.Operators.OperatorSpacing produces false positives on type alternatives in catch clause.
Code sample
To reproduce
Steps to reproduce the behavior:
test.php
with the code sample above...phpcs -s --standard=PSR12 test.php
Expected behavior
No error, since the pipe character in the code is not an operator. This already works as expected for function declarations, i.e. this is fine:
Versions (please complete the following information):
The text was updated successfully, but these errors were encountered: