diff --git a/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php b/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php index ca259854e7..1e2df37d86 100644 --- a/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php +++ b/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php @@ -72,17 +72,9 @@ public function process(File $phpcsFile, $stackPtr) } $openingBrace = $tokens[$stackPtr]['scope_opener']; - $closeBracket = $tokens[$stackPtr]['parenthesis_closer']; - if ($tokens[$stackPtr]['code'] === T_CLOSURE) { - $use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']); - if ($use !== false) { - $openBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1)); - $closeBracket = $tokens[$openBracket]['parenthesis_closer']; - } - } // Find the end of the function declaration. - $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($openingBrace - 1), $closeBracket, true); + $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($openingBrace - 1), null, true); $functionLine = $tokens[$prev]['line']; $braceLine = $tokens[$openingBrace]['line']; @@ -99,7 +91,6 @@ public function process(File $phpcsFile, $stackPtr) $error = 'Opening brace should be on the same line as the declaration'; $fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceOnNewLine'); if ($fix === true) { - $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($openingBrace - 1), $closeBracket, true); $phpcsFile->fixer->beginChangeset(); $phpcsFile->fixer->addContent($prev, ' {'); $phpcsFile->fixer->replaceToken($openingBrace, ''); @@ -168,7 +159,7 @@ public function process(File $phpcsFile, $stackPtr) $data = [$length]; $fix = $phpcsFile->addFixableError($error, $openingBrace, 'SpaceBeforeBrace', $data); if ($fix === true) { - if ($length === 0 || $length === '\t') { + if ($length === 0) { $phpcsFile->fixer->addContentBefore($openingBrace, ' '); } else { $phpcsFile->fixer->replaceToken(($openingBrace - 1), ' '); diff --git a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.1.inc similarity index 88% rename from src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc rename to src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.1.inc index 6c937a823b..2e76d62155 100644 --- a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc +++ b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.1.inc @@ -23,16 +23,16 @@ class myClass // Good. function myFunction() { } - + // Brace should be on same line. function myFunction() { } - + // Too many spaces. function myFunction() { } - + // Uses tab. function myFunction() { } @@ -70,18 +70,18 @@ class myClass function myFunction($variable1, $variable2, $variable3, $variable4) { } - + // Brace should be on same line. function myFunction($variable1, $variable2, $variable3, $variable4) { } - + // Too many spaces. function myFunction($variable1, $variable2, $variable3, $variable4) { } - + // Uses tab. function myFunction($variable1, $variable2, $variable3, $variable4) { @@ -212,3 +212,21 @@ function myFunction($a, $lot, $of, $params) function myFunction() {} function myFunction() {} // Too many spaces with an empty function. function myFunction() {} // Too many spaces (tab) with an empty function. + +// phpcs:set Generic.Functions.OpeningFunctionBraceKernighanRitchie checkFunctions 0 +function shouldBeIgnored() +{} +// phpcs:set Generic.Functions.OpeningFunctionBraceKernighanRitchie checkFunctions 1 + +function dnfReturnType(): (Response&SuccessResponse)|AnotherResponse|string +{} + +function commentAfterOpeningBrace() { // Some comment. +} + +function variableAssignmentAfterOpeningBrace() { $a = 1; +} + +abstract class MyClass { + abstract public function abstractMethod(); +} diff --git a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc.fixed b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.1.inc.fixed similarity index 88% rename from src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc.fixed rename to src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.1.inc.fixed index bfb2283844..14e62eae90 100644 --- a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc.fixed +++ b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.1.inc.fixed @@ -22,15 +22,15 @@ class myClass // Good. function myFunction() { } - + // Brace should be on same line. function myFunction() { } - + // Too many spaces. function myFunction() { } - + // Uses tab. function myFunction() { } @@ -67,17 +67,17 @@ class myClass function myFunction($variable1, $variable2, $variable3, $variable4) { } - + // Brace should be on same line. function myFunction($variable1, $variable2, $variable3, $variable4) { } - + // Too many spaces. function myFunction($variable1, $variable2, $variable3, $variable4) { } - + // Uses tab. function myFunction($variable1, $variable2, $variable3, $variable4) { @@ -200,3 +200,23 @@ function myFunction($a, $lot, $of, $params) function myFunction() {} function myFunction() {} // Too many spaces with an empty function. function myFunction() {} // Too many spaces (tab) with an empty function. + +// phpcs:set Generic.Functions.OpeningFunctionBraceKernighanRitchie checkFunctions 0 +function shouldBeIgnored() +{} +// phpcs:set Generic.Functions.OpeningFunctionBraceKernighanRitchie checkFunctions 1 + +function dnfReturnType(): (Response&SuccessResponse)|AnotherResponse|string { +} + +function commentAfterOpeningBrace() { + // Some comment. +} + +function variableAssignmentAfterOpeningBrace() { + $a = 1; +} + +abstract class MyClass { + abstract public function abstractMethod(); +} diff --git a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.2.inc b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.2.inc new file mode 100644 index 0000000000..a145bb9efe --- /dev/null +++ b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.2.inc @@ -0,0 +1,11 @@ +tabWidth = 4; + } + + }//end setCliValues() + + /** * Returns the lines where errors should occur. * * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * + * @param string $testFile The name of the test file to process. + * * @return array */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 9 => 1, - 13 => 1, - 17 => 1, - 29 => 1, - 33 => 1, - 37 => 1, - 53 => 1, - 58 => 1, - 63 => 1, - 77 => 1, - 82 => 1, - 87 => 1, - 104 => 1, - 119 => 1, - 123 => 1, - 127 => 1, - 132 => 1, - 137 => 1, - 142 => 1, - 157 => 1, - 162 => 1, - 171 => 1, - 181 => 1, - 191 => 1, - 197 => 1, - 203 => 1, - 213 => 1, - 214 => 1, - ]; + switch ($testFile) { + case 'OpeningFunctionBraceKernighanRitchieUnitTest.1.inc': + return [ + 9 => 1, + 13 => 1, + 17 => 1, + 29 => 1, + 33 => 1, + 37 => 1, + 53 => 1, + 58 => 1, + 63 => 1, + 77 => 1, + 82 => 1, + 87 => 1, + 104 => 1, + 119 => 1, + 123 => 1, + 127 => 1, + 132 => 1, + 137 => 1, + 142 => 1, + 157 => 1, + 162 => 1, + 171 => 1, + 181 => 1, + 191 => 1, + 197 => 1, + 203 => 1, + 213 => 1, + 214 => 1, + 222 => 1, + 224 => 1, + 227 => 1, + ]; + case 'OpeningFunctionBraceKernighanRitchieUnitTest.2.inc': + return [ + 6 => 1, + 10 => 1, + ]; + default: + return []; + }//end switch }//end getErrorList()