Skip to content

Commit db617f1

Browse files
committed
Generic.CodeAnalysis.EmptyPHPStatement now detects empty statements at the start of control structures (ref #2810)
1 parent 19c8127 commit db617f1

File tree

5 files changed

+15
-2
lines changed

5 files changed

+15
-2
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
2828
<notes>
2929
- The T_FN backfill now works more reliably so T_FN tokens only ever represent real arrow functions
3030
-- Thanks to Juliette Reinders Folmer for the patch
31+
- Generic.CodeAnalysis.EmptyPHPStatement now detects empty statements at the start of control structures
3132
- Fixed bug #2810 : PHPCBF fails to fix file with empty statement at start on control structure
3233
- Fixed bug #2848 : PSR12.Files.FileHeader false positive for file with mixed PHP and HTML and no file header
3334
- Fixed bug #2849 : Generic.WhiteSpace.ScopeIndent false positive with arrow function inside array

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,12 @@ public function process(File $phpcsFile, $stackPtr)
6161
&& $tokens[$prevNonEmpty]['code'] !== T_OPEN_TAG
6262
&& $tokens[$prevNonEmpty]['code'] !== T_OPEN_TAG_WITH_ECHO
6363
) {
64-
if ($tokens[$prevNonEmpty]['code'] !== T_CLOSE_CURLY_BRACKET
65-
|| isset($tokens[$prevNonEmpty]['scope_condition']) === false
64+
if (isset($tokens[$prevNonEmpty]['scope_condition']) === false) {
65+
return;
66+
}
67+
68+
if ($tokens[$prevNonEmpty]['scope_opener'] !== $prevNonEmpty
69+
&& $tokens[$prevNonEmpty]['code'] !== T_CLOSE_CURLY_BRACKET
6670
) {
6771
return;
6872
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,7 @@ switch ( $a ) {
7575
$a = function () {};
7676
$b = new class {};
7777
echo $a{0};
78+
79+
if ($foo) {
80+
;
81+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,6 @@ switch ( $a ) {
7070
$a = function () {};
7171
$b = new class {};
7272
echo $a{0};
73+
74+
if ($foo) {
75+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function getWarningList()
5959
63 => 2,
6060
71 => 1,
6161
72 => 1,
62+
80 => 1,
6263
];
6364

6465
}//end getWarningList()

0 commit comments

Comments
 (0)