Skip to content

Commit 0c1020b

Browse files
committed
Prevent finding a nested arrow function when searching for the start
1 parent 2170823 commit 0c1020b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

VariableAnalysis/Lib/Helpers.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,18 @@ private static function getPreviousArrowFunctionIndex(File $phpcsFile, $stackPtr
608608
if ($token['content'] === 'fn' && self::isArrowFunction($phpcsFile, $index)) {
609609
return $index;
610610
}
611+
// If we find a token that would close an arrow function scope before we
612+
// find a token that would open an arrow function scope, then we've found
613+
// a nested arrow function and we should ignore it, move back before THAT
614+
// arrow function's scope, and continue to search.
615+
$arrowFunctionStartIndex = $phpcsFile->findPrevious([T_FN], $index, $enclosingScopeIndex);
616+
if (is_int($arrowFunctionStartIndex)) {
617+
$openClose = self::getArrowFunctionOpenClose($phpcsFile, $arrowFunctionStartIndex);
618+
if ($openClose && $openClose['scope_closer'] === $index) {
619+
$index = $openClose['scope_opener'];
620+
}
621+
}
622+
611623
}
612624
return null;
613625
}
@@ -646,6 +658,15 @@ public static function isArrowFunction(File $phpcsFile, $stackPtr)
646658
}
647659

648660
/**
661+
* Find the opening and closing scope positions for an arrow function if the
662+
* given position is the start of the arrow function (the `fn` keyword
663+
* token).
664+
*
665+
* Returns null if the passed token is not an arrow function keyword.
666+
*
667+
* If the token is an arrow function keyword, the scope opener is returned as
668+
* the provided position.
669+
*
649670
* @param File $phpcsFile
650671
* @param int $stackPtr
651672
*

0 commit comments

Comments
 (0)