Skip to content

Commit 8cbccf5

Browse files
committed
Further fix for bug #683
1 parent 3e326cb commit 8cbccf5

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

CodeSniffer/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,14 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
122122
}
123123
}
124124

125-
$next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($opener + 1), null, true);
125+
$next = $phpcsFile->findNext(T_WHITESPACE, ($opener + 1), null, true);
126+
if ($tokens[$next]['line'] === $tokens[$opener]['line']
127+
&& $tokens[$next]['code'] === T_COMMENT
128+
) {
129+
// Skip comments on the same line.
130+
$next = $phpcsFile->findNext(T_WHITESPACE, ($next + 1), null, true);
131+
}
132+
126133
if ($tokens[$next]['line'] !== ($tokens[$opener]['line'] + 1)) {
127134
$error = 'The '.strtoupper($type).' body must start on the line following the statement';
128135
$fix = $phpcsFile->addFixableError($error, $nextCase, 'SpaceBeforeColon'.strtoupper($type));

CodeSniffer/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc

+10-1
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,21 @@ switch ($foo) {
115115
}
116116

117117
switch ($foo) {
118-
case 'bar': // some comment
118+
case 'foo': // some comment
119+
echo 'foo';
120+
break;
121+
case 'bar':
122+
// some comment
119123
echo 'bar';
120124
break;
121125
case 'baz': // other comment
122126
echo 'baz';
123127
break;
128+
case 'boo':
129+
130+
// other comment
131+
echo 'boo';
132+
break;
124133
default: // other comment
125134
echo 'default';
126135
break;

CodeSniffer/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc.fixed

+9-1
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,20 @@ switch ($foo) {
119119
}
120120

121121
switch ($foo) {
122-
case 'bar': // some comment
122+
case 'foo': // some comment
123+
echo 'foo';
124+
break;
125+
case 'bar':
126+
// some comment
123127
echo 'bar';
124128
break;
125129
case 'baz': // other comment
126130
echo 'baz';
127131
break;
132+
case 'boo':
133+
// other comment
134+
echo 'boo';
135+
break;
128136
default: // other comment
129137
echo 'default';
130138
break;

CodeSniffer/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function getErrorList()
5555
111 => 1,
5656
113 => 2,
5757
114 => 1,
58+
128 => 1,
5859
);
5960

6061
}//end getErrorList()

0 commit comments

Comments
 (0)