diff --git a/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php b/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php index 8e6deb51fd..7ae8b20e66 100644 --- a/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php +++ b/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php @@ -98,15 +98,37 @@ public function process(File $phpcsFile, $stackPtr) $error = 'Opening brace should be on a new line'; $fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceOnSameLine'); if ($fix === true) { + $hasTrailingAnnotation = false; + for ($nextLine = ($openingBrace + 1); $nextLine < $phpcsFile->numTokens; $nextLine++) { + if ($tokens[$openingBrace]['line'] !== $tokens[$nextLine]['line']) { + break; + } + + if (isset(Tokens::$phpcsCommentTokens[$tokens[$nextLine]['code']]) === true) { + $hasTrailingAnnotation = true; + } + } + $phpcsFile->fixer->beginChangeset(); $indent = $phpcsFile->findFirstOnLine([], $openingBrace); - if ($tokens[$indent]['code'] === T_WHITESPACE) { - $phpcsFile->fixer->addContentBefore($openingBrace, $tokens[$indent]['content']); + + if ($hasTrailingAnnotation === false || $nextLine === false) { + if ($tokens[$indent]['code'] === T_WHITESPACE) { + $phpcsFile->fixer->addContentBefore($openingBrace, $tokens[$indent]['content']); + } + + $phpcsFile->fixer->addNewlineBefore($openingBrace); + } else { + $phpcsFile->fixer->replaceToken($openingBrace, ''); + $phpcsFile->fixer->addNewlineBefore($nextLine); + $phpcsFile->fixer->addContentBefore($nextLine, '{'); + if ($tokens[$indent]['code'] === T_WHITESPACE) { + $phpcsFile->fixer->addContentBefore($nextLine, $tokens[$indent]['content']); + } } - $phpcsFile->fixer->addNewlineBefore($openingBrace); $phpcsFile->fixer->endChangeset(); - } + }//end if $phpcsFile->recordMetric($stackPtr, "$metricType opening brace placement", 'same line'); } else if ($lineDifference > 1) { @@ -125,7 +147,9 @@ public function process(File $phpcsFile, $stackPtr) } }//end if - $next = $phpcsFile->findNext(T_WHITESPACE, ($openingBrace + 1), null, true); + $ignore = Tokens::$phpcsCommentTokens; + $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. diff --git a/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php b/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php index bdb3ddc7fb..4778999c9d 100644 --- a/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php +++ b/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php @@ -127,7 +127,9 @@ public function process(File $phpcsFile, $stackPtr) $phpcsFile->recordMetric($stackPtr, "$metricType opening brace placement", 'same line'); }//end if - $next = $phpcsFile->findNext(T_WHITESPACE, ($openingBrace + 1), null, true); + $ignore = Tokens::$phpcsCommentTokens; + $ignore[] = T_WHITESPACE; + $next = $phpcsFile->findNext($ignore, ($openingBrace + 1), null, true); if ($tokens[$next]['line'] === $tokens[$openingBrace]['line']) { if ($next === $tokens[$stackPtr]['scope_closer'] || $tokens[$next]['code'] === T_CLOSE_TAG diff --git a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc index c8782d7250..928dd231a8 100644 --- a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc +++ b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc @@ -201,3 +201,23 @@ function myFunction($a, $lot, $of, $params) : array { return null; } + +function myFunction($a, $lot, $of, $params) { // comment + return null; +} + +function myFunction($a, $lot, $of, $params) + : array { // comment + return null; +} + +function myFunction($a, $lot, $of, $params) + : array { // phpcs:ignore Standard.Category.Sniff -- for reasons. + return null; +} + +function myFunction($a, $lot, $of, $params) + : array +{ // phpcs:ignore Standard.Category.Sniff -- for reasons. + return null; +} diff --git a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc.fixed b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc.fixed index 56702751c9..fb5e526b9c 100644 --- a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc.fixed +++ b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc.fixed @@ -213,3 +213,28 @@ function myFunction($a, $lot, $of, $params) { return null; } + +function myFunction($a, $lot, $of, $params) +{ + // comment + return null; +} + +function myFunction($a, $lot, $of, $params) + : array +{ + // comment + return null; +} + +function myFunction($a, $lot, $of, $params) + : array // phpcs:ignore Standard.Category.Sniff -- for reasons. +{ + return null; +} + +function myFunction($a, $lot, $of, $params) + : array +{ // phpcs:ignore Standard.Category.Sniff -- for reasons. + return null; +} diff --git a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.php b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.php index cd9f339c9b..05106f57e7 100644 --- a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.php +++ b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.php @@ -52,6 +52,9 @@ public function getErrorList() 176 => 1, 196 => 1, 201 => 1, + 205 => 2, + 210 => 2, + 215 => 1, ]; }//end getErrorList() diff --git a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc index 05a692a4d7..2e52759422 100644 --- a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc +++ b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc @@ -203,3 +203,8 @@ function myFunction($a, $lot, $of, $params) { return null; } + +function myFunction($a, $lot, $of, $params) + : array { // phpcs:ignore Standard.Category.Sniff -- for reasons. + return null; +} diff --git a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc.fixed b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc.fixed index af51a96f40..7d215b29a8 100644 --- a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc.fixed +++ b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc.fixed @@ -188,7 +188,11 @@ function myFunction($a, $lot, $of, $params) } function myFunction($a, $lot, $of, $params) - : array { - // phpcs:ignore Standard.Category.Sniff -- for reasons. + : array { // phpcs:ignore Standard.Category.Sniff -- for reasons. + return null; +} + +function myFunction($a, $lot, $of, $params) + : array { // phpcs:ignore Standard.Category.Sniff -- for reasons. return null; }