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

Breadcrumb

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

function DisallowTrailingMultiLineTernaryOperatorSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $inlineThenPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/DisallowTrailingMultiLineTernaryOperatorSniff.php, line 34

Class

DisallowTrailingMultiLineTernaryOperatorSniff

Namespace

SlevomatCodingStandard\Sniffs\ControlStructures

Code

public function process(File $phpcsFile, $inlineThenPointer) : void {
    $tokens = $phpcsFile->getTokens();
    $nextPointer = TokenHelper::findNextEffective($phpcsFile, $inlineThenPointer + 1);
    if ($tokens[$nextPointer]['code'] === T_INLINE_ELSE) {
        return;
    }
    if ($tokens[$inlineThenPointer]['line'] === $tokens[$nextPointer]['line']) {
        return;
    }
    $fix = $phpcsFile->addFixableError('Ternary operator should be reformatted as leading the line.', $inlineThenPointer, self::CODE_TRAILING_MULTI_LINE_TERNARY_OPERATOR_USED);
    if (!$fix) {
        return;
    }
    $inlineElsePointer = TernaryOperatorHelper::getElsePointer($phpcsFile, $inlineThenPointer);
    $pointerBeforeInlineThen = TokenHelper::findPreviousEffective($phpcsFile, $inlineThenPointer - 1);
    $pointerAfterInlineThen = TokenHelper::findNextExcluding($phpcsFile, [
        T_WHITESPACE,
    ], $inlineThenPointer + 1);
    $pointerBeforeInlineElse = TokenHelper::findPreviousEffective($phpcsFile, $inlineElsePointer - 1);
    $pointerAfterInlineElse = TokenHelper::findNextExcluding($phpcsFile, [
        T_WHITESPACE,
    ], $inlineElsePointer + 1);
    $indentation = IndentationHelper::addIndentation(IndentationHelper::getIndentation($phpcsFile, TokenHelper::findFirstNonWhitespaceOnLine($phpcsFile, $inlineThenPointer)));
    $phpcsFile->fixer
        ->beginChangeset();
    FixerHelper::removeBetween($phpcsFile, $pointerBeforeInlineThen, $inlineThenPointer);
    FixerHelper::removeBetween($phpcsFile, $inlineThenPointer, $pointerAfterInlineThen);
    $phpcsFile->fixer
        ->addContentBefore($inlineThenPointer, $phpcsFile->eolChar . $indentation);
    $phpcsFile->fixer
        ->addContentBefore($pointerAfterInlineThen, ' ');
    FixerHelper::removeBetween($phpcsFile, $pointerBeforeInlineElse, $inlineElsePointer);
    FixerHelper::removeBetween($phpcsFile, $inlineElsePointer, $pointerAfterInlineElse);
    $phpcsFile->fixer
        ->addContentBefore($inlineElsePointer, $phpcsFile->eolChar . $indentation);
    $phpcsFile->fixer
        ->addContentBefore($pointerAfterInlineElse, ' ');
    $phpcsFile->fixer
        ->endChangeset();
}

API Navigation

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