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

Breadcrumb

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

function EmptyCommentSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $commentStartPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Commenting/EmptyCommentSniff.php, line 39

Class

EmptyCommentSniff

Namespace

SlevomatCodingStandard\Sniffs\Commenting

Code

public function process(File $phpcsFile, $commentStartPointer) : void {
    $commentEndPointer = CommentHelper::getCommentEndPointer($phpcsFile, $commentStartPointer);
    if ($commentEndPointer === null) {
        // Part of block comment
        return;
    }
    $commentContent = $this->getCommentContent($phpcsFile, $commentStartPointer, $commentEndPointer);
    $isLineComment = CommentHelper::isLineComment($phpcsFile, $commentStartPointer);
    $isEmpty = $this->isEmpty($commentContent, $isLineComment);
    if (!$isEmpty) {
        return;
    }
    if ($isLineComment && $this->isPartOfMultiLineInlineComments($phpcsFile, $commentStartPointer, $commentEndPointer)) {
        return;
    }
    $fix = $phpcsFile->addFixableError('Empty comment', $commentStartPointer, self::CODE_EMPTY_COMMENT);
    if (!$fix) {
        return;
    }
    $tokens = $phpcsFile->getTokens();
    
    /** @var int $pointerBeforeWhitespaceBeforeComment */
    $pointerBeforeWhitespaceBeforeComment = TokenHelper::findPreviousNonWhitespace($phpcsFile, $commentStartPointer - 1);
    $whitespaceBeforeComment = $pointerBeforeWhitespaceBeforeComment !== $commentStartPointer - 1 ? TokenHelper::getContent($phpcsFile, $pointerBeforeWhitespaceBeforeComment + 1, $commentStartPointer - 1) : '';
    $fixedWhitespaceBeforeComment = preg_replace('~[ \\t]+$~', '', $whitespaceBeforeComment);
    $phpcsFile->fixer
        ->beginChangeset();
    FixerHelper::removeBetween($phpcsFile, $pointerBeforeWhitespaceBeforeComment, $commentStartPointer);
    $phpcsFile->fixer
        ->addContent($pointerBeforeWhitespaceBeforeComment, $fixedWhitespaceBeforeComment);
    FixerHelper::removeBetweenIncluding($phpcsFile, $commentStartPointer, $commentEndPointer);
    $whitespacePointerAfterComment = $commentEndPointer + 1;
    if ($tokens[$pointerBeforeWhitespaceBeforeComment]['line'] === $tokens[$commentStartPointer]['line']) {
        if (StringHelper::endsWith($tokens[$commentEndPointer]['content'], $phpcsFile->eolChar)) {
            $phpcsFile->fixer
                ->addNewline($commentEndPointer);
        }
    }
    elseif (array_key_exists($whitespacePointerAfterComment, $tokens) && $tokens[$whitespacePointerAfterComment]['code'] === T_WHITESPACE) {
        $fixedWhitespaceAfterComment = preg_replace('~^[ \\t]*' . $phpcsFile->eolChar . '~', '', $tokens[$whitespacePointerAfterComment]['content']);
        $phpcsFile->fixer
            ->replaceToken($whitespacePointerAfterComment, $fixedWhitespaceAfterComment);
    }
    $phpcsFile->fixer
        ->endChangeset();
}
RSS feed
Powered by Drupal