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

Breadcrumb

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

class DisallowIncrementAndDecrementOperatorsSniff

Hierarchy

  • class \SlevomatCodingStandard\Sniffs\Operators\DisallowIncrementAndDecrementOperatorsSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of DisallowIncrementAndDecrementOperatorsSniff

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Operators/DisallowIncrementAndDecrementOperatorsSniff.php, line 12

Namespace

SlevomatCodingStandard\Sniffs\Operators
View source
class DisallowIncrementAndDecrementOperatorsSniff implements Sniff {
    public const CODE_DISALLOWED_PRE_INCREMENT_OPERATOR = 'DisallowedPreIncrementOperator';
    public const CODE_DISALLOWED_POST_INCREMENT_OPERATOR = 'DisallowedPostIncrementOperator';
    public const CODE_DISALLOWED_PRE_DECREMENT_OPERATOR = 'DisallowedPreDecrementOperator';
    public const CODE_DISALLOWED_POST_DECREMENT_OPERATOR = 'DisallowedPostDecrementOperator';
    
    /**
     * @return array<int, (int|string)>
     */
    public function register() : array {
        return [
            T_DEC,
            T_INC,
        ];
    }
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $operatorPointer
     */
    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 ($tokens[$operatorPointer]['code'] === T_INC) {
            if ($isPostOperator) {
                $code = self::CODE_DISALLOWED_POST_INCREMENT_OPERATOR;
                $message = 'Use of post-increment operator is disallowed.';
            }
            else {
                $code = self::CODE_DISALLOWED_PRE_INCREMENT_OPERATOR;
                $message = 'Use of pre-increment operator is disallowed.';
            }
        }
        else {
            if ($isPostOperator) {
                $code = self::CODE_DISALLOWED_POST_DECREMENT_OPERATOR;
                $message = 'Use of post-decrement operator is disallowed.';
            }
            else {
                $code = self::CODE_DISALLOWED_PRE_DECREMENT_OPERATOR;
                $message = 'Use of pre-decrement operator is disallowed.';
            }
        }
        $phpcsFile->addError($message, $operatorPointer, $code);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
DisallowIncrementAndDecrementOperatorsSniff::CODE_DISALLOWED_POST_DECREMENT_OPERATOR public constant
DisallowIncrementAndDecrementOperatorsSniff::CODE_DISALLOWED_POST_INCREMENT_OPERATOR public constant
DisallowIncrementAndDecrementOperatorsSniff::CODE_DISALLOWED_PRE_DECREMENT_OPERATOR public constant
DisallowIncrementAndDecrementOperatorsSniff::CODE_DISALLOWED_PRE_INCREMENT_OPERATOR public constant
DisallowIncrementAndDecrementOperatorsSniff::process public function * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
Overrides Sniff::process
DisallowIncrementAndDecrementOperatorsSniff::register public function * Overrides Sniff::register
RSS feed
Powered by Drupal