-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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.ControlStructures.SwitchDeclaration.TerminatingComment does not handle try/finally blocks #3297
Comments
Sorry, but I think you may be misunderstanding how See this simple demonstration: https://3v4l.org/t2pt6 In other words, the fact that the sniff demands a "fall through" comment is 100% correct and in my opinion, this issue can be closed. |
In my example the try block always returns and in that case there is no fall-through, because the switch execution is ended by the return inside the try block. If the try block does not always returns than the comment is needed, that is correct, but not the issue here. |
@umherirrender Sorry, help me out - the So the only reason the code does not fall through to case 3 is that the |
If the exception (explicit thrown in the try block or implicit within its called function) is uncatched, the switch is also ended. No catch block would throw the exception to the caller of the function with the switch (after the finally block is executed). No catch block does not mean the exception is ignored, that needs an catch block without terminate statement (return or throw) |
@umherirrender Okay, so if we want to address this, what would be rules need to be ?
Does that sound about right ? Am I still overlooking something ? |
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
@umherirrender I've added a fix for this to PR #3354 which was open with another fix for the same sniff. Testing appreciated. |
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
Describe the bug
Using the sniff
PSR2.ControlStructures.SwitchDeclaration
brings in an edge case for the error codeTerminatingComment
when having try/finally in the switch case bodyCode sample
Custom ruleset
To reproduce
Steps to reproduce the behavior:
test.php
with the code sample above...phpcs test.php ...
Expected behavior
The try/finally always returns, that means there is no fall-through which does not need a comment.
That also the same when using finally + catch or catch alone
Versions (please complete the following information):
Additional context
The text was updated successfully, but these errors were encountered: