Skip to content

Commit fc01fa6

Browse files
committed
Replace scope_opener/closer with getArrowFunctionOpenClose
1 parent d072fa7 commit fc01fa6

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

VariableAnalysis/Lib/Helpers.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,14 +467,16 @@ public static function isTokenInsideArrowFunctionBody(File $phpcsFile, $stackPtr
467467
* @return ?int
468468
*/
469469
public static function getContainingArrowFunctionIndex(File $phpcsFile, $stackPtr) {
470-
$tokens = $phpcsFile->getTokens();
471470
$arrowFunctionIndex = self::getPreviousArrowFunctionIndex($phpcsFile, $stackPtr);
472471
if (! is_int($arrowFunctionIndex)) {
473472
return null;
474473
}
475-
$arrowFunctionToken = $tokens[$arrowFunctionIndex];
476-
$arrowFunctionScopeStart = $arrowFunctionToken['scope_opener'];
477-
$arrowFunctionScopeEnd = $arrowFunctionToken['scope_closer'];
474+
$arrowFunctionInfo = FunctionDeclarations::getArrowFunctionOpenClose($phpcsFile, $arrowFunctionIndex);
475+
if (! $arrowFunctionInfo) {
476+
return null;
477+
}
478+
$arrowFunctionScopeStart = $arrowFunctionInfo['scope_opener'];
479+
$arrowFunctionScopeEnd = $arrowFunctionInfo['scope_closer'];
478480
if ($stackPtr > $arrowFunctionScopeStart && $stackPtr < $arrowFunctionScopeEnd) {
479481
return $arrowFunctionIndex;
480482
}

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,12 @@ public function process(File $phpcsFile, $stackPtr) {
171171
T_CLOSURE,
172172
];
173173

174-
$scopeIndexThisCloses = array_reduce($this->scopeStartIndices, function ($found, $index) use ($stackPtr, $tokens) {
174+
$scopeIndexThisCloses = array_reduce($this->scopeStartIndices, function ($found, $index) use ($phpcsFile, $stackPtr, $tokens) {
175175
$scopeCloserIndex = isset($tokens[$index]['scope_closer']) ? $tokens[$index]['scope_closer'] : null;
176+
if (FunctionDeclarations::isArrowFunction($phpcsFile, $index)) {
177+
$arrowFunctionInfo = FunctionDeclarations::getArrowFunctionOpenClose($phpcsFile, $index);
178+
$scopeCloserIndex = $arrowFunctionInfo ? $arrowFunctionInfo['scope_closer'] : $scopeCloserIndex;
179+
}
176180
if (!$scopeCloserIndex) {
177181
Helpers::debug('No scope closer found for scope start', $index);
178182
}

0 commit comments

Comments
 (0)