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

Breadcrumb

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

function EarlyExitSniff::processIf

1 call to EarlyExitSniff::processIf()
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 257

Class

EarlyExitSniff

Namespace

SlevomatCodingStandard\Sniffs\ControlStructures

Code

private function processIf(File $phpcsFile, int $ifPointer) : void {
    $tokens = $phpcsFile->getTokens();
    if (!array_key_exists('scope_closer', $tokens[$ifPointer])) {
        // If without curly braces is not supported.
        return;
    }
    $nextPointer = TokenHelper::findNextNonWhitespace($phpcsFile, $tokens[$ifPointer]['scope_closer'] + 1);
    if ($nextPointer === null || $tokens[$nextPointer]['code'] !== T_CLOSE_CURLY_BRACKET) {
        return;
    }
    $previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $ifPointer - 1);
    if ($this->ignoreStandaloneIfInScope && in_array($tokens[$previousPointer]['code'], [
        T_OPEN_CURLY_BRACKET,
        T_COLON,
    ], true)) {
        return;
    }
    if ($this->ignoreOneLineTrailingIf && $tokens[$tokens[$ifPointer]['scope_opener']]['line'] + 2 === $tokens[$tokens[$ifPointer]['scope_closer']]['line']) {
        return;
    }
    if ($this->ignoreTrailingIfWithOneInstruction) {
        $pointerBeforeScopeCloser = TokenHelper::findPreviousEffective($phpcsFile, $tokens[$ifPointer]['scope_closer'] - 1);
        if ($tokens[$pointerBeforeScopeCloser]['code'] === T_SEMICOLON) {
            $ignore = true;
            $searchStartPointer = $tokens[$ifPointer]['scope_opener'] + 1;
            while (true) {
                $anotherSemicolonPointer = TokenHelper::findNext($phpcsFile, T_SEMICOLON, $searchStartPointer, $pointerBeforeScopeCloser);
                if ($anotherSemicolonPointer === null) {
                    break;
                }
                if (ScopeHelper::isInSameScope($phpcsFile, $anotherSemicolonPointer, $pointerBeforeScopeCloser)) {
                    $ignore = false;
                    break;
                }
                $searchStartPointer = $anotherSemicolonPointer + 1;
            }
            if ($ignore) {
                return;
            }
        }
    }
    $scopePointer = $tokens[$nextPointer]['scope_condition'];
    if (!in_array($tokens[$scopePointer]['code'], [
        T_FUNCTION,
        T_CLOSURE,
        T_WHILE,
        T_DO,
        T_FOREACH,
        T_FOR,
    ], true)) {
        return;
    }
    if ($this->isEarlyExitInScope($phpcsFile, $tokens[$ifPointer]['scope_opener'], $tokens[$ifPointer]['scope_closer'])) {
        return;
    }
    $fix = $phpcsFile->addFixableError('Use early exit to reduce code nesting.', $ifPointer, self::CODE_EARLY_EXIT_NOT_USED);
    if (!$fix) {
        return;
    }
    $ifCodePointers = $this->getScopeCodePointers($phpcsFile, $ifPointer);
    $ifIndentation = IndentationHelper::getIndentation($phpcsFile, $ifPointer);
    $earlyExitCode = $this->getEarlyExitCode($tokens[$scopePointer]['code']);
    $earlyExitCodeIndentation = IndentationHelper::addIndentation($ifIndentation);
    $negativeIfCondition = ConditionHelper::getNegativeCondition($phpcsFile, $tokens[$ifPointer]['parenthesis_opener'], $tokens[$ifPointer]['parenthesis_closer']);
    $afterIfCode = IndentationHelper::fixIndentation($phpcsFile, $ifCodePointers, $ifIndentation);
    $ifContent = sprintf('if %s {%s%s%s;%s%s}%s%s', $negativeIfCondition, $phpcsFile->eolChar, $earlyExitCodeIndentation, $earlyExitCode, $phpcsFile->eolChar, $ifIndentation, $phpcsFile->eolChar, $afterIfCode);
    $phpcsFile->fixer
        ->beginChangeset();
    FixerHelper::change($phpcsFile, $ifPointer, $tokens[$ifPointer]['scope_closer'], $ifContent);
    $phpcsFile->fixer
        ->endChangeset();
}

API Navigation

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