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

Breadcrumb

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

function VariableAnalysisSniff::processVaribleInsideElse

*

Parameters

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

1 call to VariableAnalysisSniff::processVaribleInsideElse()
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 1772

Class

VariableAnalysisSniff

Namespace

VariableAnalysis\Sniffs\CodeAnalysis

Code

protected function processVaribleInsideElse(File $phpcsFile, $stackPtr, $varName, $currScope) {
    // Find all assignments to this variable inside the current scope.
    $varInfo = $this->getOrCreateVariableInfo($varName, $currScope);
    $allAssignmentIndices = array_unique($varInfo->allAssignments);
    // Find the attached 'if' and 'elseif' block start and end indices.
    $blockIndices = Helpers::getAttachedBlockIndicesForElse($phpcsFile, $stackPtr);
    // If all of the assignments are within the previous attached blocks, then warn about undefined.
    $tokens = $phpcsFile->getTokens();
    $assignmentsInsideAttachedBlocks = [];
    foreach ($allAssignmentIndices as $index) {
        foreach ($blockIndices as $blockIndex) {
            $blockToken = $tokens[$blockIndex];
            Helpers::debug('for variable inside else, looking at assignment', $index, 'at block index', $blockIndex, 'which is token', $blockToken);
            if (isset($blockToken['scope_opener']) && isset($blockToken['scope_closer'])) {
                $scopeOpener = $blockToken['scope_opener'];
                $scopeCloser = $blockToken['scope_closer'];
            }
            else {
                // If the `if` statement has no scope, it is probably inline, which
                // means its scope is from the end of the condition up until the next
                // semicolon
                $scopeOpener = isset($blockToken['parenthesis_closer']) ? $blockToken['parenthesis_closer'] : $blockIndex + 1;
                $scopeCloser = $phpcsFile->findNext([
                    T_SEMICOLON,
                ], $scopeOpener);
                if (!$scopeCloser) {
                    throw new \Exception("Cannot find scope for if condition block at index {$stackPtr} while examining variable {$varName}");
                }
            }
            Helpers::debug('for variable inside else, looking at scope', $index, 'between', $scopeOpener, 'and', $scopeCloser);
            if (Helpers::isIndexInsideScope($index, $scopeOpener, $scopeCloser)) {
                $assignmentsInsideAttachedBlocks[] = $index;
            }
        }
    }
    if (count($assignmentsInsideAttachedBlocks) === count($allAssignmentIndices)) {
        if (!$varInfo->ignoreUndefined) {
            Helpers::debug("variable {$varName} inside else looks undefined");
            $this->warnAboutUndefinedVariable($phpcsFile, $varName, $stackPtr);
        }
        return;
    }
    Helpers::debug('looks like a variable read inside else');
    $this->markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $stackPtr, $currScope);
}
RSS feed
Powered by Drupal