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

Conversation

jrfnl
Copy link
Contributor

@jrfnl jrfnl commented May 13, 2021

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

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

PSR2/SwitchDeclaration: minor efficiency tweak

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 #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

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 added 3 commits May 13, 2021 09:48
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).
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 jrfnl changed the title PSR2/SwitchDeclaration: bug fix - don't remove trailing comments PSR2/SwitchDeclaration: various bug fixes May 13, 2021
@jrfnl
Copy link
Contributor Author

jrfnl commented May 13, 2021

I've updated this PR and added three more commits.

Pulling these separately would mean that the test files would conflict, so adding the commits to this open PR seemed like the simplest way forward.

I've updated the PR title + description to match the new content.

@gsherwood gsherwood added this to Idea Bank in PHPCS v3 Development via automation May 23, 2021
@gsherwood gsherwood added this to the 3.6.1 milestone May 23, 2021
gsherwood added a commit that referenced this pull request May 27, 2021
@gsherwood gsherwood merged commit e5ceaa1 into squizlabs:master May 27, 2021
PHPCS v3 Development automation moved this from Idea Bank to Ready for Release May 27, 2021
@gsherwood
Copy link
Member

Thanks a lot for all these fixes.

@jrfnl jrfnl deleted the feature/psr2-switch-declaration-various-fixes branch May 27, 2021 11:43
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
2 participants