Skip to content

Commit

Permalink
Prevent finding a nested arrow function when searching for the start
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig committed Nov 18, 2024
1 parent 2170823 commit 8d6ab7c
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions VariableAnalysis/Lib/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,17 @@ private static function getPreviousArrowFunctionIndex(File $phpcsFile, $stackPtr
if ($token['content'] === 'fn' && self::isArrowFunction($phpcsFile, $index)) {
return $index;
}
// If we find a token that would close an arrow function scope before we
// find a token that would open an arrow function scope, then we've found
// a nested arrow function and we should ignore it, move back before THAT
// arrow function's scope, and continue to search.
$arrowFunctionStartIndex = $phpcsFile->findPrevious([T_FN], $index, $enclosingScopeIndex);
if (is_int($arrowFunctionStartIndex)) {
$openClose = self::getArrowFunctionOpenClose($phpcsFile, $arrowFunctionStartIndex);
if ($openClose && $openClose['scope_closer'] === $index) {
$index = $openClose['scope_opener'];
}
}
}
return null;
}
Expand Down Expand Up @@ -646,6 +657,15 @@ public static function isArrowFunction(File $phpcsFile, $stackPtr)
}

/**
* Find the opening and closing scope positions for an arrow function if the
* given position is the start of the arrow function (the `fn` keyword
* token).
*
* Returns null if the passed token is not an arrow function keyword.
*
* If the token is an arrow function keyword, the scope opener is returned as
* the provided position.
*
* @param File $phpcsFile
* @param int $stackPtr
*
Expand Down

0 comments on commit 8d6ab7c

Please sign in to comment.