diff --git a/src/Standards/Generic/Sniffs/CodeAnalysis/EmptyStatementSniff.php b/src/Standards/Generic/Sniffs/CodeAnalysis/EmptyStatementSniff.php index 3fe5fb7d92..c9aa8dd782 100644 --- a/src/Standards/Generic/Sniffs/CodeAnalysis/EmptyStatementSniff.php +++ b/src/Standards/Generic/Sniffs/CodeAnalysis/EmptyStatementSniff.php @@ -30,6 +30,13 @@ class EmptyStatementSniff implements Sniff { + /** + * Whether to allow statements that contain only comments. + * + * @var boolean + */ + public $allowComments = false; + /** * Registers the tokens that this sniff wants to listen for. @@ -74,10 +81,16 @@ public function process(File $phpcsFile, $stackPtr) return; } + if ($this->allowComments === true) { + $emptyTokens = ([T_WHITESPACE => T_WHITESPACE] + Tokens::$phpcsCommentTokens); + } else { + $emptyTokens = Tokens::$emptyTokens; + } + $next = $phpcsFile->findNext( - Tokens::$emptyTokens, + $emptyTokens, ($token['scope_opener'] + 1), - ($token['scope_closer'] - 1), + $token['scope_closer'], true ); diff --git a/src/Standards/Generic/Tests/CodeAnalysis/EmptyStatementUnitTest.inc b/src/Standards/Generic/Tests/CodeAnalysis/EmptyStatementUnitTest.inc index 83780bce24..492f28cede 100644 --- a/src/Standards/Generic/Tests/CodeAnalysis/EmptyStatementUnitTest.inc +++ b/src/Standards/Generic/Tests/CodeAnalysis/EmptyStatementUnitTest.inc @@ -69,4 +69,18 @@ try { // TODO: Handle this exception later :-) } -if (true) {} elseif (false) {} \ No newline at end of file +if (true) {} elseif (false) {} + +// phpcs:set Generic.CodeAnalysis.EmptyStatement allowComments true + +if ($foo) { + // Just a comment +} elseif ($bar) { + /* + * Yet another comment + */ +} elseif ($baz) { + // phpcs:set Generic.CodeAnalysis.EmptyStatement somethingMadeUp true +} else { + +} diff --git a/src/Standards/Generic/Tests/CodeAnalysis/EmptyStatementUnitTest.php b/src/Standards/Generic/Tests/CodeAnalysis/EmptyStatementUnitTest.php index 9d89ce2456..266e3e4bf8 100644 --- a/src/Standards/Generic/Tests/CodeAnalysis/EmptyStatementUnitTest.php +++ b/src/Standards/Generic/Tests/CodeAnalysis/EmptyStatementUnitTest.php @@ -39,6 +39,8 @@ public function getErrorList() 64 => 1, 68 => 1, 72 => 2, + 82 => 1, + 84 => 1, ]; }//end getErrorList()