Skip to content

Commit 010fafd

Browse files
committed
Generic/EmptyStatementSniff: Allow comments to be considered non-empty
1 parent d35d320 commit 010fafd

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/Standards/Generic/Sniffs/CodeAnalysis/EmptyStatementSniff.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
class EmptyStatementSniff implements Sniff
3131
{
3232

33+
/**
34+
* Empty statements which should not be reported when there is a comment in the body.
35+
*
36+
* @var string[]
37+
*/
38+
public $allowComment = [];
39+
3340

3441
/**
3542
* Registers the tokens that this sniff wants to listen for.
@@ -75,10 +82,16 @@ public function process(File $phpcsFile, $stackPtr)
7582
return;
7683
}
7784

85+
if (in_array(strtolower($token['content']), $this->allowComment, true) === true) {
86+
$skipTokens = T_WHITESPACE;
87+
} else {
88+
$skipTokens = Tokens::$emptyTokens;
89+
}
90+
7891
$next = $phpcsFile->findNext(
79-
Tokens::$emptyTokens,
92+
$skipTokens,
8093
($token['scope_opener'] + 1),
81-
($token['scope_closer'] - 1),
94+
$token['scope_closer'],
8295
true
8396
);
8497

src/Standards/Generic/Tests/CodeAnalysis/EmptyStatementUnitTest.inc

+18
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,21 @@ try {
7272
if (true) {} elseif (false) {}
7373

7474
match($foo) {};
75+
76+
// phpcs:set Generic.CodeAnalysis.EmptyStatement allowComment[] elseif,catch
77+
78+
if ($foo) {
79+
// Should complain here
80+
} elseif ($bar) {
81+
// Should not complain here
82+
} else {
83+
// Should complain here
84+
}
85+
86+
TRY {
87+
// Should complain here
88+
} CATCH (Exception $exception) {
89+
// Should not complain here
90+
}
91+
92+
// phpcs:set Generic.CodeAnalysis.EmptyStatement allowComment[]

src/Standards/Generic/Tests/CodeAnalysis/EmptyStatementUnitTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public function getErrorList()
4040
68 => 1,
4141
72 => 2,
4242
74 => 1,
43+
78 => 1,
44+
82 => 1,
45+
86 => 1,
4346
];
4447

4548
}//end getErrorList()

0 commit comments

Comments
 (0)