Skip to content

Commit 97bbb19

Browse files
committed
Squiz/InlineComment: Fix two more issues where errors are incorrectly not being thrown
If two subsequent lines contained comments with code between them, they would be seen as part of the same comment block, while in reality there are not. This meant that errors for `NotCapital` and `InvalidEndChar` where not always being thrown when they should be. Includes additional unit tests demonstrating the issue.
1 parent ae27699 commit 97bbb19

File tree

6 files changed

+40
-4
lines changed

6 files changed

+40
-4
lines changed

src/Standards/Squiz/Sniffs/Commenting/InlineCommentSniff.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,14 @@ public function process(File $phpcsFile, $stackPtr)
211211
// The below section determines if a comment block is correctly capitalised,
212212
// and ends in a full-stop. It will find the last comment in a block, and
213213
// work its way up.
214-
$nextComment = $phpcsFile->findNext(array(T_COMMENT), ($stackPtr + 1), null, false);
215-
if (($nextComment !== false)
216-
&& (($tokens[$nextComment]['line']) === ($tokens[$stackPtr]['line'] + 1))
214+
$nextComment = $phpcsFile->findNext(T_COMMENT, ($stackPtr + 1), null, false);
215+
if ($nextComment !== false
216+
&& $tokens[$nextComment]['line'] === ($tokens[$stackPtr]['line'] + 1)
217217
) {
218-
return;
218+
$nextNonWhitespace = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), $nextComment, true);
219+
if ($nextNonWhitespace === false) {
220+
return;
221+
}
219222
}
220223

221224
$topComment = $stackPtr;
@@ -225,6 +228,11 @@ public function process(File $phpcsFile, $stackPtr)
225228
break;
226229
}
227230

231+
$nextNonWhitespace = $phpcsFile->findNext(T_WHITESPACE, ($topComment + 1), $lastComment, true);
232+
if ($nextNonWhitespace !== false) {
233+
break;
234+
}
235+
228236
$lastComment = $topComment;
229237
}
230238

src/Standards/Squiz/Tests/Commenting/InlineCommentUnitTest.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ echo $foo;
123123
*/
124124
include_once($blah);
125125

126+
// some comment without capital or full stop
127+
echo $foo; // An unrelated comment.
128+
129+
// An unrelated comment.
130+
echo $foo; // some comment without capital or full stop
131+
126132
/*
127133
* N.B.: The below test line must be the last test in the file.
128134
* Testing that a new line after an inline comment when it's the last non-whitespace

src/Standards/Squiz/Tests/Commenting/InlineCommentUnitTest.inc.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ echo $foo;
116116
*/
117117
include_once($blah);
118118

119+
// some comment without capital or full stop
120+
echo $foo; // An unrelated comment.
121+
122+
// An unrelated comment.
123+
echo $foo; // some comment without capital or full stop
124+
119125
/*
120126
* N.B.: The below test line must be the last test in the file.
121127
* Testing that a new line after an inline comment when it's the last non-whitespace

src/Standards/Squiz/Tests/Commenting/InlineCommentUnitTest.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,9 @@ console.info(foo);
121121
//**
122122
* invalid comment
123123
*/
124+
125+
// some comment without capital or full stop
126+
console.log(foo); // An unrelated comment.
127+
128+
// An unrelated comment.
129+
console.log(foo); // some comment without capital or full stop

src/Standards/Squiz/Tests/Commenting/InlineCommentUnitTest.js.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,9 @@ console.info(foo);
117117
// **
118118
* invalid comment
119119
*/
120+
121+
// some comment without capital or full stop
122+
console.log(foo); // An unrelated comment.
123+
124+
// An unrelated comment.
125+
console.log(foo); // some comment without capital or full stop

src/Standards/Squiz/Tests/Commenting/InlineCommentUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function getErrorList($testFile='InlineCommentUnitTest.inc')
4444
96 => 1,
4545
97 => 3,
4646
118 => 1,
47+
126 => 2,
48+
130 => 2,
4749
);
4850

4951
return $errors;
@@ -60,6 +62,8 @@ public function getErrorList($testFile='InlineCommentUnitTest.inc')
6062
104 => 3,
6163
118 => 1,
6264
121 => 1,
65+
125 => 2,
66+
129 => 2,
6367
);
6468
default:
6569
return array();

0 commit comments

Comments
 (0)