Skip to content

Commit

Permalink
Generic/OpeningFunctionBraceKernighanRitchie: simplify logic to find …
Browse files Browse the repository at this point in the history
…end of the function signature

When finding the end of the function signature, the sniff had an
unnecessary block of code to change the `$end` parameter passed to
`findPrevious()` when handling a closure with a `use` statement.

Since we are looking for the first non-empty token before the opening
curly brace, it is not necessary to change the value of the `$end`
parameter. Actually, we don't even need to limit the search and we can
pass `null` (the default value). The returned first non-empty token will
be the same anyway regardless if `$end` is the closing parenthesis
before `use`, after `use` or `null`.
  • Loading branch information
rodrigoprimo authored and jrfnl committed Nov 27, 2024
1 parent 195346f commit 286cd81
Showing 1 changed file with 1 addition and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down

0 comments on commit 286cd81

Please sign in to comment.