protected function VariableAnalysisSniff::checkForForeachLoopVar in Coder 8.2
Check if this is a foreach loop variable.
Parameters
\PHP_CodeSniffer\Files\File $phpcsFile:
int $stackPtr:
string $varName:
string $currScope:
Return value
bool
1 call to VariableAnalysisSniff::checkForForeachLoopVar()
- VariableAnalysisSniff::processVariable in coder_sniffer/
DrupalPractice/ Sniffs/ CodeAnalysis/ VariableAnalysisSniff.php - Called to process normal member vars.
File
- coder_sniffer/
DrupalPractice/ Sniffs/ CodeAnalysis/ VariableAnalysisSniff.php, line 1774
Class
- VariableAnalysisSniff
- Checks the for undefined function variables.
Namespace
DrupalPractice\Sniffs\CodeAnalysisCode
protected function checkForForeachLoopVar(File $phpcsFile, $stackPtr, $varName, $currScope) {
$tokens = $phpcsFile
->getTokens();
// Are we a foreach loopvar?
if (($openPtr = $this
->findContainingBrackets($phpcsFile, $stackPtr)) === false) {
return false;
}
// Is there an 'as' token between us and the opening bracket?
if ($phpcsFile
->findPrevious(T_AS, $stackPtr - 1, $openPtr) === false) {
return false;
}
$this
->markVariableAssignment($varName, $stackPtr, $currScope);
// Workaround: We want to allow foreach ($array as $key => $value) where
// $value is never read, so we just mark it read immediately here.
if ($phpcsFile
->findPrevious(T_DOUBLE_ARROW, $stackPtr - 1, $openPtr) !== false) {
$this
->markVariableRead($varName, $stackPtr, $currScope);
}
// Foreach variables that are read as references like
// foreach ($array as &$value) should not throw unused variable errors.
if (($refPtr = $phpcsFile
->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true)) !== false && $tokens[$refPtr]['code'] === T_BITWISE_AND) {
$varInfo = $this
->getVariableInfo($varName, $currScope);
$varInfo->passByReference = true;
}
return true;
}