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

Breadcrumb

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

function InlineDocCommentDeclarationSniff::checkFormat

*

Parameters

list<Annotation<VarTagValueNode>> $annotations:

1 call to InlineDocCommentDeclarationSniff::checkFormat()
InlineDocCommentDeclarationSniff::process in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Commenting/InlineDocCommentDeclarationSniff.php
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Commenting/InlineDocCommentDeclarationSniff.php, line 160

Class

InlineDocCommentDeclarationSniff

Namespace

SlevomatCodingStandard\Sniffs\Commenting

Code

private function checkFormat(File $phpcsFile, array $annotations) : void {
    foreach ($annotations as $annotation) {
        if (!$annotation->isInvalid() && $annotation->getValue()->variableName !== '') {
            continue;
        }
        $variableName = '$variableName';
        $annotationContent = (string) $annotation->getValue();
        $type = null;
        if ($annotationContent !== '' && preg_match('~(\\$\\w+)(?:\\s+(.+))?$~i', $annotationContent, $matches) === 1) {
            $variableName = $matches[1];
            $type = $matches[2] ?? null;
        }
        // It may be description when it contains whitespaces
        $isFixable = $type !== null && preg_match('~\\s~', $type) === 0;
        if (!$isFixable) {
            $phpcsFile->addError(sprintf('Invalid inline documentation comment format "@var %1$s", expected "@var type %2$s Optional description".', $annotationContent, $variableName), $annotation->getStartPointer(), self::CODE_INVALID_FORMAT);
            continue;
        }
        $fix = $phpcsFile->addFixableError(sprintf('Invalid inline documentation comment format "@var %1$s", expected "@var %2$s %3$s".', $annotationContent, $type, $variableName), $annotation->getStartPointer(), self::CODE_INVALID_FORMAT);
        if (!$fix) {
            continue;
        }
        $phpcsFile->fixer
            ->beginChangeset();
        $phpcsFile->fixer
            ->addContent($annotation->getStartPointer(), sprintf(' %s %s ', $type, $variableName));
        FixerHelper::removeBetweenIncluding($phpcsFile, $annotation->getStartPointer() + 1, $annotation->getEndPointer());
        $phpcsFile->fixer
            ->endChangeset();
    }
}

API Navigation

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