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

PSR2/SwitchDeclaration: various bug fixes #3354

Merged

Commits on May 13, 2021

  1. PSR2/SwitchDeclaration: bug fix - don't remove trailing comments

    As reported in 3352, the `PSR2.ControlStructures.SwitchDeclaration.BodyOnNextLineCASE` fixer could inadvertently remove trailing comments.
    
    This commit improves on the fix which was put in place in response to 683, by making the detection of what should be considered the start of the body more precise:
    * Any amount of trailing comments on the same line as the `case` will be ignored.
    * Any type of comment will be taken into consideration, not just PHPCS annotations and `T_COMMENT` tokens.
    * Comments _not_ on the same line as the `case` and non-empty tokens on the same line as the `case` will both be considered as the "start of the body".
    
    As for the fixer. The "body starts on same line" fixer was already okay. The "empty lines between case and body" fixer, however, will now ignore anything on the same line as the `case`, which prevents us having to account for comments which already include a new line.
    
    Includes unit tests.
    
    Fixes 3352
    jrfnl committed May 13, 2021
    Configuration menu
    Copy the full SHA
    478d4e5 View commit details
    Browse the repository at this point in the history
  2. PSR2/SwitchDeclaration: bug fix - improve handling of comments

    Comments in certain places in the code flow would throw the `findNestedTerminator()` method used for determining the `TerminatingComment` error off.
    
    Fixed now.
    
    Includes unit tests.
    Without this fix, the first test would not throw an error (false negative), while the second would (false positive).
    jrfnl committed May 13, 2021
    Configuration menu
    Copy the full SHA
    ec015a8 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    b31b9b2 View commit details
    Browse the repository at this point in the history
  4. PSR2/SwitchDeclaration: bug fix - handle try/catch/finally correctly

    Add code to handle `try-catch-finally` statements correctly when determining whether a `case` needs a terminating comment.
    
    As per squizlabs#3297 (comment):
    
    > * If there is a `finally` statement and the body of that contains a "terminating statement" (`return`, `break` etc), we don't need to check the body of the `try` or any of the `catch` statements as, no matter what, all execution branches are then covered by that one terminating statement.
    > * If there is **_no_** `finally` or if there is, but it does not contain a terminating statement, then all `try` and `catch` structures should each contain a terminating statement in the body.
    
    Includes plenty of unit tests.
    
    Fixes 3297
    jrfnl committed May 13, 2021
    Configuration menu
    Copy the full SHA
    5391139 View commit details
    Browse the repository at this point in the history