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

Breadcrumb

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

function Helpers::isVariableInsideElseBody

*

Parameters

File $phpcsFile: * @param int $stackPtr * * @return bool

1 call to Helpers::isVariableInsideElseBody()
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/Lib/Helpers.php, line 1104

Class

Helpers

Namespace

VariableAnalysis\Lib

Code

public static function isVariableInsideElseBody(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    $token = $tokens[$stackPtr];
    $conditions = isset($token['conditions']) ? $token['conditions'] : [];
    $elseTokenTypes = [
        T_ELSE,
        T_ELSEIF,
    ];
    foreach (array_reverse($conditions, true) as $scopeCode) {
        if (in_array($scopeCode, $elseTokenTypes, true)) {
            return true;
        }
    }
    // Some else body code will not have conditions because it is inline (no
    // curly braces) so we have to look in other ways.
    $previousSemicolonPtr = $phpcsFile->findPrevious([
        T_SEMICOLON,
    ], $stackPtr - 1);
    if (!is_int($previousSemicolonPtr)) {
        $previousSemicolonPtr = 0;
    }
    $elsePtr = $phpcsFile->findPrevious([
        T_ELSE,
        T_ELSEIF,
    ], $stackPtr - 1, $previousSemicolonPtr);
    if (is_int($elsePtr)) {
        return true;
    }
    return false;
}
RSS feed
Powered by Drupal