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

Breadcrumb

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

function OperatorSpacingSniff::process

Same name in this branch
  1. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php \PHP_CodeSniffer\Standards\PSR12\Sniffs\Operators\OperatorSpacingSniff::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|int Optionally returns a stack pointer. The sniff will not be called again on the current file until the returned stack pointer is reached. Return `$phpcsFile->numTokens` to skip the rest of the file.

Overrides Sniff::process

1 method overrides OperatorSpacingSniff::process()
OperatorSpacingSniff::process in vendor/squizlabs/php_codesniffer/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php
Processes this sniff, when one of its tokens is encountered.

File

vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php, line 137

Class

OperatorSpacingSniff

Namespace

PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    // Skip over declare statements as those should be handled by different sniffs.
    if ($tokens[$stackPtr]['code'] === T_DECLARE) {
        if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) {
            // Parse error / live coding.
            return $phpcsFile->numTokens;
        }
        return $tokens[$stackPtr]['parenthesis_closer'];
    }
    if ($this->isOperator($phpcsFile, $stackPtr) === false) {
        return;
    }
    if ($tokens[$stackPtr]['code'] === T_BITWISE_AND) {
        // Check there is one space before the & operator.
        if ($tokens[$stackPtr - 1]['code'] !== T_WHITESPACE) {
            $error = 'Expected 1 space before "&" operator; 0 found';
            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBeforeAmp');
            if ($fix === true) {
                $phpcsFile->fixer
                    ->addContentBefore($stackPtr, ' ');
            }
            $phpcsFile->recordMetric($stackPtr, 'Space before operator', 0);
        }
        else {
            if ($tokens[$stackPtr - 2]['line'] !== $tokens[$stackPtr]['line']) {
                $found = 'newline';
            }
            else {
                $found = $tokens[$stackPtr - 1]['length'];
            }
            $phpcsFile->recordMetric($stackPtr, 'Space before operator', $found);
            if ($found !== 1 && ($found !== 'newline' || $this->ignoreNewlines === false)) {
                $error = 'Expected 1 space before "&" operator; %s found';
                $data = [
                    $found,
                ];
                $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeAmp', $data);
                if ($fix === true) {
                    $phpcsFile->fixer
                        ->replaceToken($stackPtr - 1, ' ');
                }
            }
        }
        
        //end if
        $hasNext = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
        if ($hasNext === false) {
            // Live coding/parse error at end of file.
            return;
        }
        // Check there is one space after the & operator.
        if ($tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) {
            $error = 'Expected 1 space after "&" operator; 0 found';
            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceAfterAmp');
            if ($fix === true) {
                $phpcsFile->fixer
                    ->addContent($stackPtr, ' ');
            }
            $phpcsFile->recordMetric($stackPtr, 'Space after operator', 0);
        }
        else {
            if ($tokens[$stackPtr + 2]['line'] !== $tokens[$stackPtr]['line']) {
                $found = 'newline';
            }
            else {
                $found = $tokens[$stackPtr + 1]['length'];
            }
            $phpcsFile->recordMetric($stackPtr, 'Space after operator', $found);
            if ($found !== 1 && ($found !== 'newline' || $this->ignoreNewlines === false)) {
                $error = 'Expected 1 space after "&" operator; %s found';
                $data = [
                    $found,
                ];
                $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterAmp', $data);
                if ($fix === true) {
                    $phpcsFile->fixer
                        ->replaceToken($stackPtr + 1, ' ');
                }
            }
        }
        
        //end if
        return;
    }
    
    //end if
    $operator = $tokens[$stackPtr]['content'];
    if ($tokens[$stackPtr - 1]['code'] !== T_WHITESPACE && ($tokens[$stackPtr - 1]['code'] === T_INLINE_THEN && $tokens[$stackPtr]['code'] === T_INLINE_ELSE) === false) {
        $error = "Expected 1 space before \"{$operator}\"; 0 found";
        $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBefore');
        if ($fix === true) {
            $phpcsFile->fixer
                ->addContentBefore($stackPtr, ' ');
        }
        $phpcsFile->recordMetric($stackPtr, 'Space before operator', 0);
    }
    else {
        if (isset(Tokens::$assignmentTokens[$tokens[$stackPtr]['code']]) === false || $this->ignoreSpacingBeforeAssignments === false) {
            // Throw an error for assignments only if enabled using the sniff property
            // because other standards allow multiple spaces to align assignments.
            $prevNonWhitespace = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
            if ($tokens[$prevNonWhitespace]['line'] !== $tokens[$stackPtr]['line']) {
                $found = 'newline';
            }
            else {
                $found = $tokens[$stackPtr - 1]['length'];
            }
            $phpcsFile->recordMetric($stackPtr, 'Space before operator', $found);
            if ($found !== 1 && ($found !== 'newline' || $this->ignoreNewlines === false)) {
                $error = 'Expected 1 space before "%s"; %s found';
                $data = [
                    $operator,
                    $found,
                ];
                if (isset(Tokens::$commentTokens[$tokens[$prevNonWhitespace]['code']]) === true) {
                    // Throw a non-fixable error if the token on the previous line is a comment token,
                    // as in that case it's not for the sniff to decide where the comment should be moved to
                    // and it would get us into unfixable situations as the new line char is included
                    // in the contents of the comment token.
                    $phpcsFile->addError($error, $stackPtr, 'SpacingBefore', $data);
                }
                else {
                    $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBefore', $data);
                    if ($fix === true) {
                        $phpcsFile->fixer
                            ->beginChangeset();
                        if ($found === 'newline') {
                            $i = $stackPtr - 2;
                            while ($tokens[$i]['code'] === T_WHITESPACE) {
                                $phpcsFile->fixer
                                    ->replaceToken($i, '');
                                $i--;
                            }
                        }
                        $phpcsFile->fixer
                            ->replaceToken($stackPtr - 1, ' ');
                        $phpcsFile->fixer
                            ->endChangeset();
                    }
                }
                
                //end if
            }
            
            //end if
        }
    }
    
    //end if
    $hasNext = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
    if ($hasNext === false) {
        // Live coding/parse error at end of file.
        return;
    }
    if ($tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) {
        // Skip short ternary such as: "$foo = $bar ?: true;".
        if ($tokens[$stackPtr]['code'] === T_INLINE_THEN && $tokens[$stackPtr + 1]['code'] === T_INLINE_ELSE) {
            return;
        }
        $error = "Expected 1 space after \"{$operator}\"; 0 found";
        $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceAfter');
        if ($fix === true) {
            $phpcsFile->fixer
                ->addContent($stackPtr, ' ');
        }
        $phpcsFile->recordMetric($stackPtr, 'Space after operator', 0);
    }
    else {
        if (isset($tokens[$stackPtr + 2]) === true && $tokens[$stackPtr + 2]['line'] !== $tokens[$stackPtr]['line']) {
            $found = 'newline';
        }
        else {
            $found = $tokens[$stackPtr + 1]['length'];
        }
        $phpcsFile->recordMetric($stackPtr, 'Space after operator', $found);
        if ($found !== 1 && ($found !== 'newline' || $this->ignoreNewlines === false)) {
            $error = 'Expected 1 space after "%s"; %s found';
            $data = [
                $operator,
                $found,
            ];
            $nextNonWhitespace = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
            if ($nextNonWhitespace !== false && isset(Tokens::$commentTokens[$tokens[$nextNonWhitespace]['code']]) === true && $found === 'newline') {
                // Don't auto-fix when it's a comment or PHPCS annotation on a new line as
                // it causes fixer conflicts and can cause the meaning of annotations to change.
                $phpcsFile->addError($error, $stackPtr, 'SpacingAfter', $data);
            }
            else {
                $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfter', $data);
                if ($fix === true) {
                    $phpcsFile->fixer
                        ->replaceToken($stackPtr + 1, ' ');
                }
            }
        }
        
        //end if
    }
    
    //end if
}

API Navigation

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