Skip to content

Commit

Permalink
Generic/EmptyStatementSniff: Allow comments to be considered non-empty
Browse files Browse the repository at this point in the history
  • Loading branch information
jlherren committed Aug 12, 2021
1 parent d35d320 commit 010fafd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/Standards/Generic/Sniffs/CodeAnalysis/EmptyStatementSniff.php
Expand Up @@ -30,6 +30,13 @@
class EmptyStatementSniff implements Sniff
{

/**
* Empty statements which should not be reported when there is a comment in the body.
*
* @var string[]
*/
public $allowComment = [];


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

if (in_array(strtolower($token['content']), $this->allowComment, true) === true) {
$skipTokens = T_WHITESPACE;
} else {
$skipTokens = Tokens::$emptyTokens;
}

$next = $phpcsFile->findNext(
Tokens::$emptyTokens,
$skipTokens,
($token['scope_opener'] + 1),
($token['scope_closer'] - 1),
$token['scope_closer'],
true
);

Expand Down
Expand Up @@ -72,3 +72,21 @@ try {
if (true) {} elseif (false) {}

match($foo) {};

// phpcs:set Generic.CodeAnalysis.EmptyStatement allowComment[] elseif,catch

if ($foo) {
// Should complain here
} elseif ($bar) {
// Should not complain here
} else {
// Should complain here
}

TRY {
// Should complain here
} CATCH (Exception $exception) {
// Should not complain here
}

// phpcs:set Generic.CodeAnalysis.EmptyStatement allowComment[]
Expand Up @@ -40,6 +40,9 @@ public function getErrorList()
68 => 1,
72 => 2,
74 => 1,
78 => 1,
82 => 1,
86 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit 010fafd

Please sign in to comment.