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

Breadcrumb

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

function EarlyExitSniff::processElseIf

1 call to EarlyExitSniff::processElseIf()
EarlyExitSniff::process in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/EarlyExitSniff.php
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

File

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

Class

EarlyExitSniff

Namespace

SlevomatCodingStandard\Sniffs\ControlStructures

Code

private function processElseIf(File $phpcsFile, int $elseIfPointer) : void {
    $tokens = $phpcsFile->getTokens();
    try {
        $allConditionsPointers = $this->getAllConditionsPointers($phpcsFile, $elseIfPointer);
    } catch (Throwable $e) {
        // Elseif without curly braces is not supported.
        return;
    }
    if (TokenHelper::findNext($phpcsFile, T_FUNCTION, $tokens[$elseIfPointer]['scope_opener'] + 1, $tokens[$elseIfPointer]['scope_closer']) !== null) {
        return;
    }
    foreach ($allConditionsPointers as $conditionPointer) {
        $conditionEarlyExitPointer = $this->findEarlyExitInScope($phpcsFile, $tokens[$conditionPointer]['scope_opener'], $tokens[$conditionPointer]['scope_closer']);
        if ($conditionPointer === $elseIfPointer) {
            break;
        }
        if ($conditionEarlyExitPointer === null) {
            return;
        }
    }
    $fix = $phpcsFile->addFixableError('Use "if" instead of "elseif".', $elseIfPointer, self::CODE_USELESS_ELSEIF);
    if (!$fix) {
        return;
    }
    
    /** @var int $pointerBeforeElseIfPointer */
    $pointerBeforeElseIfPointer = TokenHelper::findPreviousNonWhitespace($phpcsFile, $elseIfPointer - 1);
    $phpcsFile->fixer
        ->beginChangeset();
    FixerHelper::removeBetween($phpcsFile, $pointerBeforeElseIfPointer, $elseIfPointer);
    $phpcsFile->fixer
        ->addNewline($pointerBeforeElseIfPointer);
    $phpcsFile->fixer
        ->addNewline($pointerBeforeElseIfPointer);
    $phpcsFile->fixer
        ->replaceToken($elseIfPointer, sprintf('%sif', IndentationHelper::getIndentation($phpcsFile, $allConditionsPointers[0])));
    $phpcsFile->fixer
        ->endChangeset();
}

API Navigation

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