Skip to content

Commit 40a97fc

Browse files
authored
Improve comment handling (#20)
* add new test case * don't flag inline comments
1 parent 79ee409 commit 40a97fc

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

Pantheon-WP-Minimum/Sniffs/Commenting/DisallowMultilineSlashCommentSniff.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,24 @@ public static function getConsecutiveSingleLineComments( File $phpcsFile, $stack
5555
return;
5656
}
5757

58-
// Check the previous line to see if we are already in a block.
58+
// If the comment is not on a line by itself, it's not part of a multi-line comment block.
59+
$prev = $phpcsFile->findPrevious( T_WHITESPACE, $stackPtr - 1, null, true );
60+
if ( false !== $prev && $tokens[ $prev ]['line'] === $token['line'] ) {
61+
return;
62+
}
63+
64+
// Check the previous line to see if we are already in a block of non-inline comments.
5965
$prevLine = $token['line'] - 1;
60-
$prevComment = $phpcsFile->findPrevious( T_COMMENT, $stackPtr - 1, null, false, null, true );
61-
if ( false !== $prevComment && $tokens[$prevComment]['line'] === $prevLine ) {
62-
if ( '//' === substr( $tokens[$prevComment]['content'], 0, 2 ) ) {
63-
return; // This is not the start of the block.
66+
$prevCommentPtr = $phpcsFile->findPrevious( T_COMMENT, $stackPtr - 1, null, false, null, true );
67+
if ( false !== $prevCommentPtr && $tokens[ $prevCommentPtr ]['line'] === $prevLine ) {
68+
$prevCommentToken = $tokens[ $prevCommentPtr ];
69+
if ( '//' === substr( $prevCommentToken['content'], 0, 2 ) ) {
70+
// Was the previous comment also a non-inline comment?
71+
$prev = $phpcsFile->findPrevious( T_WHITESPACE, $prevCommentPtr - 1, null, true );
72+
if ( false === $prev || $tokens[ $prev ]['line'] !== $prevCommentToken['line'] ) {
73+
// Yes, it was. So we are in the middle of a block.
74+
return;
75+
}
6476
}
6577
}
6678

@@ -69,9 +81,16 @@ public static function getConsecutiveSingleLineComments( File $phpcsFile, $stack
6981
$nextComment = $stackPtr;
7082
while ( true ) {
7183
$nextComment = $phpcsFile->findNext( T_COMMENT, $nextComment + 1, null, false, null, true );
72-
if ( false === $nextComment || $tokens[$nextComment]['line'] !== ( $token['line'] + $lineCount ) ) {
84+
if ( false === $nextComment || $tokens[ $nextComment ]['line'] !== ( $token['line'] + $lineCount ) ) {
7385
break; // The block has ended.
7486
}
87+
88+
// If the next comment is not on a line by itself, it's an inline comment, so the block ends here.
89+
$prev = $phpcsFile->findPrevious( T_WHITESPACE, $nextComment - 1, null, true );
90+
if ( false !== $prev && $tokens[ $prev ]['line'] === $tokens[ $nextComment ]['line'] ) {
91+
break;
92+
}
93+
7594
$lineCount++;
7695
}
7796

tests/test-disallow-multiline-slash-comment.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,10 @@
2828
* I oughta swear? Well, now we gotta sneak this back into my laboratory, we've
2929
* gotta get you home. Jennifer.
3030
*/
31+
32+
// This should be allowed.
33+
$an_array = [ // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
34+
'key1' => 1, // The first value in the array.
35+
'key2' => 2, // The second value in the array.
36+
'key3' => 3, // The third value in the array.
37+
];

0 commit comments

Comments
 (0)