Skip to content

Commit

Permalink
Generic/OpeningFunctionBraceBsdAllman: check spacing before brace for…
Browse files Browse the repository at this point in the history
… empty functions

As things were, when an empty function was detected, the sniff would bow out and not execute the "BraceIndent" check.

Fixed now.

Includes tests.
  • Loading branch information
jrfnl committed Aug 9, 2023
1 parent fe3fbe5 commit cde2f06
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,13 @@ public function process(File $phpcsFile, $stackPtr)
$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.
return;
}

$error = 'Opening brace must be the last content on the line';
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace');
if ($fix === true) {
$phpcsFile->fixer->addNewline($openingBrace);
// Only throw this error when this is not an empty function.
if ($next !== $tokens[$stackPtr]['scope_closer']) {
$error = 'Opening brace must be the last content on the line';
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace');
if ($fix === true) {
$phpcsFile->fixer->addNewline($openingBrace);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,10 @@ class Issue3357WithComment
// code here.
}
}

function myFunction()
{}
function myFunction()
{} // Too many spaces indent with an empty function.
function myFunction()
{} // Too little spaces indent with an empty function.
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,10 @@ class Issue3357WithComment
// code here.
}
}

function myFunction()
{}
function myFunction()
{} // Too many spaces indent with an empty function.
function myFunction()
{} // Too little spaces indent with an empty function.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public function getErrorList()
244 => 1,
252 => 1,
260 => 1,
268 => 1,
270 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit cde2f06

Please sign in to comment.