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

Breadcrumb

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

function ForbiddenCommentsSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $docCommentOpenPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Commenting/ForbiddenCommentsSniff.php, line 40

Class

ForbiddenCommentsSniff

Namespace

SlevomatCodingStandard\Sniffs\Commenting

Code

public function process(File $phpcsFile, $docCommentOpenPointer) : void {
    $tokens = $phpcsFile->getTokens();
    $comments = DocCommentHelper::getDocCommentDescription($phpcsFile, $docCommentOpenPointer);
    if ($comments === null) {
        return;
    }
    foreach (SniffSettingsHelper::normalizeArray($this->forbiddenCommentPatterns) as $forbiddenCommentPattern) {
        if (!SniffSettingsHelper::isValidRegularExpression($forbiddenCommentPattern)) {
            throw new Exception(sprintf('%s is not valid PCRE pattern.', $forbiddenCommentPattern));
        }
        foreach ($comments as $comment) {
            if (preg_match($forbiddenCommentPattern, $comment->getContent()) === 0) {
                continue;
            }
            $fix = $phpcsFile->addFixableError(sprintf('Documentation comment contains forbidden comment "%s".', $comment->getContent()), $comment->getPointer(), self::CODE_COMMENT_FORBIDDEN);
            if (!$fix) {
                continue;
            }
            $phpcsFile->fixer
                ->beginChangeset();
            $fixedDocComment = preg_replace($forbiddenCommentPattern, '', $comment->getContent());
            $phpcsFile->fixer
                ->replaceToken($comment->getPointer(), $fixedDocComment);
            for ($i = $comment->getPointer() - 1; $i > $docCommentOpenPointer; $i--) {
                $contentWithoutSpaces = preg_replace('~ +$~', '', $tokens[$i]['content'], -1, $replacedCount);
                if ($replacedCount === 0) {
                    break;
                }
                $phpcsFile->fixer
                    ->replaceToken($i, $contentWithoutSpaces);
            }
            $docCommentContent = '';
            for ($i = $docCommentOpenPointer + 1; $i < $tokens[$docCommentOpenPointer]['comment_closer']; $i++) {
                
                /** @var string|array<(string|int)> $token */
                $token = $phpcsFile->fixer
                    ->getTokenContent($i);
                $docCommentContent .= is_array($token) ? $token['content'] : $token;
            }
            if (preg_match('~^[\\s\\*]*$~', $docCommentContent) !== 0) {
                $pointerBeforeDocComment = $docCommentOpenPointer - 1;
                $contentBeforeWithoutSpaces = preg_replace('~[\\t ]+$~', '', $tokens[$pointerBeforeDocComment]['content'], -1, $replacedCount);
                if ($replacedCount !== 0) {
                    $phpcsFile->fixer
                        ->replaceToken($pointerBeforeDocComment, $contentBeforeWithoutSpaces);
                }
                FixerHelper::removeBetweenIncluding($phpcsFile, $docCommentOpenPointer, $tokens[$docCommentOpenPointer]['comment_closer']);
                $pointerAfterDocComment = $tokens[$docCommentOpenPointer]['comment_closer'] + 1;
                if (array_key_exists($pointerAfterDocComment, $tokens)) {
                    $contentAfterWithoutSpaces = preg_replace('~^[\\r\\n]+~', '', $tokens[$pointerAfterDocComment]['content'], -1, $replacedCount);
                    if ($replacedCount !== 0) {
                        $phpcsFile->fixer
                            ->replaceToken($pointerAfterDocComment, $contentAfterWithoutSpaces);
                    }
                }
            }
            $phpcsFile->fixer
                ->endChangeset();
        }
    }
}
RSS feed
Powered by Drupal