Skip to content

Commit ce46913

Browse files
committed
Generic/OpeningFunctionBraceKernighanRitchie: 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 "SpaceBeforeBrace" check. Fixed now. Includes tests.
1 parent b0d171b commit ce46913

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,15 @@ public function process(File $phpcsFile, $stackPtr)
130130
$ignore[] = T_WHITESPACE;
131131
$next = $phpcsFile->findNext($ignore, ($openingBrace + 1), null, true);
132132
if ($tokens[$next]['line'] === $tokens[$openingBrace]['line']) {
133-
if ($next === $tokens[$stackPtr]['scope_closer']
134-
|| $tokens[$next]['code'] === T_CLOSE_TAG
133+
// Only throw this error when this is not an empty function.
134+
if ($next !== $tokens[$stackPtr]['scope_closer']
135+
&& $tokens[$next]['code'] !== T_CLOSE_TAG
135136
) {
136-
// Ignore empty functions.
137-
return;
138-
}
139-
140-
$error = 'Opening brace must be the last content on the line';
141-
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace');
142-
if ($fix === true) {
143-
$phpcsFile->fixer->addNewline($openingBrace);
137+
$error = 'Opening brace must be the last content on the line';
138+
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace');
139+
if ($fix === true) {
140+
$phpcsFile->fixer->addNewline($openingBrace);
141+
}
144142
}
145143
}
146144

src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,7 @@ function myFunction($a, $lot, $of, $params)
208208
: array { // phpcs:ignore Standard.Category.Sniff -- for reasons.
209209
return null;
210210
}
211+
212+
function myFunction() {}
213+
function myFunction() {} // Too many spaces with an empty function.
214+
function myFunction() {} // Too many spaces (tab) with an empty function.

src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,7 @@ function myFunction($a, $lot, $of, $params)
196196
: array { // phpcs:ignore Standard.Category.Sniff -- for reasons.
197197
return null;
198198
}
199+
200+
function myFunction() {}
201+
function myFunction() {} // Too many spaces with an empty function.
202+
function myFunction() {} // Too many spaces (tab) with an empty function.

src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public function getErrorList()
5252
191 => 1,
5353
197 => 1,
5454
203 => 1,
55+
213 => 1,
56+
214 => 1,
5557
];
5658

5759
}//end getErrorList()

0 commit comments

Comments
 (0)