PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

Generic.WhiteSpace.ScopeIndent false positive with nested `match`-es

Open andrey-yantsen opened this issue 1 year ago • 3 comments

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:

  1. Create a file called test.php with the code sample above
  2. Run phpcs -s --standard=psr12 test.php
  3. 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.

andrey-yantsen avatar Dec 01 '23 17:12 andrey-yantsen

The issues is still present in v3.9.0.

andrey-yantsen avatar Feb 16 '24 15:02 andrey-yantsen

@andrey-yantsen You are welcome to submit a patch...

jrfnl avatar Feb 16 '24 16:02 jrfnl

@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.

andrey-yantsen avatar Feb 16 '24 16:02 andrey-yantsen

Pff.. this was complex one. PR #502 should fix it though. Testing appreciated.

jrfnl avatar May 19 '24 21:05 jrfnl

Thank you, @jrfnl!

I just checked the master branch — the issue is gone for all the examples I had at hand.

andrey-yantsen avatar May 22 '24 08:05 andrey-yantsen

Thanks @andrey-yantsen for testing and confirming!

jrfnl avatar May 22 '24 10:05 jrfnl