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

Breadcrumb

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

function RequireTernaryOperatorSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $ifPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/RequireTernaryOperatorSniff.php, line 47

Class

RequireTernaryOperatorSniff

Namespace

SlevomatCodingStandard\Sniffs\ControlStructures

Code

public function process(File $phpcsFile, $ifPointer) : void {
    $tokens = $phpcsFile->getTokens();
    if (!array_key_exists('scope_closer', $tokens[$ifPointer])) {
        // If without curly braces is not supported.
        return;
    }
    $elsePointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$ifPointer]['scope_closer'] + 1);
    if ($elsePointer === null || $tokens[$elsePointer]['code'] !== T_ELSE) {
        return;
    }
    if (!array_key_exists('scope_closer', $tokens[$elsePointer])) {
        // Else without curly braces is not supported.
        return;
    }
    if (!$this->isCompatibleScope($phpcsFile, $tokens[$ifPointer]['scope_opener'], $tokens[$ifPointer]['scope_closer']) || !$this->isCompatibleScope($phpcsFile, $tokens[$elsePointer]['scope_opener'], $tokens[$elsePointer]['scope_closer'])) {
        return;
    }
    
    /** @var int $firstPointerInIf */
    $firstPointerInIf = TokenHelper::findNextEffective($phpcsFile, $tokens[$ifPointer]['scope_opener'] + 1);
    
    /** @var int $firstPointerInElse */
    $firstPointerInElse = TokenHelper::findNextEffective($phpcsFile, $tokens[$elsePointer]['scope_opener'] + 1);
    if ($tokens[$firstPointerInIf]['code'] === T_RETURN && $tokens[$firstPointerInElse]['code'] === T_RETURN) {
        $this->checkIfWithReturns($phpcsFile, $ifPointer, $elsePointer, $firstPointerInIf, $firstPointerInElse);
        return;
    }
    $this->checkIfWithAssignments($phpcsFile, $ifPointer, $elsePointer, $firstPointerInIf, $firstPointerInElse);
}
RSS feed
Powered by Drupal