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

False positive from PSR2.ControlStructures.SwitchDeclaration.TerminatingComment when using trailing comment #3550

Closed
zorac opened this issue Feb 25, 2022 · 1 comment · Fixed by #3551
Milestone

Comments

@zorac
Copy link

zorac commented Feb 25, 2022

Describe the bug
A false positive is reported if a switch case contains an if/else structure where all branches exit the switch, and one of the branches ends with a comment. Minimal example is below; the error is also reported if one or more elseif branches exist, if the comment is on a different branch, if the comment is on the line after the break statement, or if return is used in place of break. No error is reported if the comment is removed, or is on the line before the break.

Code sample

<?php

switch (rand()) {
    case 1:
        if (rand() === 1) {
            break;
        } else {
            break; // comment
        }
    default:
        break;
}

Custom ruleset

<?xml version="1.0"?>
<ruleset name="My Custom Standard">
  <rule ref="PSR2.ControlStructures.SwitchDeclaration"/>
</ruleset>

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
--------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
--------------------------------------------------------------------------------
 4 | ERROR | There must be a comment when fall-through is intentional in a
   |       | non-empty case body
   |       | (PSR2.ControlStructures.SwitchDeclaration.TerminatingComment)
--------------------------------------------------------------------------------

Expected behavior
No error should be reported.

Versions:

  • OS: macOS 12.2.1
  • PHP: 8.3.1
  • PHPCS: 3.6.2
  • Standard: -

Additional context

@jrfnl
Copy link
Contributor

jrfnl commented Feb 25, 2022

@zorac Thanks for reporting this. I've reproduced the issue and pulled a fix in #3551. Testing the fix would be much appreciated.

@gsherwood gsherwood added this to the 3.7.0 milestone Feb 27, 2022
@gsherwood gsherwood changed the title False positive from PSR2.ControlStructures.SwitchDeclaration.TerminatingComment False positive from PSR2.ControlStructures.SwitchDeclaration.TerminatingComment when using trailing comment Mar 18, 2022
gsherwood added a commit that referenced this issue Mar 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants