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

Breadcrumb

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

function Helpers::makeForLoopInfo

*

Parameters

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

1 call to Helpers::makeForLoopInfo()
VariableAnalysisSniff::recordForLoop in vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
* Record the boundaries of a for loop. * *

File

vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Lib/Helpers.php, line 1527

Class

Helpers

Namespace

VariableAnalysis\Lib

Code

public static function makeForLoopInfo(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    $token = $tokens[$stackPtr];
    $forIndex = $stackPtr;
    $blockStart = $token['parenthesis_closer'];
    if (isset($token['scope_opener'])) {
        $blockStart = $token['scope_opener'];
        $blockEnd = $token['scope_closer'];
    }
    else {
        // Some for loop blocks will not have scope positions because it they are
        // inline (no curly braces) so we have to find the end of their scope by
        // looking for the end of the next statement.
        $nextSemicolonIndex = $phpcsFile->findNext([
            T_SEMICOLON,
        ], $token['parenthesis_closer']);
        if (!is_int($nextSemicolonIndex)) {
            $nextSemicolonIndex = $token['parenthesis_closer'] + 1;
        }
        $blockEnd = $nextSemicolonIndex;
    }
    $initStart = intval($token['parenthesis_opener']) + 1;
    $initEnd = null;
    $conditionStart = null;
    $conditionEnd = null;
    $incrementStart = null;
    $incrementEnd = $token['parenthesis_closer'] - 1;
    $semicolonCount = 0;
    $forLoopLevel = $tokens[$forIndex]['level'];
    $forLoopNestedParensCount = 1;
    if (isset($tokens[$forIndex]['nested_parenthesis'])) {
        $forLoopNestedParensCount = count($tokens[$forIndex]['nested_parenthesis']) + 1;
    }
    for ($i = $initStart; $i <= $incrementEnd && $semicolonCount < 2; $i++) {
        if ($tokens[$i]['code'] !== T_SEMICOLON) {
            continue;
        }
        if ($tokens[$i]['level'] !== $forLoopLevel) {
            continue;
        }
        if (count($tokens[$i]['nested_parenthesis']) !== $forLoopNestedParensCount) {
            continue;
        }
        switch ($semicolonCount) {
            case 0:
                $initEnd = $i;
                $conditionStart = $initEnd + 1;
                break;
            case 1:
                $conditionEnd = $i;
                $incrementStart = $conditionEnd + 1;
                break;
        }
        $semicolonCount += 1;
    }
    if ($initEnd === null || $conditionStart === null || $conditionEnd === null || $incrementStart === null) {
        throw new \Exception("Cannot parse for loop at position {$forIndex}");
    }
    return new ForLoopInfo($forIndex, $blockStart, $blockEnd, $initStart, $initEnd, $conditionStart, $conditionEnd, $incrementStart, $incrementEnd);
}
RSS feed
Powered by Drupal