Skip to content

Commit

Permalink
This fixes an indentation bug when switch cases fall through to the next
Browse files Browse the repository at this point in the history
case.
  • Loading branch information
BRMatt committed Oct 2, 2010
1 parent 189dcb3 commit 31f70b4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

/**
* Throws errors if switch structures do not conform to the coding standard.
* Throws errors if switch structures do not conform to the coding standard.
*
* @category PHP
* @package PHP_CodeSniffer
Expand Down Expand Up @@ -41,9 +41,9 @@ public function register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the
* @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the
* document
* @param int $stackPtr Position of the current token in the stack passed
* @param int $stackPtr Position of the current token in the stack passed
* in $tokens
* @return void
*/
Expand All @@ -52,7 +52,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens();
$line = $tokens[$stackPtr]['line'];
$tokenCount = $phpcsFile->numTokens - 1;

// Each case, break, and default should be on a separate line
$start = $end = $stackPtr;
while ($tokens[$start]['line'] == $line && $start > 0) {
Expand Down Expand Up @@ -91,8 +91,8 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)

// Code inside case and default blocks should be indented
for (
$open = $tokens[$stackPtr]['scope_opener'];
$tokens[$open]['line'] == $line;
$open = $tokens[$stackPtr]['scope_opener'];
$tokens[$open]['line'] == $line;
$open++
);

Expand All @@ -107,8 +107,25 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$tabString = str_repeat("\t", $tabCount);

foreach (range($open, $close) as $tokenPtr) {

// The first condition checks that we're on a new line (so we can check the indent)
// The second checks to see if there is sufficient indent
if ($tokens[$tokenPtr]['line'] == $tokens[$tokenPtr - 1]['line'] + 1
&& substr($tokens[$tokenPtr]['content'], 0, $tabCount) != $tabString) {

// We now need to have a look along the line and see if there're any case / default
// tags in case we're allowing conditions to drop through
for (
$localTokenPtr = $tokenPtr;
$tokens[$localTokenPtr]['line'] === $tokens[$localTokenPtr + 1]['line'];
$localTokenPtr++
) {
// If there's a break or default tag on this line then we need to move onto the next line
if(in_array($tokens[$localTokenPtr]['type'], array('T_CASE', 'T_DEFAULT'))) {
continue 2;
}
}

$phpcsFile->addError('Code inside case and default blocks should be indented', $tokenPtr);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ switch (true) {
echo 'default case';
break;
}

switch(TRUE) {
case TRUE:
return TRUE;
case FALSE:
return FALSE;
default:
return 'pie';
}

0 comments on commit 31f70b4

Please sign in to comment.