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
gsherwood
merged 4 commits into
squizlabs:master
from
jrfnl:feature/psr2-switch-declaration-various-fixes
May 27, 2021
Merged
PSR2/SwitchDeclaration: various bug fixes #3354
gsherwood
merged 4 commits into
squizlabs:master
from
jrfnl:feature/psr2-switch-declaration-various-fixes
May 27, 2021
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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
changed the title
PSR2/SwitchDeclaration: bug fix - don't remove trailing comments
PSR2/SwitchDeclaration: various bug fixes
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. |
Thanks a lot for all these fixes. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
case
will be ignored.T_COMMENT
tokens.case
and non-empty tokens on the same line as thecase
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 theTerminatingComment
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 acase
needs a terminating comment.As per #3297 (comment):
Includes plenty of unit tests.
Fixes #3297