Skip to content

Commit 6cc41f7

Browse files
Review
1 parent f50d9c3 commit 6cc41f7

File tree

3 files changed

+9
-25
lines changed

3 files changed

+9
-25
lines changed

src/Standards/Generic/Sniffs/CodeAnalysis/EmptyPHPStatementSniff.php

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,6 @@ class EmptyPHPStatementSniff implements Sniff
2020
{
2121

2222

23-
/**
24-
* Any token that should not have a scope_closer followed by a semi-colon.
25-
*
26-
* @var int[]
27-
*/
28-
private $conditionTokens = [
29-
T_IF,
30-
T_SWITCH,
31-
T_CASE,
32-
T_WHILE,
33-
T_ELSE,
34-
T_ELSEIF,
35-
T_FOR,
36-
T_FOREACH,
37-
T_DO,
38-
T_TRY,
39-
T_CATCH,
40-
T_FINALLY,
41-
];
42-
43-
4423
/**
4524
* Returns an array of tokens this test wants to listen for.
4625
*
@@ -74,15 +53,18 @@ public function process(File $phpcsFile, $stackPtr)
7453
case 'T_SEMICOLON':
7554
$prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
7655

77-
if ($prevNonEmpty === false
78-
|| ($tokens[$prevNonEmpty]['code'] !== T_SEMICOLON
56+
if ($prevNonEmpty === false) {
57+
return;
58+
}
59+
60+
if ($tokens[$prevNonEmpty]['code'] !== T_SEMICOLON
7961
&& $tokens[$prevNonEmpty]['code'] !== T_OPEN_TAG
80-
&& $tokens[$prevNonEmpty]['code'] !== T_OPEN_TAG_WITH_ECHO)
62+
&& $tokens[$prevNonEmpty]['code'] !== T_OPEN_TAG_WITH_ECHO
8163
) {
8264
// Still detect `if (foo) {};`.
8365
if ($tokens[$prevNonEmpty]['code'] !== T_CLOSE_CURLY_BRACKET
8466
|| isset($tokens[$prevNonEmpty]['scope_condition']) === false
85-
|| in_array($tokens[$tokens[$prevNonEmpty]['scope_condition']]['code'], $this->conditionTokens) === false
67+
|| in_array($tokens[$tokens[$prevNonEmpty]['scope_condition']]['code'], [T_CLOSURE, T_ANON_CLASS]) === true
8668
) {
8769
return;
8870
}

src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,4 @@ while (true) {};
6363
// Do not break closure and anonymous class;
6464
$a = function () {};
6565
$b = new class {};
66+
echo $a{0};

src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.inc.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ while (true) {}
5858
// Do not break closure and anonymous class;
5959
$a = function () {};
6060
$b = new class {};
61+
echo $a{0};

0 commit comments

Comments
 (0)