From cde2f068178e2c3cb1de8ad371ac430d2fd9b3fc Mon Sep 17 00:00:00 2001 From: jrfnl Date: Wed, 9 Aug 2023 19:32:42 +0200 Subject: [PATCH] Generic/OpeningFunctionBraceBsdAllman: check spacing before brace for empty functions As things were, when an empty function was detected, the sniff would bow out and not execute the "BraceIndent" check. Fixed now. Includes tests. --- .../OpeningFunctionBraceBsdAllmanSniff.php | 16 +++++++--------- .../OpeningFunctionBraceBsdAllmanUnitTest.inc | 7 +++++++ ...eningFunctionBraceBsdAllmanUnitTest.inc.fixed | 7 +++++++ .../OpeningFunctionBraceBsdAllmanUnitTest.php | 2 ++ 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php b/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php index ff19526a9b..efb8932bfa 100644 --- a/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php +++ b/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php @@ -170,15 +170,13 @@ public function process(File $phpcsFile, $stackPtr) $ignore[] = T_WHITESPACE; $next = $phpcsFile->findNext($ignore, ($openingBrace + 1), null, true); if ($tokens[$next]['line'] === $tokens[$openingBrace]['line']) { - if ($next === $tokens[$stackPtr]['scope_closer']) { - // Ignore empty functions. - return; - } - - $error = 'Opening brace must be the last content on the line'; - $fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace'); - if ($fix === true) { - $phpcsFile->fixer->addNewline($openingBrace); + // Only throw this error when this is not an empty function. + if ($next !== $tokens[$stackPtr]['scope_closer']) { + $error = 'Opening brace must be the last content on the line'; + $fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace'); + if ($fix === true) { + $phpcsFile->fixer->addNewline($openingBrace); + } } } diff --git a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc index 3ae3b1ed0b..146f9cf6dd 100644 --- a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc +++ b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc @@ -261,3 +261,10 @@ class Issue3357WithComment // code here. } } + + function myFunction() + {} + function myFunction() + {} // Too many spaces indent with an empty function. + function myFunction() +{} // Too little spaces indent with an empty function. diff --git a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc.fixed b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc.fixed index f164c4934e..ac4929d7d5 100644 --- a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc.fixed +++ b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc.fixed @@ -278,3 +278,10 @@ class Issue3357WithComment // code here. } } + + function myFunction() + {} + function myFunction() + {} // Too many spaces indent with an empty function. + function myFunction() + {} // Too little spaces indent with an empty function. diff --git a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.php b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.php index 10af4fd55a..c2dc221eff 100644 --- a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.php +++ b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.php @@ -61,6 +61,8 @@ public function getErrorList() 244 => 1, 252 => 1, 260 => 1, + 268 => 1, + 270 => 1, ]; }//end getErrorList()