diff --git a/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php b/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php index ff19526a9b..84725ccd37 100644 --- a/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php +++ b/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php @@ -73,6 +73,7 @@ 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) { @@ -95,6 +96,18 @@ public function process(File $phpcsFile, $stackPtr) } if ($lineDifference === 0) { + // Ignore constructors with empty function body + if ($tokens[$stackPtr]['code'] === T_FUNCTION + && $phpcsFile->getDeclarationName($stackPtr) === '__construct' + ) { + $closingBrace = $tokens[$stackPtr]['scope_closer']; + $lastContent = $phpcsFile->findPrevious([T_WHITESPACE], ($closingBrace - 1), $openingBrace, true); + + if ($lastContent === $openingBrace) { + return; + } + } + $error = 'Opening brace should be on a new line'; $fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceOnSameLine'); if ($fix === true) { diff --git a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc index 3ae3b1ed0b..f8261e73f4 100644 --- a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc +++ b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc @@ -26,6 +26,9 @@ function myFunction() class myClass { + // Good. + function __construct() {} + // Brace should be on new line. function myFunction() { } diff --git a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc.fixed b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc.fixed index f164c4934e..97d62f0def 100644 --- a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc.fixed +++ b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.inc.fixed @@ -27,6 +27,9 @@ function myFunction() class myClass { + // Good. + function __construct() {} + // Brace should be on new line. function myFunction() { diff --git a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.php b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.php index 10af4fd55a..85fe1e3e20 100644 --- a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.php +++ b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceBsdAllmanUnitTest.php @@ -26,41 +26,41 @@ class OpeningFunctionBraceBsdAllmanUnitTest extends AbstractSniffUnitTest public function getErrorList() { return [ - 4 => 1, - 13 => 1, - 19 => 1, - 24 => 1, - 30 => 1, - 40 => 1, - 44 => 1, - 50 => 1, - 55 => 1, - 67 => 1, - 78 => 1, - 85 => 1, - 91 => 1, - 98 => 1, - 110 => 1, - 115 => 1, - 122 => 1, - 128 => 1, - 155 => 1, + 7 => 1, + 16 => 1, + 22 => 1, + 27 => 1, + 33 => 1, + 43 => 1, + 47 => 1, + 53 => 1, + 58 => 1, + 70 => 1, + 81 => 1, + 88 => 1, + 94 => 1, + 101 => 1, + 113 => 1, + 118 => 1, + 125 => 1, + 131 => 1, 158 => 1, - 164 => 1, - 168 => 1, - 172 => 1, - 176 => 1, - 196 => 1, - 201 => 1, - 205 => 2, - 210 => 2, - 215 => 1, - 220 => 1, - 231 => 1, - 236 => 1, - 244 => 1, - 252 => 1, - 260 => 1, + 161 => 1, + 167 => 1, + 171 => 1, + 175 => 1, + 179 => 1, + 199 => 1, + 204 => 1, + 208 => 2, + 213 => 2, + 218 => 1, + 223 => 1, + 234 => 1, + 239 => 1, + 247 => 1, + 255 => 1, + 263 => 1, ]; }//end getErrorList() diff --git a/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php b/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php index fd03875347..bba8201577 100644 --- a/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php +++ b/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php @@ -67,6 +67,24 @@ public function process(File $phpcsFile, $stackPtr) $lastContent = $phpcsFile->findPrevious([T_INLINE_HTML, T_WHITESPACE, T_OPEN_TAG], ($scopeEnd - 1), $scopeStart, true); for ($lineStart = $scopeEnd; $tokens[$lineStart]['column'] > 1; $lineStart--); + // Allow opening and closing brackets with empty body on the same line for constructors + if ($tokens[$stackPtr]['code'] === T_FUNCTION + && $phpcsFile->getDeclarationName($stackPtr) === '__construct' + && $lastContent === $scopeStart + ) { + $whitespaceBetween = $phpcsFile->findPrevious([T_WHITESPACE], ($scopeEnd - 1), $scopeStart); + + if ($whitespaceBetween) { + $error = 'Expected no space between opening and closing brace'; + $fix = $phpcsFile->addFixableError($error, $scopeEnd, 'ContentBefore'); + if ($fix === true) { + $phpcsFile->fixer->replaceToken($whitespaceBetween, ''); + } + } + + return; + } + if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line'] || ($tokens[$lineStart]['code'] === T_INLINE_HTML && trim($tokens[$lineStart]['content']) !== '') diff --git a/src/Standards/Squiz/Tests/WhiteSpace/ScopeClosingBraceUnitTest.inc.fixed b/src/Standards/Squiz/Tests/WhiteSpace/ScopeClosingBraceUnitTest.inc.fixed index 7f08d0ffd1..0262e61d47 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/ScopeClosingBraceUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/WhiteSpace/ScopeClosingBraceUnitTest.inc.fixed @@ -3,8 +3,7 @@ use some\foo\{ClassA, ClassB, ClassC as C}; class Test { public function __construct() - { - } + {} function test1() { diff --git a/src/Standards/Squiz/Tests/WhiteSpace/ScopeClosingBraceUnitTest.php b/src/Standards/Squiz/Tests/WhiteSpace/ScopeClosingBraceUnitTest.php index d659d64731..45273df1e6 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/ScopeClosingBraceUnitTest.php +++ b/src/Standards/Squiz/Tests/WhiteSpace/ScopeClosingBraceUnitTest.php @@ -26,6 +26,7 @@ class ScopeClosingBraceUnitTest extends AbstractSniffUnitTest public function getErrorList() { return [ + 7 => 1, 11 => 1, 13 => 1, 24 => 1,