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

Line indented incorrectly false positive when using match-expression inside switch case #3530

Closed
i350 opened this issue Jan 9, 2022 · 1 comment

Comments

@i350
Copy link

i350 commented Jan 9, 2022

Describe the bug
When I uses match-expression inside a switch-case, I get non logical error

Code sample

    switch ($appEnv) {
        case 'production':
            return ($foo == 'bar') ? 'prod' : 'production';
        case 'staging':
            return match ($foo) {
                'bar' => 'custom_1',
                'baz' => 'custom_2',
                default => 'staging'
            };
        case 'testing':
            return 'testing';
        default:
            return 'default';
    }

I get error starting from the line "case 'testing': .....'

Error: Line indented incorrectly; expected 12 spaces, found 8

This require me to justify the "case 'testing':" with its body and "default:" case with its body to closing curly brace of match expression!
Like this:

    switch ($appEnv) {
        case 'production':
            return ($foo == 'bar') ? 'prod' : 'production';
        case 'staging':
            return match ($foo) {
                'bar' => 'custom_1',
                'baz' => 'custom_2',
                default => 'staging'
            };
            case 'testing':
                return 'testing';
            default:
                return 'default';
    }

To reproduce
Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. See error message displayed
PHPCS output here

Expected behavior
A clear and concise description of what you expected to happen.

Versions (please complete the following information):

  • OS: Linux 050918c5cfc1 5.11.0-43-generic ~ 20.04.2-Ubuntu SMP x86_64
  • PHP: 8.0.13
  • PHPCS: 3.6.1 stable
  • Standard: PSR2
@gsherwood gsherwood changed the title Using match-expression inside switch-case cases error: Line indented incorrectly; Line indented incorrectly false positive when using match-expression inside switch case Jan 10, 2022
@gsherwood gsherwood added this to Idea Bank in PHPCS v3 Development via automation Jan 10, 2022
@gsherwood gsherwood added this to the 3.7.0 milestone Jan 10, 2022
gsherwood added a commit that referenced this issue Jan 10, 2022
gsherwood added a commit that referenced this issue Jan 10, 2022
@gsherwood
Copy link
Member

This problem was caused by the match expression being returned instead of assigned to a variable and then returned. The return here is special as it is the closer for the case statement, so everything got confused. There was already code to handle this for anon classes and closures, so I treat match in the same way.

Thanks for reporting this. The fix will be in 3.7.0.

PHPCS v3 Development automation moved this from Idea Bank to Ready for Release Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PHPCS v3 Development
Ready for Release
Development

No branches or pull requests

2 participants