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

Breadcrumb

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

class DuplicateSpacesSniff

Hierarchy

  • class \SlevomatCodingStandard\Sniffs\Whitespaces\DuplicateSpacesSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of DuplicateSpacesSniff

File

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

Namespace

SlevomatCodingStandard\Sniffs\Whitespaces
View source
class DuplicateSpacesSniff implements Sniff {
    public const CODE_DUPLICATE_SPACES = 'DuplicateSpaces';
    
    /** @var bool */
    public $ignoreSpacesBeforeAssignment = false;
    
    /** @var bool */
    public $ignoreSpacesInAnnotation = false;
    
    /** @var bool */
    public $ignoreSpacesInComment = false;
    
    /** @var bool */
    public $ignoreSpacesInParameters = false;
    
    /** @var bool */
    public $ignoreSpacesInMatch = false;
    
    /**
     * @return array<int, (int|string)>
     */
    public function register() : array {
        return [
            T_WHITESPACE,
            T_DOC_COMMENT_WHITESPACE,
            T_DOC_COMMENT_STRING,
        ];
    }
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $whitespacePointer
     */
    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();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
DuplicateSpacesSniff::$ignoreSpacesBeforeAssignment public property @var bool
DuplicateSpacesSniff::$ignoreSpacesInAnnotation public property @var bool
DuplicateSpacesSniff::$ignoreSpacesInComment public property @var bool
DuplicateSpacesSniff::$ignoreSpacesInMatch public property @var bool
DuplicateSpacesSniff::$ignoreSpacesInParameters public property @var bool
DuplicateSpacesSniff::CODE_DUPLICATE_SPACES public constant
DuplicateSpacesSniff::process public function * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
Overrides Sniff::process
DuplicateSpacesSniff::register public function * Overrides Sniff::register
RSS feed
Powered by Drupal