Skip to content

Commit 6938964

Browse files
committed
Fixed bug #20123 : PSR2 complains about an empty second statement in for-loop
1 parent 0bce916 commit 6938964

File tree

5 files changed

+19
-2
lines changed

5 files changed

+19
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
9494
$phpcsFile->addError($error, $stackPtr, 'SpacingBeforeFirst');
9595
}
9696

97-
if ($tokens[($firstSemicolon + 1)]['code'] !== T_WHITESPACE) {
97+
if ($tokens[($firstSemicolon + 1)]['code'] !== T_WHITESPACE
98+
&& $tokens[($firstSemicolon + 1)]['code'] !== T_SEMICOLON
99+
) {
98100
$error = 'Expected 1 space after first semicolon of FOR loop; 0 found';
99101
$phpcsFile->addError($error, $stackPtr, 'NoSpaceAfterFirst');
100102
} else {
@@ -109,7 +111,9 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
109111
$secondSemicolon = $phpcsFile->findNext(T_SEMICOLON, ($firstSemicolon + 1));
110112

111113
if ($secondSemicolon !== false) {
112-
if ($tokens[($secondSemicolon - 1)]['code'] === T_WHITESPACE) {
114+
if ($tokens[($secondSemicolon - 1)]['code'] === T_WHITESPACE
115+
&& $tokens[($firstSemicolon + 1)]['code'] !== T_SEMICOLON
116+
) {
113117
$error = 'Space found before second semicolon of FOR loop';
114118
$phpcsFile->addError($error, $stackPtr, 'SpacingBeforeSecond');
115119
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ for ($i = 0; $i < 10;) {
2727
for ($i = 0; $i < 10; ) {
2828
}
2929

30+
for ($i = 0; ; $i++) {
31+
}
32+
for ($i = 0;; $i++) {
33+
}
34+
3035
?>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ for (var i = 0; i < 10;) {
3232
}
3333
for (var i = 0; i < 10; ) {
3434
}
35+
36+
for (var i = 0; ; i++) {
37+
}
38+
for (var i = 0;; i++) {
39+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function getErrorList($testFile='ForLoopDeclarationUnitTest.inc')
5353
17 => 2,
5454
21 => 6,
5555
27 => 1,
56+
30 => 1,
5657
);
5758
break;
5859
case 'ForLoopDeclarationUnitTest.js':
@@ -63,6 +64,7 @@ public function getErrorList($testFile='ForLoopDeclarationUnitTest.inc')
6364
15 => 2,
6465
19 => 6,
6566
33 => 1,
67+
36 => 1,
6668
);
6769
break;
6870
default:

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
3737
- Exit statements are now recognised as valid closers for CASE and DEFAULT blocks
3838
- Fixed bug #20097 : CLI.php throws error in php 5.2
3939
- Fixed bug #20100 : incorrect Function mysql() has been deprecated report
40+
- Fixed bug #20123 : PSR2 complains about an empty second statement in for-loop
4041
</notes>
4142
<contents>
4243
<dir name="/">

0 commit comments

Comments
 (0)