CRITICAL
Rule Definition
Using the proper declaration in switch statements helps to read , understand , modify , maintain and enhance the code easily.
Remediation
Sample:
switch ($expr)
{ case 0: echo 'First case, with a break'; break; case 1: echo 'Second case, which falls through'; // no break case 2: case 3: // case is followed more than 1 space , which is against coding standard Case 4: // CASE keyword must be lowercase , which is against coding standard echo 'Third case, return instead of break'; return; case 5: //There must be a comment when fall-through is intentional. case 6: echo '6th case,'; Default: // DEFAULT keyword must be lowercase echo 'Default case'; break; }
Remediation:
switch ($expr)
{ case 0: echo 'First case, with a break'; break; case 1: echo 'Second case, which falls through'; // no break case 2: case 3: // case is followed single space , correct coding standard case 4: // CASE keyword is in lowercase , correct coding standard echo 'Third case, return instead of break'; return; case 5: echo 'Fifth case, which falls through'; // Fall through comments are added. case 6: echo '6th case,'; default: // DEFAULT keyword in lowercase, correct coding standard echo 'Default case'; break; }
Violation Code Sample
<?php
switch ($expr)
{
case 0:
echo 'Statements must be defined with colon' //Statement must be defined with a colon VIOLATION
break;
case 1:
echo 'Terminating statement must be indented to the same level as the CASE body';
break; //Terminating statement must be indented to the same level as the CASE body VIOLATION
case 2:
echo 'Terminating statement must be on a line by itself'; break; //Terminating statement must be on a line by itselfbreak; VIOLATION
case 3: echo 'Body must start on the line following the statement'; //Body must start on the line following the statement VIOLATION
break;
case 4:
echo 'case without no break statement'; // no break VIOLATION
case 5: //empty case body VIOLATION
case 6: // case is followed single space , correct coding standard VIOLATION
CASE 7:
echo 'keyword in lowercase'; // CASE keyword is in lowercase , correct coding standard VIOLATION
break;
case 8 :
echo 'any space before colon'; //space before the colon in a statement , //VIOLATION
break;
DEFAULT:
echo 'Default case'; // DEFAULT keyword in lowercase, correct coding standard echo VIOLATION
}
?<
Fixed Code Sample
<?php
switch ($expr)
{
case 0:
echo 'Statements must be defined with colon'; //Statement must be defined with a colon ,VIOLATION REMOVED
break;
case 1:
echo 'Terminating statement must be indented to the same level as the CASE body';
break; //Terminating statement must be indented to the same level as the CASE body
case 2:
echo 'Terminating statement must be on a line by itself'; //Terminating statement must be on a line by itself
break;
case 3:
echo 'Body must start on the line following the statement'; //Body must start on the line following the statement
break;
case 4:
echo 'case without no break statement';
break; // no break VIOLATION REMOVED
case 5:
echo 'case without body not allowed';
break; //empty case body VIOLATION REMOVED
case 6:
echo 'case is followed single space , correct coding standard';
break; // case is followed single space , correct coding standard VIOLATION REMOVED
case 7:
echo 'case keyword in lowercase'; // CASE keyword is in lowercase , //VIOLATION REMOVED
break;
case 8:
echo 'there must not be any space before colon'; //There must be no space before the colon in a statement , //VIOLATION REMOVED
break;
default:
echo 'Default case'; // DEFAULT keyword in lowercase, correct coding standard VIOLATION REMOVED
}
?>
Reference
Source Reference:
https://pear.php.net/reference/PHP_CodeSniffer-latest/PHP_CodeSniffer/PSR2_Sniffs_ControlStructures_SwitchDeclarationSniff.html
Related Technologies
Technical Criterion
Documentation - Style Conformity
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.