SwitchDeclaration
Ensures all switch statements are defined correctly.
Scope
PSR2.ControlStructures.SwitchDeclaration
Description
Case statements should be indented 4 spaces from the switch keyword. It should also be followed by a space. Colons in switch declarations should not be preceded by whitespace. Break statements should be indented 4 more spaces from the case statement. There must be a comment when falling through from one case into the next.
Properties
Property Name | Type | Default | Available Since |
---|---|---|---|
indent | int | 4 | 1.4.5 |
Usage
<rule ref="PSR2.ControlStructures.SwitchDeclaration">
<properties>
<property name="indent" value="2" />
</properties>
</rule>
Examples
Example 1
Valid: Case statement indented correctly.
switch ($foo) {
case 'bar':
break;
}
Invalid: Case statement not indented 4 spaces.
switch ($foo) {
case 'bar':
break;
}
Example 2
Valid: Case statement followed by 1 space.
switch ($foo) {
case 'bar':
break;
}
Invalid: Case statement not followed by 1 space.
switch ($foo) {
case'bar':
break;
}
Example 3
Valid: Colons not prefixed by whitespace.
switch ($foo) {
case 'bar':
break;
default:
break;
}
Invalid: Colons prefixed by whitespace.
switch ($foo) {
case 'bar' :
break;
default :
break;
}
Example 4
Valid: Break statement indented correctly.
switch ($foo) {
case 'bar':
break;
}
Invalid: Break statement not indented 4 spaces.
switch ($foo) {
case 'bar':
break;
}
Example 5
Valid: Comment marking intentional fall-through.
switch ($foo) {
case 'bar':
// no break
default:
break;
}
Invalid: No comment marking intentional fall-through.
switch ($foo) {
case 'bar':
default:
break;
}