Skip to content

Commit

Permalink
Merge branch 'emptyPhpStatement' of https://github.com/VincentLanglet…
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Oct 30, 2019
2 parents 7f1ee75 + cbcbba4 commit f23c989
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,26 @@ public function process(File $phpcsFile, $stackPtr)
case 'T_SEMICOLON':
$prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);

if ($prevNonEmpty === false
|| ($tokens[$prevNonEmpty]['code'] !== T_SEMICOLON
if ($prevNonEmpty === false) {
return;
}

if ($tokens[$prevNonEmpty]['code'] !== T_SEMICOLON
&& $tokens[$prevNonEmpty]['code'] !== T_OPEN_TAG
&& $tokens[$prevNonEmpty]['code'] !== T_OPEN_TAG_WITH_ECHO)
&& $tokens[$prevNonEmpty]['code'] !== T_OPEN_TAG_WITH_ECHO
) {
return;
if ($tokens[$prevNonEmpty]['code'] !== T_CLOSE_CURLY_BRACKET
|| isset($tokens[$prevNonEmpty]['scope_condition']) === false
) {
return;
}

$scopeOwner = $tokens[$tokens[$prevNonEmpty]['scope_condition']]['code'];
if ($scopeOwner === T_CLOSURE || $scopeOwner === T_ANON_CLASS) {
return;
}

// Else, it's something like `if (foo) {};` and the semi-colon is not needed.
}

if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,26 @@ 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) {};

class ABC {} ; ;

switch ( $a ) {
case 1:
break;
case 2:
break;
default:
break; ;
};

// Do not break closures and anonymous classes and curlies without scope owners.
$a = function () {};
$b = new class {};
echo $a{0};
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,26 @@ 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) {}

class ABC {}

switch ( $a ) {
case 1:
break;
case 2:
break;
default:
break;
}

// Do not break closures and anonymous classes and curlies without scope owners.
$a = function () {};
$b = new class {};
echo $a{0};
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public function getWarningList()
45 => 1,
49 => 1,
50 => 1,
57 => 1,
59 => 1,
61 => 1,
63 => 2,
71 => 1,
72 => 1,
];

}//end getWarningList()
Expand Down

0 comments on commit f23c989

Please sign in to comment.