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

Breadcrumb

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

function IncrementDecrementUsageSniff::processIncDec

Checks to ensure increment and decrement operators are not confusing.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.:

int $stackPtr The position of the current token: in the stack passed in $tokens.

Return value

void

1 call to IncrementDecrementUsageSniff::processIncDec()
IncrementDecrementUsageSniff::process in vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Operators/IncrementDecrementUsageSniff.php
Processes this test, when one of its tokens is encountered.

File

vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/Operators/IncrementDecrementUsageSniff.php, line 69

Class

IncrementDecrementUsageSniff

Namespace

PHP_CodeSniffer\Standards\Squiz\Sniffs\Operators

Code

protected function processIncDec($phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    // Work out where the variable is so we know where to
    // start looking for other operators.
    if ($tokens[$stackPtr - 1]['code'] === T_VARIABLE || $tokens[$stackPtr - 1]['code'] === T_STRING && ($tokens[$stackPtr - 2]['code'] === T_OBJECT_OPERATOR || $tokens[$stackPtr - 2]['code'] === T_NULLSAFE_OBJECT_OPERATOR)) {
        $start = $stackPtr + 1;
    }
    else {
        $start = $stackPtr + 2;
    }
    $next = $phpcsFile->findNext(Tokens::$emptyTokens, $start, null, true);
    if ($next === false) {
        return;
    }
    if (isset(Tokens::$arithmeticTokens[$tokens[$next]['code']]) === true) {
        $error = 'Increment and decrement operators cannot be used in an arithmetic operation';
        $phpcsFile->addError($error, $stackPtr, 'NotAllowed');
        return;
    }
    $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $start - 3, null, true);
    if ($prev === false) {
        return;
    }
    // Check if this is in a string concat.
    if ($tokens[$next]['code'] === T_STRING_CONCAT || $tokens[$prev]['code'] === T_STRING_CONCAT) {
        $error = 'Increment and decrement operators must be bracketed when used in string concatenation';
        $phpcsFile->addError($error, $stackPtr, 'NoBrackets');
    }
}
RSS feed
Powered by Drupal