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

Generic.WhiteSpace.ScopeIndent false positive with nested match-es #110

Open
3 tasks done
andrey-yantsen opened this issue Dec 1, 2023 · 4 comments · May be fixed by #502
Open
3 tasks done

Generic.WhiteSpace.ScopeIndent false positive with nested match-es #110

andrey-yantsen opened this issue Dec 1, 2023 · 4 comments · May be fixed by #502

Comments

@andrey-yantsen
Copy link

andrey-yantsen commented Dec 1, 2023

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:

  • I have searched the issue list and am not opening a duplicate issue.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • I have verified the issue still exists in the master branch of PHP_CodeSniffer.
@andrey-yantsen
Copy link
Author

The issues is still present in v3.9.0.

@jrfnl
Copy link
Member

jrfnl commented Feb 16, 2024

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

@andrey-yantsen
Copy link
Author

andrey-yantsen commented Feb 16, 2024

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

jrfnl added a commit that referenced this issue May 19, 2024
Both the mentioned issues are fixed by the improvements to the `File::findStartOfStatement()` method in this same PR.

Fixes #110
Fixes #437
Fixes squizlabs/PHP_CodeSniffer 3875
@jrfnl jrfnl self-assigned this May 19, 2024
@jrfnl jrfnl added this to the 3.10.x Next milestone May 19, 2024
@jrfnl
Copy link
Member

jrfnl commented May 19, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment