Skip to content
Prev Previous commit
Next Next commit
Move findVariableScope to Helpers
  • Loading branch information
sirbrillig committed Feb 12, 2018
commit 894a0e87be1ec26b40b5a076ea0cb540c98cfdda
46 changes: 46 additions & 0 deletions VariableAnalysis/Lib/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,50 @@ public static function isNextThingAnAssign(File $phpcsFile, int $stackPtr) {
public static function normalizeVarName($varName) {
return preg_replace('/[{}$]/', '', $varName);
}

public static function findFunctionPrototype(File $phpcsFile, int $stackPtr) {
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];

$openPtr = Helpers::findContainingBrackets($phpcsFile, $stackPtr);
if ($openPtr === false) {
return false;
}
$functionPtr = Helpers::findPreviousFunctionPtr($phpcsFile, $openPtr);
if (($functionPtr !== false) && ($tokens[$functionPtr]['code'] === T_FUNCTION)) {
return $functionPtr;
}
return false;
}

public static function findVariableScope(File $phpcsFile, int $stackPtr) {
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];

$in_class = false;
if (!empty($token['conditions'])) {
foreach (array_reverse($token['conditions'], true) as $scopePtr => $scopeCode) {
if (($scopeCode === T_FUNCTION) || ($scopeCode === T_CLOSURE)) {
return $scopePtr;
}
if (($scopeCode === T_CLASS) || ($scopeCode === T_INTERFACE)) {
$in_class = true;
}
}
}

$scopePtr = Helpers::findFunctionPrototype($phpcsFile, $stackPtr);
if ($scopePtr !== false) {
return $scopePtr;
}

if ($in_class) {
// Member var of a class, we don't care.
return false;
}

// File scope, hmm, lets use first token of file?
return 0;
}

}
51 changes: 3 additions & 48 deletions VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,51 +218,6 @@ protected function markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $sta
}
}

protected function findFunctionPrototype(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];

$openPtr = Helpers::findContainingBrackets($phpcsFile, $stackPtr);
if ($openPtr === false) {
return false;
}
$functionPtr = Helpers::findPreviousFunctionPtr($phpcsFile, $openPtr);
if (($functionPtr !== false) && ($tokens[$functionPtr]['code'] === T_FUNCTION)) {
return $functionPtr;
}
return false;
}

protected function findVariableScope(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];

$in_class = false;
if (!empty($token['conditions'])) {
foreach (array_reverse($token['conditions'], true) as $scopePtr => $scopeCode) {
if (($scopeCode === T_FUNCTION) || ($scopeCode === T_CLOSURE)) {
return $scopePtr;
}
if (($scopeCode === T_CLASS) || ($scopeCode === T_INTERFACE)) {
$in_class = true;
}
}
}

$scopePtr = $this->findFunctionPrototype($phpcsFile, $stackPtr);
if ($scopePtr !== false) {
return $scopePtr;
}

if ($in_class) {
// Member var of a class, we don't care.
return false;
}

// File scope, hmm, lets use first token of file?
return 0;
}

protected function checkForFunctionPrototype(File $phpcsFile, $stackPtr, $varName, $currScope) {
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
Expand Down Expand Up @@ -684,7 +639,7 @@ protected function processVariable(File $phpcsFile, $stackPtr) {
$token = $tokens[$stackPtr];

$varName = Helpers::normalizeVarName($token['content']);
$currScope = $this->findVariableScope($phpcsFile, $stackPtr);
$currScope = Helpers::findVariableScope($phpcsFile, $stackPtr);
if ($currScope === false) {
return;
}
Expand Down Expand Up @@ -795,7 +750,7 @@ protected function processVariableInString(File $phpcsFile, $stackPtr) {
return;
}

$currScope = $this->findVariableScope($phpcsFile, $stackPtr);
$currScope = Helpers::findVariableScope($phpcsFile, $stackPtr);
foreach ($matches[1] as $varName) {
$varName = Helpers::normalizeVarName($varName);
// Are we $this within a class?
Expand Down Expand Up @@ -867,7 +822,7 @@ protected function processCompact(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];

$currScope = $this->findVariableScope($phpcsFile, $stackPtr);
$currScope = Helpers::findVariableScope($phpcsFile, $stackPtr);

$arguments = Helpers::findFunctionCallArguments($phpcsFile, $stackPtr);
if ($arguments !== false) {
Expand Down