diff --git a/src/Standards/Generic/Sniffs/CodeAnalysis/EmptyPHPStatementSniff.php b/src/Standards/Generic/Sniffs/CodeAnalysis/EmptyPHPStatementSniff.php index 03a6f09985..63ed99b0c3 100644 --- a/src/Standards/Generic/Sniffs/CodeAnalysis/EmptyPHPStatementSniff.php +++ b/src/Standards/Generic/Sniffs/CodeAnalysis/EmptyPHPStatementSniff.php @@ -20,6 +20,27 @@ class EmptyPHPStatementSniff implements Sniff { + /** + * Any token that should not have a scope_closer followed by a semi-colon. + * + * @var int[] + */ + private $conditionTokens = [ + T_IF, + T_SWITCH, + T_CASE, + T_WHILE, + T_ELSE, + T_ELSEIF, + T_FOR, + T_FOREACH, + T_DO, + T_TRY, + T_CATCH, + T_FINALLY, + ]; + + /** * Returns an array of tokens this test wants to listen for. * @@ -58,7 +79,13 @@ public function process(File $phpcsFile, $stackPtr) && $tokens[$prevNonEmpty]['code'] !== T_OPEN_TAG && $tokens[$prevNonEmpty]['code'] !== T_OPEN_TAG_WITH_ECHO) ) { - return; + // Still detect `if (foo) {};`. + if ($tokens[$prevNonEmpty]['code'] !== T_CLOSE_CURLY_BRACKET + || isset($tokens[$prevNonEmpty]['scope_condition']) === false + || in_array($tokens[$tokens[$prevNonEmpty]['scope_condition']]['code'], $this->conditionTokens) === false + ) { + return; + } } if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { diff --git a/src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.inc b/src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.inc index 7c7d3d804c..f46906f14f 100644 --- a/src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.inc +++ b/src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.inc @@ -52,3 +52,14 @@ function_call(); 1, 49 => 1, 50 => 1, + 57 => 1, + 59 => 1, + 61 => 1, ]; }//end getWarningList()