From db617f16fd942d51a8bc70af8786deecb8b8c896 Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Wed, 19 Feb 2020 10:11:28 +1100 Subject: [PATCH] Generic.CodeAnalysis.EmptyPHPStatement now detects empty statements at the start of control structures (ref #2810) --- package.xml | 1 + .../Sniffs/CodeAnalysis/EmptyPHPStatementSniff.php | 8 ++++++-- .../Tests/CodeAnalysis/EmptyPHPStatementUnitTest.inc | 4 ++++ .../CodeAnalysis/EmptyPHPStatementUnitTest.inc.fixed | 3 +++ .../Tests/CodeAnalysis/EmptyPHPStatementUnitTest.php | 1 + 5 files changed, 15 insertions(+), 2 deletions(-) diff --git a/package.xml b/package.xml index f26d0012ab..bda0f37f75 100644 --- a/package.xml +++ b/package.xml @@ -28,6 +28,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> - The T_FN backfill now works more reliably so T_FN tokens only ever represent real arrow functions -- Thanks to Juliette Reinders Folmer for the patch + - Generic.CodeAnalysis.EmptyPHPStatement now detects empty statements at the start of control structures - Fixed bug #2810 : PHPCBF fails to fix file with empty statement at start on control structure - Fixed bug #2848 : PSR12.Files.FileHeader false positive for file with mixed PHP and HTML and no file header - Fixed bug #2849 : Generic.WhiteSpace.ScopeIndent false positive with arrow function inside array diff --git a/src/Standards/Generic/Sniffs/CodeAnalysis/EmptyPHPStatementSniff.php b/src/Standards/Generic/Sniffs/CodeAnalysis/EmptyPHPStatementSniff.php index 275d4744d8..a37d6cc68f 100644 --- a/src/Standards/Generic/Sniffs/CodeAnalysis/EmptyPHPStatementSniff.php +++ b/src/Standards/Generic/Sniffs/CodeAnalysis/EmptyPHPStatementSniff.php @@ -61,8 +61,12 @@ public function process(File $phpcsFile, $stackPtr) && $tokens[$prevNonEmpty]['code'] !== T_OPEN_TAG && $tokens[$prevNonEmpty]['code'] !== T_OPEN_TAG_WITH_ECHO ) { - if ($tokens[$prevNonEmpty]['code'] !== T_CLOSE_CURLY_BRACKET - || isset($tokens[$prevNonEmpty]['scope_condition']) === false + if (isset($tokens[$prevNonEmpty]['scope_condition']) === false) { + return; + } + + if ($tokens[$prevNonEmpty]['scope_opener'] !== $prevNonEmpty + && $tokens[$prevNonEmpty]['code'] !== T_CLOSE_CURLY_BRACKET ) { return; } diff --git a/src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.inc b/src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.inc index 83d5c4524a..e627fd3edf 100644 --- a/src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.inc +++ b/src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.inc @@ -75,3 +75,7 @@ switch ( $a ) { $a = function () {}; $b = new class {}; echo $a{0}; + +if ($foo) { +; +} diff --git a/src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.inc.fixed b/src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.inc.fixed index 3d866f2136..2773f97b85 100644 --- a/src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.inc.fixed +++ b/src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.inc.fixed @@ -70,3 +70,6 @@ switch ( $a ) { $a = function () {}; $b = new class {}; echo $a{0}; + +if ($foo) { +} diff --git a/src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.php b/src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.php index c1b7c8ee4b..23b3ec8fba 100644 --- a/src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.php +++ b/src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.php @@ -59,6 +59,7 @@ public function getWarningList() 63 => 2, 71 => 1, 72 => 1, + 80 => 1, ]; }//end getWarningList()