Skip to content

Commit

Permalink
Fixed bug #857 : PSR2.ControlStructure.SwitchDeclaration shouldn't ch…
Browse files Browse the repository at this point in the history
…eck indent of curly brace closers
  • Loading branch information
gsherwood committed Jan 12, 2016
1 parent d81bdb8 commit 210189a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}
}

$opener = $tokens[$nextCase]['scope_opener'];
$opener = $tokens[$nextCase]['scope_opener'];
$nextCloser = $tokens[$nextCase]['scope_closer'];
if ($tokens[$opener]['code'] === T_COLON) {
if ($tokens[($opener - 1)]['code'] === T_WHITESPACE) {
$error = 'There must be no space before the colon in a '.strtoupper($type).' statement';
Expand Down Expand Up @@ -152,38 +153,37 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}
}
}//end if
} else {
$error = strtoupper($type).' statements must be defined using a colon';
$phpcsFile->addError($error, $nextCase, 'WrongOpener'.$type);
}//end if

$nextCloser = $tokens[$nextCase]['scope_closer'];
if ($tokens[$nextCloser]['scope_condition'] === $nextCase) {
// Only need to check some things once, even if the
// closer is shared between multiple case statements, or even
// the default case.
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($nextCloser - 1), $nextCase, true);
if ($tokens[$prev]['line'] === $tokens[$nextCloser]['line']) {
$error = 'Terminating statement must be on a line by itself';
$fix = $phpcsFile->addFixableError($error, $nextCloser, 'BreakNotNewLine');
if ($fix === true) {
$phpcsFile->fixer->addNewLine($prev);
$phpcsFile->fixer->replaceToken($nextCloser, trim($tokens[$nextCloser]['content']));
}
} else {
$diff = ($caseAlignment + $this->indent - $tokens[$nextCloser]['column']);
if ($diff !== 0) {
$error = 'Terminating statement must be indented to the same level as the CASE body';
$fix = $phpcsFile->addFixableError($error, $nextCloser, 'BreakIndent');
if ($tokens[$nextCloser]['scope_condition'] === $nextCase) {
// Only need to check some things once, even if the
// closer is shared between multiple case statements, or even
// the default case.
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($nextCloser - 1), $nextCase, true);
if ($tokens[$prev]['line'] === $tokens[$nextCloser]['line']) {
$error = 'Terminating statement must be on a line by itself';
$fix = $phpcsFile->addFixableError($error, $nextCloser, 'BreakNotNewLine');
if ($fix === true) {
if ($diff > 0) {
$phpcsFile->fixer->addContentBefore($nextCloser, str_repeat(' ', $diff));
} else {
$phpcsFile->fixer->substrToken(($nextCloser - 1), 0, $diff);
$phpcsFile->fixer->addNewLine($prev);
$phpcsFile->fixer->replaceToken($nextCloser, trim($tokens[$nextCloser]['content']));
}
} else {
$diff = ($caseAlignment + $this->indent - $tokens[$nextCloser]['column']);
if ($diff !== 0) {
$error = 'Terminating statement must be indented to the same level as the CASE body';
$fix = $phpcsFile->addFixableError($error, $nextCloser, 'BreakIndent');
if ($fix === true) {
if ($diff > 0) {
$phpcsFile->fixer->addContentBefore($nextCloser, str_repeat(' ', $diff));
} else {
$phpcsFile->fixer->substrToken(($nextCloser - 1), 0, $diff);
}
}
}
}
}//end if
}//end if
} else {
$error = strtoupper($type).' statements must be defined using a colon';
$phpcsFile->addError($error, $nextCase, 'WrongOpener'.$type);
}//end if

// We only want cases from here on in.
Expand Down
25 changes: 18 additions & 7 deletions CodeSniffer/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -1172,13 +1172,24 @@ public function processAdditional(&$tokens, $eolChar)
// not whatever it already is. The opener needs to be the opening curly
// brace so everything matches up.
$newCloser = $tokens[$x]['bracket_closer'];
$tokens[$i]['scope_closer'] = $newCloser;
$tokens[$x]['scope_closer'] = $newCloser;
$tokens[$i]['scope_opener'] = $x;
$tokens[$x]['scope_condition'] = $i;
$tokens[$newCloser]['scope_condition'] = $i;
$tokens[$newCloser]['scope_opener'] = $x;
$tokens[$newCloser]['scope_closer'] = $newCloser;
foreach (array($i, $x, $newCloser) as $index) {
$tokens[$index]['scope_condition'] = $i;
$tokens[$index]['scope_opener'] = $x;
$tokens[$index]['scope_closer'] = $newCloser;
}

unset($tokens[$scopeOpener]['scope_condition']);
unset($tokens[$scopeOpener]['scope_opener']);
unset($tokens[$scopeOpener]['scope_closer']);
unset($tokens[$scopeCloser]['scope_condition']);
unset($tokens[$scopeCloser]['scope_opener']);
unset($tokens[$scopeCloser]['scope_closer']);
unset($tokens[$x]['bracket_opener']);
unset($tokens[$x]['bracket_closer']);
unset($tokens[$newCloser]['bracket_opener']);
unset($tokens[$newCloser]['bracket_closer']);
$tokens[$scopeCloser]['conditions'][] = $i;

if (PHP_CODESNIFFER_VERBOSITY > 1) {
$line = $tokens[$i]['line'];
$tokenType = $tokens[$i]['type'];
Expand Down
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Thanks to Tim Bezhashvyly for the patch
- Fixed bug #852 : Generic.Commenting.DocComment not finding errors when long description is omitted
- Fixed bug #855 : Capital letter detection for multibyte strings doesn't work correctly
- Fixed bug #857 : PSR2.ControlStructure.SwitchDeclaration shouldn't check indent of curly brace closers
- Fixed bug #21005 : Incorrect indent detection when multiple properties are initialized to arrays
- Fixed bug #21010 : Incorrect missing colon detection in CSS when first style is not on new line
- Fixed bug #21011 : Incorrect error message text when newline found after opening brace
Expand Down

0 comments on commit 210189a

Please sign in to comment.