PHP_CodeSniffer
PHP_CodeSniffer copied to clipboard
Generic.WhiteSpace.ScopeIndent false positive with nested `match`-es
Describe the bug
The ScopeIndent sniff suggests a quite strange indentation when processing multi-nested match
-statements
// It's a copy of my bug report from the old repo (squizlabs/PHP_CodeSniffer#3875) // Thank you, Juliette, for an amazing initiative in keeping the project going!
Code sample
<?php
echo match (1) {
0 => match (2) {
2 => match (3) {
3 => 3,
default => -1,
},
},
1 => match (2) {
1 => match (3) {
3 => 3,
default => -1,
},
2 => match (3) {
3 => 3,
default => -1,
},
},
};
How that code will look if formatted as suggested
<?php
echo match (1) {
0 => match (2) {
2 => match (3) {
3 => 3,
default => -1,
},
},
1 => match (2) {
1 => match (3) {
3 => 3,
default => -1,
},
2 => match (3) {
3 => 3,
default => -1,
},
},
};
Custom ruleset
N/A — reproducible with PSR12
To reproduce
Steps to reproduce the behavior:
- Create a file called
test.php
with the code sample above - Run
phpcs -s --standard=psr12 test.php
- See errors displayed
------------------------------------------------------------------------------------------------------------------------------
FOUND 4 ERRORS AFFECTING 4 LINES
------------------------------------------------------------------------------------------------------------------------------
15 | ERROR | [x] Line indented incorrectly; expected at least 12 spaces, found 8
| | (Generic.WhiteSpace.ScopeIndent.Incorrect)
16 | ERROR | [x] Line indented incorrectly; expected at least 16 spaces, found 12
| | (Generic.WhiteSpace.ScopeIndent.Incorrect)
17 | ERROR | [x] Line indented incorrectly; expected at least 16 spaces, found 12
| | (Generic.WhiteSpace.ScopeIndent.Incorrect)
18 | ERROR | [x] Line indented incorrectly; expected 12 spaces, found 8 (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------------------------------
Expected behavior
No indentation errors.
Versions (please complete the following information)
Operating System | macOS 12.5 |
PHP version | 8.0 |
PHP_CodeSniffer version | 3.9.0 |
Standard | PSR2 |
Install type | Local composer |
Additional context
none
Please confirm:
- [x] I have searched the issue list and am not opening a duplicate issue.
- [x] I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
- [x] I have verified the issue still exists in the
master
branch of PHP_CodeSniffer.
The issues is still present in v3.9.0.
@andrey-yantsen You are welcome to submit a patch...
@jrfnl I'm in no way rushing it, and I want to do a patch at some later date. Sorry if it sounded like an attack, that wasn't my intention :)
I just saw that there were some changes around the processing of match
-es in the last releases, and I wanted to save some time on retesting to anybody stumbling upon this ticket. I probably should've just updated the PHP_CodeSniffer version
in the initial message 😅 I'll do better next time.
Pff.. this was complex one. PR #502 should fix it though. Testing appreciated.
Thank you, @jrfnl!
I just checked the master branch — the issue is gone for all the examples I had at hand.
Thanks @andrey-yantsen for testing and confirming!