From 41dee0024d1db83d1c8e5236fb9dfa5e97736aa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20Tisza?= Date: Sun, 8 Oct 2017 05:33:58 +0000 Subject: [PATCH] Allow empty {} on single line in Squiz.WhiteSpace.ScopeClosingBrace.ContentBefore Allows non-awkward single line syntax for empty functions/classes. Fixes one of the cases mentioned in #1580. --- .../Squiz/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php | 6 ++++-- .../Squiz/Tests/WhiteSpace/ScopeClosingBraceUnitTest.inc | 8 ++++++-- .../Tests/WhiteSpace/ScopeClosingBraceUnitTest.inc.fixed | 8 ++++++-- .../Squiz/Tests/WhiteSpace/ScopeClosingBraceUnitTest.php | 6 +++--- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php b/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php index 4c22a59b8b..ea3194373e 100644 --- a/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php +++ b/src/Standards/Squiz/Sniffs/WhiteSpace/ScopeClosingBraceSniff.php @@ -63,9 +63,11 @@ public function process(File $phpcsFile, $stackPtr) $scopeStart = $tokens[$stackPtr]['scope_opener']; $scopeEnd = $tokens[$stackPtr]['scope_closer']; - // Check that the closing brace is on it's own line. + // Check that the closing brace is on it's own line. Empty {} is always allowed. $lastContent = $phpcsFile->findPrevious(array(T_INLINE_HTML, T_WHITESPACE, T_OPEN_TAG), ($scopeEnd - 1), $scopeStart, true); - if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line']) { + if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line'] + && $scopeEnd !== ($scopeStart + 1) + ) { $error = 'Closing brace must be on a line by itself'; $fix = $phpcsFile->addFixableError($error, $scopeEnd, 'ContentBefore'); if ($fix === true) { diff --git a/src/Standards/Squiz/Tests/WhiteSpace/ScopeClosingBraceUnitTest.inc b/src/Standards/Squiz/Tests/WhiteSpace/ScopeClosingBraceUnitTest.inc index de09a82bb2..579aa3a0ce 100644 --- a/src/Standards/Squiz/Tests/WhiteSpace/ScopeClosingBraceUnitTest.inc +++ b/src/Standards/Squiz/Tests/WhiteSpace/ScopeClosingBraceUnitTest.inc @@ -10,9 +10,11 @@ class Test { } - function test2() {} + function test2() {foo();} - private function _test3() + function test3() {} + + private function _test4() { } @@ -92,6 +94,8 @@ switch ($blah) { 1, 13 => 1, - 24 => 1, - 80 => 1, - 102 => 1, + 26 => 1, + 82 => 1, + 106 => 1, ); }//end getErrorList()