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

Breadcrumb

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

function RequireCombinedAssignmentOperatorSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $equalPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Operators/RequireCombinedAssignmentOperatorSniff.php, line 55

Class

RequireCombinedAssignmentOperatorSniff

Namespace

SlevomatCodingStandard\Sniffs\Operators

Code

public function process(File $phpcsFile, $equalPointer) : void {
    $tokens = $phpcsFile->getTokens();
    
    /** @var int $variableStartPointer */
    $variableStartPointer = TokenHelper::findNextEffective($phpcsFile, $equalPointer + 1);
    $variableEndPointer = IdentificatorHelper::findEndPointer($phpcsFile, $variableStartPointer);
    if ($variableEndPointer === null) {
        return;
    }
    $operatorPointer = TokenHelper::findNextEffective($phpcsFile, $variableEndPointer + 1);
    $operators = [
        T_BITWISE_AND => '&=',
        T_BITWISE_OR => '|=',
        T_STRING_CONCAT => '.=',
        T_DIVIDE => '/=',
        T_MINUS => '-=',
        T_POW => '**=',
        T_MODULUS => '%=',
        T_MULTIPLY => '*=',
        T_PLUS => '+=',
        T_SL => '<<=',
        T_SR => '>>=',
        T_BITWISE_XOR => '^=',
    ];
    if (!array_key_exists($tokens[$operatorPointer]['code'], $operators)) {
        return;
    }
    $isFixable = true;
    if ($tokens[$variableEndPointer]['code'] === T_CLOSE_SQUARE_BRACKET) {
        $pointerAfterOperator = TokenHelper::findNextEffective($phpcsFile, $operatorPointer + 1);
        if (in_array($tokens[$pointerAfterOperator]['code'], [
            T_CONSTANT_ENCAPSED_STRING,
            T_DOUBLE_QUOTED_STRING,
            T_START_HEREDOC,
            T_START_NOWDOC,
        ], true)) {
            return;
        }
        $isFixable = in_array($tokens[$pointerAfterOperator]['code'], [
            T_LNUMBER,
            T_DNUMBER,
        ], true);
    }
    $variableContent = IdentificatorHelper::getContent($phpcsFile, $variableStartPointer, $variableEndPointer);
    
    /** @var int $beforeEqualEndPointer */
    $beforeEqualEndPointer = TokenHelper::findPreviousEffective($phpcsFile, $equalPointer - 1);
    $beforeEqualStartPointer = IdentificatorHelper::findStartPointer($phpcsFile, $beforeEqualEndPointer);
    if ($beforeEqualStartPointer === null) {
        return;
    }
    $beforeEqualVariableContent = IdentificatorHelper::getContent($phpcsFile, $beforeEqualStartPointer, $beforeEqualEndPointer);
    if ($beforeEqualVariableContent !== $variableContent) {
        return;
    }
    $semicolonPointer = TokenHelper::findNext($phpcsFile, T_SEMICOLON, $equalPointer + 1);
    if (TokenHelper::findNext($phpcsFile, Tokens::$operators, $operatorPointer + 1, $semicolonPointer) !== null) {
        return;
    }
    $errorMessage = sprintf('Use "%s" operator instead of "=" and "%s".', $operators[$tokens[$operatorPointer]['code']], $tokens[$operatorPointer]['content']);
    if (!$isFixable) {
        $phpcsFile->addError($errorMessage, $equalPointer, self::CODE_REQUIRED_COMBINED_ASSIGNMENT_OPERATOR);
        return;
    }
    $fix = $phpcsFile->addFixableError($errorMessage, $equalPointer, self::CODE_REQUIRED_COMBINED_ASSIGNMENT_OPERATOR);
    if (!$fix) {
        return;
    }
    $phpcsFile->fixer
        ->beginChangeset();
    FixerHelper::change($phpcsFile, $equalPointer, $operatorPointer, $operators[$tokens[$operatorPointer]['code']]);
    $phpcsFile->fixer
        ->endChangeset();
}

API Navigation

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