You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
The SwitchDeclaration sniff fails to determine the correct indent for the break statement when the case is on the same level as switch
Code sample
switch ( $tokens[$next]['code'] ) {
caseT_OPEN_CURLY_BRACKET:
caseT_OPEN_SQUARE_BRACKET:
caseT_OPEN_PARENTHESIS:
if ( isset( $tokens[$next]['parenthesis_closer'] ) ) {
// jump to closing parenthesis to ignore commas between opener and closer$next = $tokens[$next]['parenthesis_closer'];
}
break; # <!--- ERROR | [x] Terminating statement must be indented to the same level as the CASE bodycaseT_COMMA:
$argCount++;
break; # <!--- ERROR | [x] Terminating statement must be indented to the same level as the CASE body
}
Expected behavior
The autofix adds a indent of 4 spaces to the break. Expected is no error reporting and no autofix or the cases are also reported and autofixed.
Versions (please complete the following information):
PHPCS: 3.5.8
The text was updated successfully, but these errors were encountered:
The sniff is making an assumption that the CASE keywords are indented 4 spaces from the SWITCH keyword. So it is then enforcing the rule that the breaking statements must be indented 8 spaces from the SWITCH keyword. Obviously, the error message is confusing here due to this assumption.
When taken as a whole, the PSR2 standard does reformat this code correctly because the ScopeIndent sniff is the one that ensures CASE statements are indented 4 spaces from the SWITCH. When taken alone, this sniff will not enforce that part of the standard.
So there are 2 ways forward here:
Have the sniff enforce that CASE statements must be indented 4 spaces from the SWITCH keyword, which will make the sniff more valuable by itself, and fix the error message. This does mean a double-up of checking CASE indents though as the ScopeIndent sniff is going to do it regardless.
Hide the error message when it is confusing and let ScopeIndent report an error for the CASE statement.
I'm learning towards option 2 at the moment, but will look into it more.
gsherwood
changed the title
PSR2.ControlStructures.SwitchDeclaration.BreakIndent fails when case: is not indented
PSR2.ControlStructures.SwitchDeclaration.BreakIndent false positive when case keyword is not indented
Nov 3, 2020
Describe the bug
The
SwitchDeclaration
sniff fails to determine the correct indent for thebreak
statement when thecase
is on the same level asswitch
Code sample
Expected behavior
The autofix adds a indent of 4 spaces to the break. Expected is no error reporting and no autofix or the cases are also reported and autofixed.
Versions (please complete the following information):
The text was updated successfully, but these errors were encountered: