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

Breadcrumb

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

function AnnotationNameSniff::process

* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

Parameters

int $docCommentOpenPointer:

Overrides Sniff::process

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Commenting/AnnotationNameSniff.php, line 169

Class

AnnotationNameSniff

Namespace

SlevomatCodingStandard\Sniffs\Commenting

Code

public function process(File $phpcsFile, $docCommentOpenPointer) : void {
    $annotations = AnnotationHelper::getAnnotations($phpcsFile, $docCommentOpenPointer);
    $correctAnnotationNames = $this->getNormalizedAnnotationNames();
    foreach ($annotations as $annotation) {
        $lowerCasedAnnotationName = strtolower($annotation->getName());
        if (!array_key_exists($lowerCasedAnnotationName, $correctAnnotationNames)) {
            continue;
        }
        $correctAnnotationName = $correctAnnotationNames[$lowerCasedAnnotationName];
        if ($correctAnnotationName === $annotation->getName()) {
            continue;
        }
        $annotationNameWithoutAtSign = ltrim($annotation->getName(), '@');
        $fullyQualifiedAnnotationName = NamespaceHelper::resolveClassName($phpcsFile, $annotationNameWithoutAtSign, $annotation->getStartPointer());
        if (NamespaceHelper::normalizeToCanonicalName($fullyQualifiedAnnotationName) !== $annotationNameWithoutAtSign) {
            continue;
        }
        $fix = $phpcsFile->addFixableError(sprintf('Annotation name is incorrect. Expected %s, found %s.', $correctAnnotationName, $annotation->getName()), $annotation->getStartPointer(), self::CODE_ANNOTATION_NAME_INCORRECT);
        if (!$fix) {
            continue;
        }
        $phpcsFile->fixer
            ->beginChangeset();
        $phpcsFile->fixer
            ->replaceToken($annotation->getStartPointer(), $correctAnnotationName);
        $phpcsFile->fixer
            ->endChangeset();
    }
    $tokens = $phpcsFile->getTokens();
    $docCommentContent = TokenHelper::getContent($phpcsFile, $docCommentOpenPointer, $tokens[$docCommentOpenPointer]['comment_closer']);
    if (preg_match_all('~\\{(' . implode('|', $correctAnnotationNames) . ')\\}~i', $docCommentContent, $matches, PREG_OFFSET_CAPTURE) === 0) {
        return;
    }
    foreach ($matches[1] as $match) {
        $correctAnnotationName = $correctAnnotationNames[strtolower($match[0])];
        if ($correctAnnotationName === $match[0]) {
            continue;
        }
        $fix = $phpcsFile->addFixableError(sprintf('Annotation name is incorrect. Expected %s, found %s.', $correctAnnotationName, $match[0]), $docCommentOpenPointer, self::CODE_ANNOTATION_NAME_INCORRECT);
        if (!$fix) {
            continue;
        }
        $phpcsFile->fixer
            ->beginChangeset();
        $fixedDocCommentContent = substr($docCommentContent, 0, $match[1]) . $correctAnnotationName . substr($docCommentContent, $match[1] + strlen($match[0]));
        FixerHelper::change($phpcsFile, $docCommentOpenPointer, $tokens[$docCommentOpenPointer]['comment_closer'], $fixedDocCommentContent);
        $phpcsFile->fixer
            ->endChangeset();
    }
}

API Navigation

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