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

Breadcrumb

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

function RequireAttributeAfterDocCommentSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $attributeOpenerPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Attributes/RequireAttributeAfterDocCommentSniff.php, line 32

Class

RequireAttributeAfterDocCommentSniff

Namespace

SlevomatCodingStandard\Sniffs\Attributes

Code

public function process(File $phpcsFile, $attributeOpenerPointer) : void {
    if (!AttributeHelper::isValidAttribute($phpcsFile, $attributeOpenerPointer)) {
        return;
    }
    $tokens = $phpcsFile->getTokens();
    $docCommentOpenerPointer = TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $tokens[$attributeOpenerPointer]['attribute_closer'] + 1);
    if ($tokens[$docCommentOpenerPointer]['code'] !== T_DOC_COMMENT_OPEN_TAG) {
        return;
    }
    $docCommentStartPointer = TokenHelper::findFirstTokenOnLine($phpcsFile, $docCommentOpenerPointer);
    $docCommentEndPointer = TokenHelper::findLastTokenOnLine($phpcsFile, $tokens[$docCommentOpenerPointer]['comment_closer']);
    $docComment = TokenHelper::getContent($phpcsFile, $docCommentStartPointer, $docCommentEndPointer);
    $firstAttributeOpenerPointer = $attributeOpenerPointer;
    do {
        $nonWhitespacePointerBefore = TokenHelper::findPreviousNonWhitespace($phpcsFile, $firstAttributeOpenerPointer - 1);
        if ($tokens[$nonWhitespacePointerBefore]['code'] !== T_ATTRIBUTE_END) {
            break;
        }
        $firstAttributeOpenerPointer = $tokens[$nonWhitespacePointerBefore]['attribute_opener'];
    } while (true);
    $attributeStartPointer = TokenHelper::findFirstTokenOnLine($phpcsFile, $firstAttributeOpenerPointer);
    $fix = $phpcsFile->addFixableError('Attribute should be placed after documentation comment.', $attributeOpenerPointer, self::CODE_ATTRIBUTE_BEFORE_DOC_COMMENT);
    if (!$fix) {
        return;
    }
    $phpcsFile->fixer
        ->beginChangeset();
    $phpcsFile->fixer
        ->addContentBefore($attributeStartPointer, $docComment);
    FixerHelper::removeBetweenIncluding($phpcsFile, $docCommentStartPointer, $docCommentEndPointer);
    $phpcsFile->fixer
        ->endChangeset();
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal