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

Breadcrumb

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

function DuplicateSpacesSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $whitespacePointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Whitespaces/DuplicateSpacesSniff.php, line 65

Class

DuplicateSpacesSniff

Namespace

SlevomatCodingStandard\Sniffs\Whitespaces

Code

public function process(File $phpcsFile, $whitespacePointer) : void {
    $tokens = $phpcsFile->getTokens();
    if ($tokens[$whitespacePointer]['column'] === 1) {
        return;
    }
    $content = $tokens[$whitespacePointer]['content'];
    if ($content === $phpcsFile->eolChar) {
        return;
    }
    if ($tokens[$whitespacePointer]['code'] === T_WHITESPACE) {
        if ($this->ignoreSpacesBeforeAssignment) {
            $pointerAfter = TokenHelper::findNextNonWhitespace($phpcsFile, $whitespacePointer + 1);
            if ($pointerAfter !== null && in_array($tokens[$pointerAfter]['code'], Tokens::$assignmentTokens, true)) {
                return;
            }
        }
        if ($this->ignoreSpacesInParameters) {
            $pointerAfter = TokenHelper::findNextNonWhitespace($phpcsFile, $whitespacePointer + 1);
            if ($pointerAfter !== null && $tokens[$pointerAfter]['code'] === T_VARIABLE && ParameterHelper::isParameter($phpcsFile, $pointerAfter)) {
                return;
            }
        }
        if ($this->ignoreSpacesInMatch) {
            $pointerAfter = TokenHelper::findNextNonWhitespace($phpcsFile, $whitespacePointer + 1);
            if ($pointerAfter !== null && $tokens[$pointerAfter]['code'] === T_MATCH_ARROW) {
                return;
            }
        }
    }
    else {
        if ($this->ignoreSpacesInComment) {
            return;
        }
        if ($tokens[$whitespacePointer - 1]['code'] === T_DOC_COMMENT_STAR && $tokens[$whitespacePointer + 1]['code'] === T_DOC_COMMENT_STRING) {
            return;
        }
        if ($this->ignoreSpacesInAnnotation) {
            $pointerBefore = TokenHelper::findPrevious($phpcsFile, [
                T_DOC_COMMENT_OPEN_TAG,
                T_DOC_COMMENT_TAG,
            ], $whitespacePointer - 1);
            if ($pointerBefore !== null && $tokens[$pointerBefore]['code'] === T_DOC_COMMENT_TAG && $tokens[$whitespacePointer + 1]['code'] !== T_DOC_COMMENT_CLOSE_TAG) {
                return;
            }
        }
    }
    $matchResult = preg_match_all('~ {2,}~', $content, $matches, PREG_OFFSET_CAPTURE);
    if ($matchResult === false || $matchResult === 0) {
        return;
    }
    $tabWidth = $phpcsFile->config->tabWidth;
    $fix = false;
    foreach ($matches[0] as [
        $match,
        $offset,
    ]) {
        $firstPointerOnLine = TokenHelper::findFirstNonWhitespaceOnLine($phpcsFile, $whitespacePointer - 1);
        $indentation = IndentationHelper::getIndentation($phpcsFile, $firstPointerOnLine);
        $indentationWithoutTabs = str_replace(IndentationHelper::TAB_INDENT, $tabWidth === 0 ? IndentationHelper::SPACES_INDENT : str_repeat(' ', $tabWidth), $indentation);
        $position = $tokens[$whitespacePointer]['column'] + $offset - strlen($indentation) + strlen($indentationWithoutTabs);
        $fixable = $phpcsFile->addFixableError(sprintf('Duplicate spaces at position %d.', $position), $whitespacePointer, self::CODE_DUPLICATE_SPACES);
        if ($fixable) {
            $fix = true;
        }
    }
    if (!$fix) {
        return;
    }
    $phpcsFile->fixer
        ->beginChangeset();
    $phpcsFile->fixer
        ->replaceToken($whitespacePointer, preg_replace('~ {2,}~', ' ', $content));
    $phpcsFile->fixer
        ->endChangeset();
}

API Navigation

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