Skip to content

Commit

Permalink
Generic/ConstructorName: minor efficiency tweak
Browse files Browse the repository at this point in the history
As things were, the `$startIndex` would be the `function` keyword in the function declaration, which means that the complete declaration (parameters, defaults etc) would be walked, while we only really want to look _inside_ the function body.

Fixed by starting the `while` loop on the `scope_opener` instead.

Additionally, while the `$startIndex` would be moved forward on each loop, it would still examine one token too much (`scope_opener` cannot be a `T_DOUBLE_COLON`, `T_STRING` after `T_DOUBLE_COLON` cannot be a `T_DOUBLE_COLON`).
Also fixed now.
  • Loading branch information
jrfnl committed Oct 30, 2024
1 parent a3a67d8 commit 7b98484
Showing 1 changed file with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
}

// Stop if the constructor doesn't have a body, like when it is abstract.
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
if (isset($tokens[$stackPtr]['scope_opener'], $tokens[$stackPtr]['scope_closer']) === false) {
return;
}

Expand All @@ -103,8 +103,8 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
$parentClassNameLc = strtolower($parentClassName);

$endFunctionIndex = $tokens[$stackPtr]['scope_closer'];
$startIndex = $stackPtr;
while (($doubleColonIndex = $phpcsFile->findNext(T_DOUBLE_COLON, $startIndex, $endFunctionIndex)) !== false) {
$startIndex = $tokens[$stackPtr]['scope_opener'];
while (($doubleColonIndex = $phpcsFile->findNext(T_DOUBLE_COLON, ($startIndex + 1), $endFunctionIndex)) !== false) {
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($doubleColonIndex + 1), null, true);
if ($tokens[$nextNonEmpty]['code'] !== T_STRING
|| strtolower($tokens[$nextNonEmpty]['content']) !== $parentClassNameLc
Expand Down

0 comments on commit 7b98484

Please sign in to comment.