From 054762663a9204f8a703cfd5acd3a32e72b74e41 Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Wed, 19 Feb 2020 09:34:35 +1100 Subject: [PATCH] Fixed bug #2810 : PHPCBF fails to fix file with empty statement at start on control structure --- package.xml | 1 + .../Squiz/Sniffs/WhiteSpace/SemicolonSpacingSniff.php | 3 ++- .../Squiz/Tests/WhiteSpace/SemicolonSpacingUnitTest.inc | 5 +++++ .../Tests/WhiteSpace/SemicolonSpacingUnitTest.inc.fixed | 5 +++++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/package.xml b/package.xml index 57b9bee7cc..f26d0012ab 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 + - 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 - Fixed bug #2850 : Generic.PHP.LowerCaseKeyword complains __HALT_COMPILER is uppercase diff --git a/src/Standards/Squiz/Sniffs/WhiteSpace/SemicolonSpacingSniff.php b/src/Standards/Squiz/Sniffs/WhiteSpace/SemicolonSpacingSniff.php index aee88cea4d..3fa0b715af 100644 --- a/src/Standards/Squiz/Sniffs/WhiteSpace/SemicolonSpacingSniff.php +++ b/src/Standards/Squiz/Sniffs/WhiteSpace/SemicolonSpacingSniff.php @@ -59,7 +59,7 @@ public function process(File $phpcsFile, $stackPtr) $nonSpace = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 2), null, true); - // Detect whether this is a semi-colons for a conditions in a `for()` control structure. + // Detect whether this is a semi-colon for a condition in a `for()` control structure. $forCondition = false; if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { $nestedParens = $tokens[$stackPtr]['nested_parenthesis']; @@ -77,6 +77,7 @@ public function process(File $phpcsFile, $stackPtr) if ($tokens[$nonSpace]['code'] === T_SEMICOLON || ($forCondition === true && $nonSpace === $tokens[$owner]['parenthesis_opener']) + || (isset($tokens[$nonSpace]['scope_opener']) === true) ) { // Empty statement. return; diff --git a/src/Standards/Squiz/Tests/WhiteSpace/SemicolonSpacingUnitTest.inc b/src/Standards/Squiz/Tests/WhiteSpace/SemicolonSpacingUnitTest.inc index 725d422943..0d65c376de 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/SemicolonSpacingUnitTest.inc +++ b/src/Standards/Squiz/Tests/WhiteSpace/SemicolonSpacingUnitTest.inc @@ -28,3 +28,8 @@ for ( ; ; ) {} // But it should when the semi-colon in a `for` follows a comment (but shouldn't move the semi-colon). for ( /* Deliberately left empty. */ ; $ptr >= 0; $ptr-- ) {} for ( $i = 1 ; /* Deliberately left empty. */ ; $i++ ) {} + +// This is an empty statement and should be ignored. +if ($foo) { +; +} diff --git a/src/Standards/Squiz/Tests/WhiteSpace/SemicolonSpacingUnitTest.inc.fixed b/src/Standards/Squiz/Tests/WhiteSpace/SemicolonSpacingUnitTest.inc.fixed index e8c0099b3f..22aaa9f0ef 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/SemicolonSpacingUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/WhiteSpace/SemicolonSpacingUnitTest.inc.fixed @@ -28,3 +28,8 @@ for ( ; ; ) {} // But it should when the semi-colon in a `for` follows a comment (but shouldn't move the semi-colon). for ( /* Deliberately left empty. */; $ptr >= 0; $ptr-- ) {} for ( $i = 1; /* Deliberately left empty. */; $i++ ) {} + +// This is an empty statement and should be ignored. +if ($foo) { +; +}