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

Breadcrumb

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

function RequireTernaryOperatorSniff::checkIfWithAssignments

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

File

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

Class

RequireTernaryOperatorSniff

Namespace

SlevomatCodingStandard\Sniffs\ControlStructures

Code

private function checkIfWithAssignments(File $phpcsFile, int $ifPointer, int $elsePointer, int $firstPointerInIf, int $firstPointerInElse) : void {
    $tokens = $phpcsFile->getTokens();
    $identificatorEndPointerInIf = IdentificatorHelper::findEndPointer($phpcsFile, $firstPointerInIf);
    $identificatorEndPointerInElse = IdentificatorHelper::findEndPointer($phpcsFile, $firstPointerInElse);
    if ($identificatorEndPointerInIf === null || $identificatorEndPointerInElse === null) {
        return;
    }
    $identificatorInIf = TokenHelper::getContent($phpcsFile, $firstPointerInIf, $identificatorEndPointerInIf);
    $identificatorInElse = TokenHelper::getContent($phpcsFile, $firstPointerInElse, $identificatorEndPointerInElse);
    if ($identificatorInIf !== $identificatorInElse) {
        return;
    }
    $assignmentPointerInIf = TokenHelper::findNextEffective($phpcsFile, $identificatorEndPointerInIf + 1);
    $assignmentPointerInElse = TokenHelper::findNextEffective($phpcsFile, $identificatorEndPointerInElse + 1);
    if ($tokens[$assignmentPointerInIf]['code'] !== T_EQUAL || $tokens[$assignmentPointerInElse]['code'] !== T_EQUAL) {
        return;
    }
    $pointerAfterAssignmentInIf = TokenHelper::findNextEffective($phpcsFile, $assignmentPointerInIf + 1);
    $pointerAfterAssignmentInElse = TokenHelper::findNextEffective($phpcsFile, $assignmentPointerInElse + 1);
    if ($tokens[$pointerAfterAssignmentInIf]['code'] === T_BITWISE_AND || $tokens[$pointerAfterAssignmentInElse]['code'] === T_BITWISE_AND) {
        return;
    }
    $ifContainsComment = $this->containsComment($phpcsFile, $ifPointer);
    $elseContainsComment = $this->containsComment($phpcsFile, $elsePointer);
    $conditionContainsLogicalOperators = $this->containsLogicalOperators($phpcsFile, $ifPointer);
    $errorParameters = [
        'Use ternary operator.',
        $ifPointer,
        self::CODE_TERNARY_OPERATOR_NOT_USED,
    ];
    if ($ifContainsComment || $elseContainsComment || $conditionContainsLogicalOperators) {
        $phpcsFile->addError(...$errorParameters);
        return;
    }
    $fix = $phpcsFile->addFixableError(...$errorParameters);
    if (!$fix) {
        return;
    }
    
    /** @var int $semicolonAfterAssignmentInIf */
    $semicolonAfterAssignmentInIf = TokenHelper::findNext($phpcsFile, T_SEMICOLON, $pointerAfterAssignmentInIf + 1);
    $semicolonAfterAssignmentInElse = TokenHelper::findNext($phpcsFile, T_SEMICOLON, $pointerAfterAssignmentInElse + 1);
    $phpcsFile->fixer
        ->beginChangeset();
    FixerHelper::change($phpcsFile, $ifPointer, $tokens[$ifPointer]['parenthesis_opener'], sprintf('%s = ', $identificatorInIf));
    FixerHelper::change($phpcsFile, $tokens[$ifPointer]['parenthesis_closer'], $pointerAfterAssignmentInIf - 1, ' ? ');
    FixerHelper::change($phpcsFile, $semicolonAfterAssignmentInIf, $pointerAfterAssignmentInElse - 1, ' : ');
    FixerHelper::removeBetweenIncluding($phpcsFile, $semicolonAfterAssignmentInElse + 1, $tokens[$elsePointer]['scope_closer']);
    $phpcsFile->fixer
        ->endChangeset();
}

API Navigation

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