Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. VariableAnalysisSniff.php

function VariableAnalysisSniff::processVariableAsForeachLoopVar

*

Parameters

File $phpcsFile: * @param int $stackPtr * @param string $varName * @param int $currScope * * @return bool

1 call to VariableAnalysisSniff::processVariableAsForeachLoopVar()
VariableAnalysisSniff::processVariable in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
* Process a normal variable in the code. * * Most importantly, this function determines if the variable use is a "read" * (using the variable for something) or a "write" (an assignment) or, * sometimes, both at once. * …

File

vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php, line 1417

Class

VariableAnalysisSniff

Namespace

VariableAnalysis\Sniffs\CodeAnalysis

Code

protected function processVariableAsForeachLoopVar(File $phpcsFile, $stackPtr, $varName, $currScope) {
    $tokens = $phpcsFile->getTokens();
    // Are we a foreach loopvar?
    $openParenPtr = Helpers::findContainingOpeningBracket($phpcsFile, $stackPtr);
    if (!is_int($openParenPtr)) {
        return false;
    }
    $foreachPtr = Helpers::getIntOrNull($phpcsFile->findPrevious(Tokens::$emptyTokens, $openParenPtr - 1, null, true));
    if (!is_int($foreachPtr)) {
        return false;
    }
    if ($tokens[$foreachPtr]['code'] === T_LIST) {
        $openParenPtr = Helpers::findContainingOpeningBracket($phpcsFile, $foreachPtr);
        if (!is_int($openParenPtr)) {
            return false;
        }
        $foreachPtr = Helpers::getIntOrNull($phpcsFile->findPrevious(Tokens::$emptyTokens, $openParenPtr - 1, null, true));
        if (!is_int($foreachPtr)) {
            return false;
        }
    }
    if ($tokens[$foreachPtr]['code'] !== T_FOREACH) {
        return false;
    }
    // Is there an 'as' token between us and the foreach?
    if ($phpcsFile->findPrevious(T_AS, $stackPtr - 1, $openParenPtr) === false) {
        return false;
    }
    $this->markVariableAssignment($varName, $stackPtr, $currScope);
    $varInfo = $this->getOrCreateVariableInfo($varName, $currScope);
    // Is this the value of a key => value foreach?
    if ($phpcsFile->findPrevious(T_DOUBLE_ARROW, $stackPtr - 1, $openParenPtr) !== false) {
        $varInfo->isForeachLoopAssociativeValue = true;
    }
    // Are we pass-by-reference?
    $referencePtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true, null, true);
    if ($referencePtr !== false && $tokens[$referencePtr]['code'] === T_BITWISE_AND) {
        Helpers::debug('processVariableAsForeachLoopVar: found foreach loop variable assigned by reference');
        $varInfo->isDynamicReference = true;
    }
    return true;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal