Skip to content

Commit

Permalink
Add more check for emptyPhpStatement
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed May 8, 2019
1 parent 1d7c657 commit f50d9c3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,14 @@ function_call();
<?php
// Guard against false positives for two consecutive semi-colons in a for statement.
for ( $i = 0; ; $i++ ) {}

// Test for useless semi-colons
for ( $i = 0; ; $i++ ) {};

if (true) {};

while (true) {};

// Do not break closure and anonymous class;
$a = function () {};
$b = new class {};
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,14 @@ function_call();
<?php
// Guard against false positives for two consecutive semi-colons in a for statement.
for ( $i = 0; ; $i++ ) {}

// Test for useless semi-colons
for ( $i = 0; ; $i++ ) {}

if (true) {}

while (true) {}

// Do not break closure and anonymous class;
$a = function () {};
$b = new class {};
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public function getWarningList()
45 => 1,
49 => 1,
50 => 1,
57 => 1,
59 => 1,
61 => 1,
];

}//end getWarningList()
Expand Down

0 comments on commit f50d9c3

Please sign in to comment.