Skip to content

Commit

Permalink
Only use HTML/close PHP tag tokens as empty when necessary (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig authored Jun 18, 2021
1 parent 22c83ec commit 9df9ade
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions VariableAnalysis/Lib/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Helpers {
/**
* return int[]
*/
public static function getEmptyTokens() {
public static function getPossibleEndOfFileTokens() {
return array_merge(
array_values(Tokens::$emptyTokens),
[
Expand Down Expand Up @@ -178,7 +178,7 @@ public static function getFunctionIndexForFunctionArgument(File $phpcsFile, $sta
return null;
}

$nonFunctionTokenTypes = self::getEmptyTokens();
$nonFunctionTokenTypes = Tokens::$emptyTokens;
$nonFunctionTokenTypes[] = T_STRING;
$nonFunctionTokenTypes[] = T_BITWISE_AND;
$functionPtr = self::getIntOrNull($phpcsFile->findPrevious($nonFunctionTokenTypes, $startOfArguments - 1, null, true, null, true));
Expand Down Expand Up @@ -218,7 +218,7 @@ public static function isTokenInsideFunctionUseImport(File $phpcsFile, $stackPtr
public static function getUseIndexForUseImport(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();

$nonUseTokenTypes = self::getEmptyTokens();
$nonUseTokenTypes = Tokens::$emptyTokens;
$nonUseTokenTypes[] = T_VARIABLE;
$nonUseTokenTypes[] = T_ELLIPSIS;
$nonUseTokenTypes[] = T_COMMA;
Expand Down Expand Up @@ -247,7 +247,7 @@ public static function findFunctionCall(File $phpcsFile, $stackPtr) {
$openPtr = Helpers::findContainingOpeningBracket($phpcsFile, $stackPtr);
if (is_int($openPtr)) {
// First non-whitespace thing and see if it's a T_STRING function name
$functionPtr = $phpcsFile->findPrevious(self::getEmptyTokens(), $openPtr - 1, null, true, null, true);
$functionPtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, $openPtr - 1, null, true, null, true);
if (is_int($functionPtr) && $tokens[$functionPtr]['code'] === T_STRING) {
return $functionPtr;
}
Expand All @@ -274,7 +274,7 @@ public static function findFunctionCallArguments(File $phpcsFile, $stackPtr) {
}

// $stackPtr is the function name, find our brackets after it
$openPtr = $phpcsFile->findNext(self::getEmptyTokens(), $stackPtr + 1, null, true, null, true);
$openPtr = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true);
if (($openPtr === false) || ($tokens[$openPtr]['code'] !== T_OPEN_PARENTHESIS)) {
return [];
}
Expand Down Expand Up @@ -312,7 +312,7 @@ public static function getNextAssignPointer(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();

// Is the next non-whitespace an assignment?
$nextPtr = $phpcsFile->findNext(self::getEmptyTokens(), $stackPtr + 1, null, true, null, true);
$nextPtr = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true);
if (is_int($nextPtr)
&& isset(Tokens::$assignmentTokens[$tokens[$nextPtr]['code']])
// Ignore double arrow to prevent triggering on `foreach ( $array as $k => $v )`.
Expand Down Expand Up @@ -540,14 +540,14 @@ public static function isArrowFunction(File $phpcsFile, $stackPtr) {
return false;
}
// Make sure next non-space token is an open parenthesis
$openParenIndex = $phpcsFile->findNext(self::getEmptyTokens(), $stackPtr + 1, null, true);
$openParenIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true);
if (! is_int($openParenIndex) || $tokens[$openParenIndex]['code'] !== T_OPEN_PARENTHESIS) {
return false;
}
// Find the associated close parenthesis
$closeParenIndex = $tokens[$openParenIndex]['parenthesis_closer'];
// Make sure the next token is a fat arrow
$fatArrowIndex = $phpcsFile->findNext(self::getEmptyTokens(), $closeParenIndex + 1, null, true);
$fatArrowIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $closeParenIndex + 1, null, true);
if (! is_int($fatArrowIndex)) {
return false;
}
Expand Down Expand Up @@ -575,14 +575,14 @@ public static function getArrowFunctionOpenClose(File $phpcsFile, $stackPtr) {
return null;
}
// Make sure next non-space token is an open parenthesis
$openParenIndex = $phpcsFile->findNext(self::getEmptyTokens(), $stackPtr + 1, null, true);
$openParenIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true);
if (! is_int($openParenIndex) || $tokens[$openParenIndex]['code'] !== T_OPEN_PARENTHESIS) {
return null;
}
// Find the associated close parenthesis
$closeParenIndex = $tokens[$openParenIndex]['parenthesis_closer'];
// Make sure the next token is a fat arrow
$fatArrowIndex = $phpcsFile->findNext(self::getEmptyTokens(), $closeParenIndex + 1, null, true);
$fatArrowIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $closeParenIndex + 1, null, true);
if (! is_int($fatArrowIndex)) {
return null;
}
Expand Down Expand Up @@ -632,7 +632,7 @@ public static function getListAssignments(File $phpcsFile, $listOpenerIndex) {
}

// Find the assignment (equals sign) which, if this is a list assignment, should be the next non-space token
$assignPtr = $phpcsFile->findNext(self::getEmptyTokens(), $closePtr + 1, null, true);
$assignPtr = $phpcsFile->findNext(Tokens::$emptyTokens, $closePtr + 1, null, true);

// If the next token isn't an assignment, check for nested brackets because we might be a nested assignment
if (! is_int($assignPtr) || $tokens[$assignPtr]['code'] !== T_EQUAL) {
Expand Down Expand Up @@ -747,8 +747,10 @@ public static function isVariableANumericVariable($varName) {
*/
public static function isVariableInsideElseCondition(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
$nonFunctionTokenTypes = self::getEmptyTokens();
$nonFunctionTokenTypes = Tokens::$emptyTokens;
$nonFunctionTokenTypes[] = T_OPEN_PARENTHESIS;
$nonFunctionTokenTypes[] = T_INLINE_HTML;
$nonFunctionTokenTypes[] = T_CLOSE_TAG;
$nonFunctionTokenTypes[] = T_VARIABLE;
$nonFunctionTokenTypes[] = T_ELLIPSIS;
$nonFunctionTokenTypes[] = T_COMMA;
Expand Down Expand Up @@ -869,7 +871,7 @@ public static function getScopeCloseForScopeOpen(File $phpcsFile, $scopeStartInd
public static function getLastNonEmptyTokenIndexInFile(File $phpcsFile) {
$tokens = $phpcsFile->getTokens();
foreach (array_reverse($tokens, true) as $index => $token) {
if (! in_array($token['code'], self::getEmptyTokens(), true)) {
if (! in_array($token['code'], self::getPossibleEndOfFileTokens(), true)) {
return $index;
}
}
Expand Down Expand Up @@ -953,7 +955,7 @@ public static function getFunctionIndexForFunctionCallArgument(File $phpcsFile,
return null;
}

$nonFunctionTokenTypes = self::getEmptyTokens();
$nonFunctionTokenTypes = Tokens::$emptyTokens;
$functionPtr = self::getIntOrNull($phpcsFile->findPrevious($nonFunctionTokenTypes, $startOfArguments - 1, null, true, null, true));
if (! is_int($functionPtr) || ! isset($tokens[$functionPtr]['code'])) {
return null;
Expand Down Expand Up @@ -997,7 +999,7 @@ public static function isVariableInsideIssetOrEmpty(File $phpcsFile, $stackPtr)
*/
public static function isVariableArrayPushShortcut(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
$nonFunctionTokenTypes = self::getEmptyTokens();
$nonFunctionTokenTypes = Tokens::$emptyTokens;

$arrayPushOperatorIndex1 = self::getIntOrNull($phpcsFile->findNext($nonFunctionTokenTypes, $stackPtr + 1, null, true, null, true));
if (! is_int($arrayPushOperatorIndex1)) {
Expand Down Expand Up @@ -1095,7 +1097,7 @@ public static function isTokenInsideAssignmentLHS(File $phpcsFile, $stackPtr) {
public static function isTokenVariableVariable(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();

$prev = $phpcsFile->findPrevious(self::getEmptyTokens(), ($stackPtr - 1), null, true);
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
if ($prev === false) {
return false;
}
Expand All @@ -1106,7 +1108,7 @@ public static function isTokenVariableVariable(File $phpcsFile, $stackPtr) {
return false;
}

$prevPrev = $phpcsFile->findPrevious(self::getEmptyTokens(), ($prev - 1), null, true);
$prevPrev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prev - 1), null, true);
if ($prevPrev !== false && $tokens[$prevPrev]['code'] === T_DOLLAR) {
return true;
}
Expand Down

0 comments on commit 9df9ade

Please sign in to comment.