Skip to content

Commit 1741355

Browse files
committed
EmptyCommentSniff: empty comments in multiline commented code are not reported
1 parent 72ca17f commit 1741355

File tree

5 files changed

+179
-5
lines changed

5 files changed

+179
-5
lines changed

SlevomatCodingStandard/Sniffs/Commenting/EmptyCommentSniff.php

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,17 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $commentStartPoi
3636

3737
$commentContent = $this->getCommentContent($phpcsFile, $commentStartPointer, $commentEndPointer);
3838

39-
$isEmpty = $this->isLineComment($phpcsFile, $commentStartPointer)
40-
? (bool) preg_match('~^\\s*$~', $commentContent)
41-
: (bool) preg_match('~^[\\s\*]*$~', $commentContent);
39+
$isLineComment = $this->isLineComment($phpcsFile, $commentStartPointer);
40+
$isEmpty = $this->isEmpty($commentContent, $isLineComment);
4241

4342
if (!$isEmpty) {
4443
return;
4544
}
4645

46+
if ($isLineComment && $this->isPartOfMultilineInlineComments($phpcsFile, $commentStartPointer, $commentEndPointer)) {
47+
return;
48+
}
49+
4750
$fix = $phpcsFile->addFixableError(
4851
'Empty comment',
4952
$commentStartPointer,
@@ -96,6 +99,13 @@ private function isLineComment(\PHP_CodeSniffer\Files\File $phpcsFile, int $comm
9699
return (bool) preg_match('~^(?://|#)(.*)~', $phpcsFile->getTokens()[$commentPointer]['content']);
97100
}
98101

102+
private function isEmpty(string $comment, bool $isLineComment): bool
103+
{
104+
return $isLineComment
105+
? (bool) preg_match('~^\\s*$~', $comment)
106+
: (bool) preg_match('~^[\\s\*]*$~', $comment);
107+
}
108+
99109
private function getCommentEndPointer(\PHP_CodeSniffer\Files\File $phpcsFile, int $commentStartPointer): ?int
100110
{
101111
$tokens = $phpcsFile->getTokens();
@@ -132,4 +142,76 @@ private function getCommentContent(\PHP_CodeSniffer\Files\File $phpcsFile, int $
132142
return substr(TokenHelper::getContent($phpcsFile, $commentStartPointer, $commentEndPointer), 2, -2);
133143
}
134144

145+
private function isPartOfMultilineInlineComments(\PHP_CodeSniffer\Files\File $phpcsFile, int $commentStartPointer, int $commentEndPointer): bool
146+
{
147+
if (!$this->isNonEmptyLineCommentBefore($phpcsFile, $commentStartPointer)) {
148+
return false;
149+
}
150+
151+
if (!$this->isNonEmptyLineCommentAfter($phpcsFile, $commentStartPointer, $commentEndPointer)) {
152+
return false;
153+
}
154+
155+
return true;
156+
}
157+
158+
private function isNonEmptyLineCommentBefore(\PHP_CodeSniffer\Files\File $phpcsFile, int $commentStartPointer): bool
159+
{
160+
$tokens = $phpcsFile->getTokens();
161+
162+
/** @var int $beforeCommentStartPointer */
163+
$beforeCommentStartPointer = TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $commentStartPointer - 1);
164+
165+
if ($tokens[$beforeCommentStartPointer]['code'] !== T_COMMENT) {
166+
return false;
167+
}
168+
169+
if (!$this->isLineComment($phpcsFile, $beforeCommentStartPointer)) {
170+
return false;
171+
}
172+
173+
if ($tokens[$beforeCommentStartPointer]['line'] + 1 !== $tokens[$commentStartPointer]['line']) {
174+
return false;
175+
}
176+
177+
/** @var int $beforeCommentEndPointer */
178+
$beforeCommentEndPointer = $this->getCommentEndPointer($phpcsFile, $beforeCommentStartPointer);
179+
if (!$this->isEmpty($this->getCommentContent($phpcsFile, $beforeCommentStartPointer, $beforeCommentEndPointer), true)) {
180+
return true;
181+
}
182+
183+
return $this->isNonEmptyLineCommentBefore($phpcsFile, $beforeCommentStartPointer);
184+
}
185+
186+
private function isNonEmptyLineCommentAfter(\PHP_CodeSniffer\Files\File $phpcsFile, int $commentStartPointer, int $commentEndPointer): bool
187+
{
188+
$tokens = $phpcsFile->getTokens();
189+
190+
$afterCommentStartPointer = TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $commentEndPointer + 1);
191+
192+
if ($afterCommentStartPointer === null) {
193+
return false;
194+
}
195+
196+
if ($tokens[$afterCommentStartPointer]['code'] !== T_COMMENT) {
197+
return false;
198+
}
199+
200+
if (!$this->isLineComment($phpcsFile, $afterCommentStartPointer)) {
201+
return false;
202+
}
203+
204+
if ($tokens[$commentEndPointer]['line'] + 1 !== $tokens[$afterCommentStartPointer]['line']) {
205+
return false;
206+
}
207+
208+
/** @var int $afterCommentEndPointer */
209+
$afterCommentEndPointer = $this->getCommentEndPointer($phpcsFile, $afterCommentStartPointer);
210+
if (!$this->isEmpty($this->getCommentContent($phpcsFile, $afterCommentStartPointer, $afterCommentEndPointer), true)) {
211+
return true;
212+
}
213+
214+
return $this->isNonEmptyLineCommentAfter($phpcsFile, $afterCommentStartPointer, $afterCommentEndPointer);
215+
}
216+
135217
}

tests/Sniffs/Commenting/EmptyCommentSniffTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public function testErrors(): void
1515
{
1616
$report = self::checkFile(__DIR__ . '/data/emptyCommentErrors.php');
1717

18-
self::assertSame(19, $report->getErrorCount());
18+
self::assertSame(33, $report->getErrorCount());
1919

20-
foreach ([3, 5, 7, 8, 10, 12, 15, 19, 20, 22, 24, 27, 31, 35, 39, 43, 50, 55, 60] as $line) {
20+
foreach ([3, 5, 7, 8, 10, 12, 15, 19, 20, 22, 24, 27, 31, 35, 39, 43, 50, 55, 60, 65, 69, 74, 76, 84, 86, 91, 96, 97, 98, 103, 104, 107, 113] as $line) {
2121
self::assertSniffError($report, $line, EmptyCommentSniff::CODE_EMPTY_COMMENT);
2222
}
2323

tests/Sniffs/Commenting/data/emptyCommentErrors.fixed.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,35 @@ public function method()
3030
}
3131

3232
}
33+
34+
// Line comment
35+
36+
// Line comment
37+
/*
38+
Block comment
39+
*/
40+
/*
41+
Block comment
42+
*/
43+
44+
if (true) {
45+
46+
} // Line comment
47+
if (true) {
48+
49+
}
50+
51+
if (true) {
52+
53+
}
54+
55+
if (true) {
56+
57+
}
58+
59+
// Line comment
60+
if (true) {
61+
62+
}
63+
64+
// Line comment

tests/Sniffs/Commenting/data/emptyCommentErrors.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,53 @@ public function method()
6161
}
6262

6363
}
64+
65+
//
66+
// Line comment
67+
68+
// Line comment
69+
//
70+
71+
/*
72+
Block comment
73+
*/
74+
//
75+
76+
//
77+
/*
78+
Block comment
79+
*/
80+
81+
if (true) {
82+
83+
} // Line comment
84+
//
85+
86+
//
87+
if (true) {
88+
89+
}
90+
91+
//
92+
if (true) {
93+
94+
}
95+
96+
//
97+
//
98+
//
99+
if (true) {
100+
101+
}
102+
103+
//
104+
//
105+
106+
// Line comment
107+
//
108+
if (true) {
109+
110+
}
111+
112+
// Line comment
113+
//

tests/Sniffs/Commenting/data/emptyCommentNoErrors.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,13 @@
2525
/**
2626
//
2727
*/
28+
29+
// Line comment
30+
//
31+
// Line comment
32+
33+
// Line comment
34+
//
35+
//
36+
//
37+
// Line comment

0 commit comments

Comments
 (0)