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

Breadcrumb

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

function RequireOnlyStandaloneIncrementAndDecrementOperatorsSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $operatorPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Operators/RequireOnlyStandaloneIncrementAndDecrementOperatorsSniff.php, line 45

Class

RequireOnlyStandaloneIncrementAndDecrementOperatorsSniff

Namespace

SlevomatCodingStandard\Sniffs\Operators

Code

public function process(File $phpcsFile, $operatorPointer) : void {
    $tokens = $phpcsFile->getTokens();
    
    /** @var int $nextPointer */
    $nextPointer = TokenHelper::findNextEffective($phpcsFile, $operatorPointer + 1);
    $afterVariableEndPointer = IdentificatorHelper::findEndPointer($phpcsFile, $nextPointer);
    $isPostOperator = $afterVariableEndPointer === null;
    if ($isPostOperator) {
        
        /** @var int $beforeVariableEndPointer */
        $beforeVariableEndPointer = TokenHelper::findPreviousEffective($phpcsFile, $operatorPointer - 1);
        
        /** @var int $instructionStartPointer */
        $instructionStartPointer = IdentificatorHelper::findStartPointer($phpcsFile, $beforeVariableEndPointer);
        $instructionEndPointer = $operatorPointer;
    }
    else {
        $instructionStartPointer = $operatorPointer;
        
        /** @var int $instructionEndPointer */
        $instructionEndPointer = $afterVariableEndPointer;
    }
    if ($this->isStandalone($phpcsFile, $instructionStartPointer, $instructionEndPointer)) {
        return;
    }
    if ($tokens[$operatorPointer]['code'] === T_INC) {
        if ($isPostOperator) {
            $code = self::CODE_POST_INCREMENT_OPERATOR_NOT_USED_STANDALONE;
            $message = 'Post-increment operator should be used only as single instruction.';
        }
        else {
            $code = self::CODE_PRE_INCREMENT_OPERATOR_NOT_USED_STANDALONE;
            $message = 'Pre-increment operator should be used only as single instruction.';
        }
    }
    else {
        if ($isPostOperator) {
            $code = self::CODE_POST_DECREMENT_OPERATOR_NOT_USED_STANDALONE;
            $message = 'Post-decrement operator should be used only as single instruction.';
        }
        else {
            $code = self::CODE_PRE_DECREMENT_OPERATOR_NOT_USED_STANDALONE;
            $message = 'Pre-decrement operator should be used only as single instruction.';
        }
    }
    $phpcsFile->addError($message, $operatorPointer, $code);
}
RSS feed
Powered by Drupal