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

Breadcrumb

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

class ForbiddenCommentsSniff

Hierarchy

  • class \SlevomatCodingStandard\Sniffs\Commenting\ForbiddenCommentsSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of ForbiddenCommentsSniff

File

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

Namespace

SlevomatCodingStandard\Sniffs\Commenting
View source
class ForbiddenCommentsSniff implements Sniff {
    public const CODE_COMMENT_FORBIDDEN = 'CommentForbidden';
    
    /** @var list<string> */
    public $forbiddenCommentPatterns = [];
    
    /**
     * @return array<int, (int|string)>
     */
    public function register() : array {
        return [
            T_DOC_COMMENT_OPEN_TAG,
        ];
    }
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $docCommentOpenPointer
     */
    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();
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ForbiddenCommentsSniff::$forbiddenCommentPatterns public property @var list&lt;string&gt;
ForbiddenCommentsSniff::CODE_COMMENT_FORBIDDEN public constant
ForbiddenCommentsSniff::process public function * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
Overrides Sniff::process
ForbiddenCommentsSniff::register public function * Overrides Sniff::register
RSS feed
Powered by Drupal