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

Breadcrumb

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

function LogicalOperatorSpacingSniff::process

Processes this sniff, when one of its tokens is encountered.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The current file being checked.:

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

Return value

void

Overrides Sniff::process

File

vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/WhiteSpace/LogicalOperatorSpacingSniff.php, line 51

Class

LogicalOperatorSpacingSniff

Namespace

PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    // Check there is one space before the operator.
    if ($tokens[$stackPtr - 1]['code'] !== T_WHITESPACE) {
        $error = 'Expected 1 space before logical operator; 0 found';
        $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBefore');
        if ($fix === true) {
            $phpcsFile->fixer
                ->addContentBefore($stackPtr, ' ');
        }
    }
    else {
        $prev = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
        if ($tokens[$stackPtr]['line'] === $tokens[$prev]['line'] && $tokens[$stackPtr - 1]['length'] !== 1) {
            $found = $tokens[$stackPtr - 1]['length'];
            $error = 'Expected 1 space before logical operator; %s found';
            $data = [
                $found,
            ];
            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'TooMuchSpaceBefore', $data);
            if ($fix === true) {
                $phpcsFile->fixer
                    ->replaceToken($stackPtr - 1, ' ');
            }
        }
    }
    // Check there is one space after the operator.
    if ($tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) {
        $error = 'Expected 1 space after logical operator; 0 found';
        $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceAfter');
        if ($fix === true) {
            $phpcsFile->fixer
                ->addContent($stackPtr, ' ');
        }
    }
    else {
        $next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
        if ($tokens[$stackPtr]['line'] === $tokens[$next]['line'] && $tokens[$stackPtr + 1]['length'] !== 1) {
            $found = $tokens[$stackPtr + 1]['length'];
            $error = 'Expected 1 space after logical operator; %s found';
            $data = [
                $found,
            ];
            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'TooMuchSpaceAfter', $data);
            if ($fix === true) {
                $phpcsFile->fixer
                    ->replaceToken($stackPtr + 1, ' ');
            }
        }
    }
}
RSS feed
Powered by Drupal