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

Breadcrumb

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

function UselessInheritDocCommentSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $docCommentOpenPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Commenting/UselessInheritDocCommentSniff.php, line 38

Class

UselessInheritDocCommentSniff

Namespace

SlevomatCodingStandard\Sniffs\Commenting

Code

public function process(File $phpcsFile, $docCommentOpenPointer) : void {
    $tokens = $phpcsFile->getTokens();
    $docCommentContent = '';
    for ($i = $docCommentOpenPointer + 1; $i < $tokens[$docCommentOpenPointer]['comment_closer']; $i++) {
        if (in_array($tokens[$i]['code'], [
            T_DOC_COMMENT_WHITESPACE,
            T_DOC_COMMENT_STAR,
        ], true)) {
            continue;
        }
        $docCommentContent .= $tokens[$i]['content'];
    }
    if (preg_match('~^(?:\\{@inheritDoc\\}|@inheritDoc)$~i', $docCommentContent) === 0) {
        return;
    }
    $searchPointer = $tokens[$docCommentOpenPointer]['comment_closer'] + 1;
    do {
        $docCommentOwnerPointer = TokenHelper::findNext($phpcsFile, array_merge(TokenHelper::$functionTokenCodes, TokenHelper::getTypeHintTokenCodes(), [
            T_ATTRIBUTE,
        ]), $searchPointer);
        if ($docCommentOwnerPointer === null) {
            return;
        }
        if ($tokens[$docCommentOwnerPointer]['code'] === T_ATTRIBUTE) {
            $searchPointer = $tokens[$docCommentOwnerPointer]['attribute_closer'] + 1;
            continue;
        }
        break;
    } while (true);
    if (in_array($tokens[$docCommentOwnerPointer]['code'], TokenHelper::$functionTokenCodes, true)) {
        $returnTypeHint = FunctionHelper::findReturnTypeHint($phpcsFile, $docCommentOwnerPointer);
        if ($returnTypeHint === null) {
            return;
        }
        if (TypeHintHelper::isSimpleIterableTypeHint($returnTypeHint->getTypeHintWithoutNullabilitySymbol())) {
            return;
        }
        $parametersTypeHints = FunctionHelper::getParametersTypeHints($phpcsFile, $docCommentOwnerPointer);
        foreach ($parametersTypeHints as $parameterTypeHint) {
            if ($parameterTypeHint === null) {
                return;
            }
            if (TypeHintHelper::isSimpleIterableTypeHint($parameterTypeHint->getTypeHint())) {
                return;
            }
        }
    }
    $fix = $phpcsFile->addFixableError('Useless documentation comment with @inheritDoc.', $docCommentOpenPointer, self::CODE_USELESS_INHERIT_DOC_COMMENT);
    if (!$fix) {
        return;
    }
    
    /** @var int $fixerStart */
    $fixerStart = TokenHelper::findLastTokenOnPreviousLine($phpcsFile, $docCommentOpenPointer);
    $phpcsFile->fixer
        ->beginChangeset();
    FixerHelper::removeBetweenIncluding($phpcsFile, $fixerStart, $tokens[$docCommentOpenPointer]['comment_closer']);
    $phpcsFile->fixer
        ->endChangeset();
}
RSS feed
Powered by Drupal