Skip to content

Commit e079598

Browse files
committed
Make findPreviousFunctionPtr check token type
1 parent 12507d6 commit e079598

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

VariableAnalysis/Lib/Helpers.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ public static function areConditionsWithinFunctionBeforeClass(array $conditions)
112112
}
113113

114114
/**
115+
* Find the token index of the function keyword for an open brace
116+
*
117+
* Given the token index of an open curly brace, return the index of the
118+
* function keyword for that brace, or null if the brace is not for a
119+
* function.
120+
*
115121
* @param File $phpcsFile
116122
* @param int $openPtr
117123
*
@@ -122,11 +128,24 @@ public static function findPreviousFunctionPtr(File $phpcsFile, $openPtr) {
122128
// so we look backwards from the opening bracket for the first thing that
123129
// isn't a function name, reference sigil or whitespace and check if it's a
124130
// function keyword.
125-
$functionPtrTypes = Tokens::$emptyTokens;
126-
$functionPtrTypes[T_STRING] = T_STRING;
127-
$functionPtrTypes[T_BITWISE_AND] = T_BITWISE_AND;
131+
$nonFunctionTokenTypes = Tokens::$emptyTokens;
132+
$nonFunctionTokenTypes[T_STRING] = T_STRING;
133+
$nonFunctionTokenTypes[T_BITWISE_AND] = T_BITWISE_AND;
134+
135+
$functionPtr = self::getIntOrNull($phpcsFile->findPrevious($nonFunctionTokenTypes, $openPtr - 1, null, true, null, true));
136+
if (! is_int($functionPtr)) {
137+
return null;
138+
}
128139

129-
return self::getIntOrNull($phpcsFile->findPrevious($functionPtrTypes, $openPtr - 1, null, true, null, true));
140+
$functionTokenTypes = [
141+
T_FUNCTION,
142+
T_CLOSURE,
143+
];
144+
$tokens = $phpcsFile->getTokens();
145+
if (in_array($tokens[$functionPtr]['code'], $functionTokenTypes, true)) {
146+
return $functionPtr;
147+
}
148+
return null;
130149
}
131150

132151
/**
@@ -275,11 +294,7 @@ public static function findFunctionDefinition(File $phpcsFile, $stackPtr) {
275294
if (! is_int($openPtr)) {
276295
return null;
277296
}
278-
$functionPtr = Helpers::findPreviousFunctionPtr($phpcsFile, $openPtr);
279-
if (($functionPtr !== null) && ($tokens[$functionPtr]['code'] === T_FUNCTION)) {
280-
return $functionPtr;
281-
}
282-
return null;
297+
return Helpers::findPreviousFunctionPtr($phpcsFile, $openPtr);
283298
}
284299

285300
/**

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -491,14 +491,7 @@ protected function isVariableAFunctionDefinition(File $phpcsFile, $stackPtr, $va
491491
}
492492

493493
$functionPtr = Helpers::findPreviousFunctionPtr($phpcsFile, $openPtr);
494-
if (// phpcs:ignore PSR2.ControlStructures.ControlStructureSpacing.SpacingAfterOpenBrace
495-
(is_int($functionPtr))
496-
&& (
497-
($tokens[$functionPtr]['code'] === T_FUNCTION)
498-
|| (FunctionDeclarations::isArrowFunction($phpcsFile, $functionPtr))
499-
|| ($tokens[$functionPtr]['code'] === T_CLOSURE)
500-
)
501-
) {
494+
if (is_int($functionPtr)) {
502495
$this->markVariableDeclaration($varName, ScopeType::PARAM, null, $stackPtr, $functionPtr);
503496
// Are we pass-by-reference?
504497
$referencePtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true, null, true);

0 commit comments

Comments
 (0)