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

Breadcrumb

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

class OperatorSpacingSniff

Same name in this branch
  1. 11.1.x vendor/squizlabs/php_codesniffer/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php \PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\OperatorSpacingSniff

Hierarchy

  • class \PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\OperatorSpacingSniff implements \PHP_CodeSniffer\Sniffs\Sniff
    • class \PHP_CodeSniffer\Standards\PSR12\Sniffs\Operators\OperatorSpacingSniff extends \PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\OperatorSpacingSniff

Expanded class hierarchy of OperatorSpacingSniff

File

vendor/squizlabs/php_codesniffer/src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php, line 16

Namespace

PHP_CodeSniffer\Standards\PSR12\Sniffs\Operators
View source
class OperatorSpacingSniff extends SquizOperatorSpacingSniff {
    
    /**
     * Returns an array of tokens this test wants to listen for.
     *
     * @return array<int|string>
     */
    public function register() {
        parent::register();
        $targets = Tokens::$comparisonTokens;
        $targets += Tokens::$operators;
        $targets += Tokens::$assignmentTokens;
        $targets += Tokens::$booleanOperators;
        $targets[] = T_INLINE_THEN;
        $targets[] = T_INLINE_ELSE;
        $targets[] = T_STRING_CONCAT;
        $targets[] = T_INSTANCEOF;
        // Also register the contexts we want to specifically skip over.
        $targets[] = T_DECLARE;
        return $targets;
    }
    
    //end register()
    
    /**
     * Processes this sniff, when one of its tokens is encountered.
     *
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being checked.
     * @param int                         $stackPtr  The position of the current token in
     *                                               the stack passed in $tokens.
     *
     * @return 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.
     */
    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;
        }
        $operator = $tokens[$stackPtr]['content'];
        $checkBefore = true;
        $checkAfter = true;
        // Skip short ternary.
        if ($tokens[$stackPtr]['code'] === T_INLINE_ELSE && $tokens[$stackPtr - 1]['code'] === T_INLINE_THEN) {
            $checkBefore = false;
        }
        // Skip operator with comment on previous line.
        if ($tokens[$stackPtr - 1]['code'] === T_COMMENT && $tokens[$stackPtr - 1]['line'] < $tokens[$stackPtr]['line']) {
            $checkBefore = false;
        }
        if (isset($tokens[$stackPtr + 1]) === true) {
            // Skip short ternary.
            if ($tokens[$stackPtr]['code'] === T_INLINE_THEN && $tokens[$stackPtr + 1]['code'] === T_INLINE_ELSE) {
                $checkAfter = false;
            }
        }
        else {
            // Skip partial files.
            $checkAfter = false;
        }
        if ($checkBefore === true && $tokens[$stackPtr - 1]['code'] !== T_WHITESPACE) {
            $error = 'Expected at least 1 space before "%s"; 0 found';
            $data = [
                $operator,
            ];
            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBefore', $data);
            if ($fix === true) {
                $phpcsFile->fixer
                    ->addContentBefore($stackPtr, ' ');
            }
        }
        if ($checkAfter === true && $tokens[$stackPtr + 1]['code'] !== T_WHITESPACE) {
            $error = 'Expected at least 1 space after "%s"; 0 found';
            $data = [
                $operator,
            ];
            $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceAfter', $data);
            if ($fix === true) {
                $phpcsFile->fixer
                    ->addContent($stackPtr, ' ');
            }
        }
    }
    
    //end process()

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
OperatorSpacingSniff::$ignoreNewlines public property Allow newlines instead of spaces.
OperatorSpacingSniff::$ignoreSpacingBeforeAssignments public property Don&#039;t check spacing for assignment operators.
OperatorSpacingSniff::$nonOperandTokens private property A list of tokens that aren&#039;t considered as operands.
OperatorSpacingSniff::$supportedTokenizers public property A list of tokenizers this sniff supports.
OperatorSpacingSniff::isOperator protected function Checks if an operator is actually a different type of token in the current context.
OperatorSpacingSniff::process public function Processes this sniff, when one of its tokens is encountered. Overrides OperatorSpacingSniff::process
OperatorSpacingSniff::register public function Returns an array of tokens this test wants to listen for. Overrides OperatorSpacingSniff::register
RSS feed
Powered by Drupal