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

Breadcrumb

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

function RequireNullCoalesceOperatorSniff::checkIdenticalOperator

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

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/RequireNullCoalesceOperatorSniff.php, line 113

Class

RequireNullCoalesceOperatorSniff

Namespace

SlevomatCodingStandard\Sniffs\ControlStructures

Code

public function checkIdenticalOperator(File $phpcsFile, int $identicalOperator) : void {
    $tokens = $phpcsFile->getTokens();
    
    /** @var int $pointerBeforeIdenticalOperator */
    $pointerBeforeIdenticalOperator = TokenHelper::findPreviousEffective($phpcsFile, $identicalOperator - 1);
    
    /** @var int $pointerAfterIdenticalOperator */
    $pointerAfterIdenticalOperator = TokenHelper::findNextEffective($phpcsFile, $identicalOperator + 1);
    if ($tokens[$pointerBeforeIdenticalOperator]['code'] !== T_NULL && $tokens[$pointerAfterIdenticalOperator]['code'] !== T_NULL) {
        return;
    }
    $isYodaCondition = $tokens[$pointerBeforeIdenticalOperator]['code'] === T_NULL;
    $variableEndPointer = $isYodaCondition ? $pointerAfterIdenticalOperator : $pointerBeforeIdenticalOperator;
    $tmpPointer = $variableEndPointer;
    while ($tokens[$tmpPointer]['code'] === T_CLOSE_PARENTHESIS) {
        
        /** @var int $tmpPointer */
        $tmpPointer = TokenHelper::findPreviousEffective($phpcsFile, $tokens[$tmpPointer]['parenthesis_opener'] - 1);
    }
    $variableStartPointer = IdentificatorHelper::findStartPointer($phpcsFile, $tmpPointer);
    if ($variableStartPointer === null) {
        return;
    }
    $pointerBeforeCondition = TokenHelper::findPreviousEffective($phpcsFile, ($isYodaCondition ? $pointerBeforeIdenticalOperator : $variableStartPointer) - 1);
    if (in_array($tokens[$pointerBeforeCondition]['code'], Tokens::$booleanOperators, true)) {
        return;
    }
    
    /** @var int $inlineThenPointer */
    $inlineThenPointer = TokenHelper::findNextEffective($phpcsFile, ($isYodaCondition ? $variableEndPointer : $pointerAfterIdenticalOperator) + 1);
    if ($tokens[$inlineThenPointer]['code'] !== T_INLINE_THEN) {
        return;
    }
    $inlineElsePointer = TernaryOperatorHelper::getElsePointer($phpcsFile, $inlineThenPointer);
    $inlineElseEndPointer = TernaryOperatorHelper::getEndPointer($phpcsFile, $inlineThenPointer, $inlineElsePointer);
    $pointerAfterInlineElseEnd = TokenHelper::findNextEffective($phpcsFile, $inlineElseEndPointer + 1);
    $variableContent = IdentificatorHelper::getContent($phpcsFile, $variableStartPointer, $variableEndPointer);
    
    /** @var int $compareToStartPointer */
    $compareToStartPointer = TokenHelper::findNextEffective($phpcsFile, ($tokens[$identicalOperator]['code'] === T_IS_IDENTICAL ? $inlineElsePointer : $inlineThenPointer) + 1);
    
    /** @var int $compareToEndPointer */
    $compareToEndPointer = TokenHelper::findPreviousEffective($phpcsFile, ($tokens[$identicalOperator]['code'] === T_IS_IDENTICAL ? $pointerAfterInlineElseEnd : $inlineElsePointer) - 1);
    $compareToContent = IdentificatorHelper::getContent($phpcsFile, $compareToStartPointer, $compareToEndPointer);
    if ($compareToContent !== $variableContent) {
        return;
    }
    $fix = $phpcsFile->addFixableError('Use null coalesce operator instead of ternary operator.', $inlineThenPointer, self::CODE_NULL_COALESCE_OPERATOR_NOT_USED);
    if (!$fix) {
        return;
    }
    
    /** @var int $conditionStart */
    $conditionStart = $isYodaCondition ? $pointerBeforeIdenticalOperator : $variableStartPointer;
    $variableContent = trim(TokenHelper::getContent($phpcsFile, $variableStartPointer, $variableEndPointer));
    $phpcsFile->fixer
        ->beginChangeset();
    $phpcsFile->fixer
        ->replaceToken($conditionStart, sprintf('%s ??', $variableContent));
    if ($tokens[$identicalOperator]['code'] === T_IS_IDENTICAL) {
        FixerHelper::removeBetweenIncluding($phpcsFile, $conditionStart + 1, $inlineThenPointer);
        $pointerBeforeInlineElse = TokenHelper::findPreviousEffective($phpcsFile, $inlineElsePointer - 1);
        FixerHelper::removeBetweenIncluding($phpcsFile, $pointerBeforeInlineElse + 1, $inlineElseEndPointer);
    }
    else {
        FixerHelper::removeBetweenIncluding($phpcsFile, $conditionStart + 1, $inlineElsePointer);
    }
    $phpcsFile->fixer
        ->endChangeset();
}

API Navigation

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