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

Breadcrumb

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

function RequireNullSafeObjectOperatorSniff::checkNextCondition

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

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/RequireNullSafeObjectOperatorSniff.php, line 278

Class

RequireNullSafeObjectOperatorSniff

Namespace

SlevomatCodingStandard\Sniffs\ControlStructures

Code

private function checkNextCondition(File $phpcsFile, int $identicalPointer, int $conditionStartPointer, string $identificator, int $nextConditionBooleanPointer) : int {
    $nextIdentificatorPointers = $this->getNextIdentificator($phpcsFile, $nextConditionBooleanPointer);
    if ($nextIdentificatorPointers === null) {
        return $nextConditionBooleanPointer;
    }
    [
        $nextIdentificatorStartPointer,
        $nextIdentificatorEndPointer,
    ] = $nextIdentificatorPointers;
    $nextIdentificator = IdentificatorHelper::getContent($phpcsFile, $nextIdentificatorStartPointer, $nextIdentificatorEndPointer);
    if (!$this->areIdentificatorsCompatible($identificator, $nextIdentificator)) {
        return $nextIdentificatorEndPointer;
    }
    $pointerAfterNexIdentificator = TokenHelper::findNextEffective($phpcsFile, $nextIdentificatorEndPointer + 1);
    $tokens = $phpcsFile->getTokens();
    if ($tokens[$pointerAfterNexIdentificator]['code'] !== $tokens[$identicalPointer]['code'] && !in_array($tokens[$pointerAfterNexIdentificator]['code'], [
        T_INLINE_THEN,
        T_SEMICOLON,
    ], true)) {
        return $pointerAfterNexIdentificator;
    }
    if (!in_array($tokens[$pointerAfterNexIdentificator]['code'], [
        T_IS_IDENTICAL,
        T_IS_NOT_IDENTICAL,
    ], true)) {
        return $pointerAfterNexIdentificator;
    }
    $pointerAfterIdentical = TokenHelper::findNextEffective($phpcsFile, $pointerAfterNexIdentificator + 1);
    if ($tokens[$pointerAfterIdentical]['code'] !== T_NULL) {
        return $pointerAfterNexIdentificator;
    }
    $identificatorDifference = $this->getIdentificatorDifference($phpcsFile, $identificator, $nextIdentificatorStartPointer, $nextIdentificatorEndPointer);
    $fix = $phpcsFile->addFixableError('Operator ?-> is required.', $identicalPointer, self::CODE_REQUIRED_NULL_SAFE_OBJECT_OPERATOR);
    if (!$fix) {
        return $pointerAfterNexIdentificator;
    }
    $isConditionOfTernaryOperator = TernaryOperatorHelper::isConditionOfTernaryOperator($phpcsFile, $identicalPointer);
    $phpcsFile->fixer
        ->beginChangeset();
    FixerHelper::change($phpcsFile, $conditionStartPointer, $nextIdentificatorEndPointer, sprintf('%s?%s', $identificator, $identificatorDifference));
    $phpcsFile->fixer
        ->endChangeset();
    if ($isConditionOfTernaryOperator) {
        return TokenHelper::findNext($phpcsFile, T_INLINE_THEN, $identicalPointer + 1);
    }
    return $pointerAfterNexIdentificator;
}
RSS feed
Powered by Drupal