Skip to content

Commit 2b1adc7

Browse files
committed
Fixed bug #19685 : PSR2 catch-22 with empty third statement in for loop
1 parent 371df28 commit 2b1adc7

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

CodeSniffer/Standards/Squiz/Sniffs/ControlStructures/ForLoopDeclarationSniff.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,22 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
114114
$phpcsFile->addError($error, $stackPtr, 'SpacingBeforeSecond');
115115
}
116116

117-
if ($tokens[($secondSemicolon + 1)]['code'] !== T_WHITESPACE) {
117+
if (($secondSemicolon + 1) !== $closingBracket
118+
&& $tokens[($secondSemicolon + 1)]['code'] !== T_WHITESPACE
119+
) {
118120
$error = 'Expected 1 space after second semicolon of FOR loop; 0 found';
119121
$phpcsFile->addError($error, $stackPtr, 'NoSpaceAfterSecond');
120122
} else {
121123
if (strlen($tokens[($secondSemicolon + 1)]['content']) !== 1) {
122-
$spaces = strlen($tokens[($firstSemicolon + 1)]['content']);
123-
$error = 'Expected 1 space after second semicolon of FOR loop; %s found';
124+
$spaces = strlen($tokens[($secondSemicolon + 1)]['content']);
124125
$data = array($spaces);
125-
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterSecond', $data);
126+
if (($secondSemicolon + 2) === $closingBracket) {
127+
$error = 'Expected no space after second semicolon of FOR loop; %s found';
128+
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterSecondNoThird', $data);
129+
} else {
130+
$error = 'Expected 1 space after second semicolon of FOR loop; %s found';
131+
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterSecond', $data);
132+
}
126133
}
127134
}
128135
}//end if

CodeSniffer/Standards/Squiz/Tests/ControlStructures/ForLoopDeclarationUnitTest.inc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,10 @@ for ($i = 0;$i < 10;$i++) {
2121
for ( $i = 0 ; $i < 10 ; $i++ ) {
2222
}
2323

24-
?>
24+
for ($i = 0; $i < 10;) {
25+
}
26+
27+
for ($i = 0; $i < 10; ) {
28+
}
29+
30+
?>

CodeSniffer/Standards/Squiz/Tests/ControlStructures/ForLoopDeclarationUnitTest.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@ for (var widgetid in this.loadedContents) {
2626
if (dfx.isset(widget) === true) {
2727
widget.loadAutoSaveCWidgetStore.setData('activeScreen', null);widget.getContents(this.loadedContents[widgetid], function() {self.widgetLoaded(widget.id);});
2828
}
29-
}
29+
}
30+
31+
for (var i = 0; i < 10;) {
32+
}
33+
for (var i = 0; i < 10; ) {
34+
}

CodeSniffer/Standards/Squiz/Tests/ControlStructures/ForLoopDeclarationUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function getErrorList($testFile='ForLoopDeclarationUnitTest.inc')
5252
14 => 2,
5353
17 => 2,
5454
21 => 6,
55+
27 => 1,
5556
);
5657
break;
5758
case 'ForLoopDeclarationUnitTest.js':
@@ -61,6 +62,7 @@ public function getErrorList($testFile='ForLoopDeclarationUnitTest.inc')
6162
12 => 2,
6263
15 => 2,
6364
19 => 6,
65+
33 => 1,
6466
);
6567
break;
6668
default:

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
6565
- Fixed bug #19644 : Alternative syntax, e.g. if/endif triggers Inline Control Structure error
6666
- Fixed bug #19655 : Closures reporting as multi-line when they are not
6767
- Fixed bug #19675 : Improper indent of nested anonymous function bodies in a call
68+
- Fixed bug #19685 : PSR2 catch-22 with empty third statement in for loop
6869
</notes>
6970
<contents>
7071
<dir name="/">

0 commit comments

Comments
 (0)