Skip to content

Commit 24a8a67

Browse files
committed
Only use T_FN if it exists
1 parent 477fe51 commit 24a8a67

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

VariableAnalysis/Lib/Helpers.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,10 @@ public static function getFunctionIndexForFunctionArgument(File $phpcsFile, $sta
149149
$functionTokenTypes = [
150150
T_FUNCTION,
151151
T_CLOSURE,
152-
T_FN, // TODO: cannot use this before PHP 7.4
153152
];
153+
if (defined('T_FN')) {
154+
$functionTokenTypes[] = T_FN;
155+
}
154156
if (!in_array($tokens[$functionPtr]['code'], $functionTokenTypes, true)) {
155157
return null;
156158
}
@@ -436,8 +438,10 @@ private static function getStartOfTokenScope(File $phpcsFile, $stackPtr) {
436438
$functionTokenTypes = [
437439
T_FUNCTION,
438440
T_CLOSURE,
439-
T_FN, // TODO: cannot use this before PHP 7.4
440441
];
442+
if (defined('T_FN')) {
443+
$functionTokenTypes[] = T_FN;
444+
}
441445
foreach (array_reverse($conditions, true) as $scopePtr => $scopeCode) {
442446
if (in_array($scopeCode, $functionTokenTypes, true)) {
443447
return $scopePtr;
@@ -494,9 +498,12 @@ public static function isTokenInsideArrowFunctionBody(File $phpcsFile, $stackPtr
494498
* @return ?int
495499
*/
496500
public static function getContainingArrowFunctionIndex(File $phpcsFile, $stackPtr) {
501+
if (! defined('T_FN')) {
502+
return null;
503+
}
497504
$tokens = $phpcsFile->getTokens();
498505
$enclosingScopeIndex = self::findVariableScopeExceptArrowFunctions($phpcsFile, $stackPtr);
499-
$arrowFunctionIndex = $phpcsFile->findPrevious([T_FN], $stackPtr - 1, $enclosingScopeIndex); // TODO: cannot use T_FN before PHP 7.4
506+
$arrowFunctionIndex = $phpcsFile->findPrevious([T_FN], $stackPtr - 1, $enclosingScopeIndex);
500507
if (! is_int($arrowFunctionIndex)) {
501508
return null;
502509
}

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class VariableAnalysisSniff implements Sniff {
118118
* @return (int|string)[]
119119
*/
120120
public function register() {
121-
return [
121+
$types = [
122122
T_VARIABLE,
123123
T_DOUBLE_QUOTED_STRING,
124124
T_HEREDOC,
@@ -129,8 +129,11 @@ public function register() {
129129
T_COMMA,
130130
T_SEMICOLON,
131131
T_CLOSE_PARENTHESIS,
132-
T_FN, // TODO: we can't use this before php 7.4 so we need to replace it somehow
133132
];
133+
if (defined('T_FN')) {
134+
$types[] = T_FN;
135+
}
136+
return $types;
134137
}
135138

136139
/**
@@ -165,8 +168,10 @@ public function process(File $phpcsFile, $stackPtr) {
165168
$scopeStartTokenTypes = [
166169
T_FUNCTION,
167170
T_CLOSURE,
168-
T_FN, // TODO: we cannot use this before PHP 7.4
169171
];
172+
if (defined('T_FN')) {
173+
$scopeStartTokenTypes[] = T_FN;
174+
}
170175

171176
$scopeIndexThisCloses = array_reduce($this->scopeStartIndices, function ($found, $index) use ($stackPtr, $tokens) {
172177
if ($stackPtr === $tokens[$index]['scope_closer']) {
@@ -279,7 +284,6 @@ protected function getVariableInfo($varName, $currScope) {
279284
* @return VariableInfo
280285
*/
281286
protected function getOrCreateVariableInfo($varName, $currScope) {
282-
// TODO: this needs to find the scope of an arrow function if we are inside one, which must also include the scope of its parent!
283287
$scopeInfo = $this->getOrCreateScopeInfo($currScope);
284288
if (!isset($scopeInfo->variables[$varName])) {
285289
Helpers::debug("creating a new variable for '{$varName}' in scope", $scopeInfo);
@@ -460,7 +464,6 @@ protected function isVariableUndefined($varName, $stackPtr, $currScope) {
460464
if (isset($varInfo->firstInitialized) && $varInfo->firstInitialized <= $stackPtr) {
461465
return false;
462466
}
463-
// TODO: if the variable is in an arrow function body, also check the enclosing scope
464467
return true;
465468
}
466469

0 commit comments

Comments
 (0)