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

Breadcrumb

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

function EarlyExitSniff::getAllConditionsPointers

*

Return value

list<int>

3 calls to EarlyExitSniff::getAllConditionsPointers()
EarlyExitSniff::findEarlyExitInScope in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/EarlyExitSniff.php
EarlyExitSniff::processElse in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/EarlyExitSniff.php
EarlyExitSniff::processElseIf in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/EarlyExitSniff.php

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/EarlyExitSniff.php, line 427

Class

EarlyExitSniff

Namespace

SlevomatCodingStandard\Sniffs\ControlStructures

Code

private function getAllConditionsPointers(File $phpcsFile, int $conditionPointer) : array {
    $tokens = $phpcsFile->getTokens();
    $conditionsPointers = [
        $conditionPointer,
    ];
    if (isset($tokens[$conditionPointer]['scope_opener']) && $tokens[$tokens[$conditionPointer]['scope_opener']]['code'] === T_COLON) {
        // Alternative control structure syntax.
        throw new Exception(sprintf('"%s" without curly braces is not supported.', $tokens[$conditionPointer]['content']));
    }
    if ($tokens[$conditionPointer]['code'] !== T_IF) {
        $currentConditionPointer = $conditionPointer;
        do {
            $previousConditionCloseParenthesisPointer = TokenHelper::findPreviousEffective($phpcsFile, $currentConditionPointer - 1);
            $currentConditionPointer = $tokens[$previousConditionCloseParenthesisPointer]['scope_condition'];
            $conditionsPointers[] = $currentConditionPointer;
        } while ($tokens[$currentConditionPointer]['code'] !== T_IF);
    }
    if ($tokens[$conditionPointer]['code'] !== T_ELSE) {
        if (!array_key_exists('scope_closer', $tokens[$conditionPointer])) {
            throw new Exception(sprintf('"%s" without curly braces is not supported.', $tokens[$conditionPointer]['content']));
        }
        $currentConditionPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$conditionPointer]['scope_closer'] + 1);
        if ($currentConditionPointer !== null) {
            while (in_array($tokens[$currentConditionPointer]['code'], [
                T_ELSEIF,
                T_ELSE,
            ], true)) {
                $conditionsPointers[] = $currentConditionPointer;
                if (!array_key_exists('scope_closer', $tokens[$currentConditionPointer])) {
                    throw new Exception(sprintf('"%s" without curly braces is not supported.', $tokens[$currentConditionPointer]['content']));
                }
                $currentConditionPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$currentConditionPointer]['scope_closer'] + 1);
            }
        }
    }
    sort($conditionsPointers);
    return $conditionsPointers;
}
RSS feed
Powered by Drupal