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

Breadcrumb

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

function SpaceAfterNotSniff::process

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

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

Overrides Sniff::process

File

vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/Formatting/SpaceAfterNotSniff.php, line 65

Class

SpaceAfterNotSniff

Namespace

PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting

Code

public function process(File $phpcsFile, $stackPtr) {
    $tokens = $phpcsFile->getTokens();
    $this->spacing = (int) $this->spacing;
    $pluralizeSpace = 's';
    if ($this->spacing === 1) {
        $pluralizeSpace = '';
    }
    $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true);
    if ($nextNonEmpty === false) {
        return;
    }
    if ($this->ignoreNewlines === true && $tokens[$stackPtr]['line'] !== $tokens[$nextNonEmpty]['line']) {
        return;
    }
    if ($this->spacing === 0 && $nextNonEmpty === $stackPtr + 1) {
        return;
    }
    $nextNonWhitespace = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
    if ($nextNonEmpty !== $nextNonWhitespace) {
        $error = 'Expected %s space%s after NOT operator; comment found';
        $data = [
            $this->spacing,
            $pluralizeSpace,
        ];
        $phpcsFile->addError($error, $stackPtr, 'CommentFound', $data);
        return;
    }
    $found = 0;
    if ($tokens[$stackPtr]['line'] !== $tokens[$nextNonEmpty]['line']) {
        $found = 'newline';
    }
    else {
        if ($tokens[$stackPtr + 1]['code'] === T_WHITESPACE) {
            $found = $tokens[$stackPtr + 1]['length'];
        }
    }
    if ($found === $this->spacing) {
        return;
    }
    $error = 'Expected %s space%s after NOT operator; %s found';
    $data = [
        $this->spacing,
        $pluralizeSpace,
        $found,
    ];
    $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data);
    if ($fix === true) {
        $padding = str_repeat(' ', $this->spacing);
        if ($found === 0) {
            $phpcsFile->fixer
                ->addContent($stackPtr, $padding);
        }
        else {
            $phpcsFile->fixer
                ->beginChangeset();
            $start = $stackPtr + 1;
            if ($this->spacing > 0) {
                $phpcsFile->fixer
                    ->replaceToken($start, $padding);
                ++$start;
            }
            for ($i = $start; $i < $nextNonWhitespace; $i++) {
                $phpcsFile->fixer
                    ->replaceToken($i, '');
            }
            $phpcsFile->fixer
                ->endChangeset();
        }
    }
}
RSS feed
Powered by Drupal