Skip to content

Commit

Permalink
Ignore annotation names
Browse files Browse the repository at this point in the history
fixes #1603
  • Loading branch information
gemal committed Aug 4, 2023
1 parent 4b2af2f commit cb3072a
Showing 1 changed file with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use SlevomatCodingStandard\Helpers\NamespaceHelper;
use SlevomatCodingStandard\Helpers\PhpDocParserHelper;
use SlevomatCodingStandard\Helpers\ReferencedName;
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
use SlevomatCodingStandard\Helpers\TypeHelper;
use SlevomatCodingStandard\Helpers\TypeHintHelper;
use function sprintf;
Expand All @@ -23,6 +24,9 @@ class FullyQualifiedClassNameInAnnotationSniff implements Sniff

public const CODE_NON_FULLY_QUALIFIED_CLASS_NAME = 'NonFullyQualifiedClassName';

/** @var list<string> */
public $ignoredAnnotationNames = [];

/**
* @return array<int, (int|string)>
*/
Expand Down Expand Up @@ -58,6 +62,11 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
continue;
}

$annotationName = $annotation->getName();
if (in_array($annotationName, $this->getIgnoredAnnotationNames())) {
continue;
}

$fullyQualifiedTypeHint = TypeHintHelper::getFullyQualifiedTypeHint($phpcsFile, $docCommentOpenPointer, $typeHint);
if ($fullyQualifiedTypeHint === $typeHint) {
continue;
Expand All @@ -66,7 +75,7 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
$fix = $phpcsFile->addFixableError(sprintf(
'Class name %s in %s should be referenced via a fully qualified name.',
$fullyQualifiedTypeHint,
$annotation->getName()
$annotationName
), $annotation->getStartPointer(), self::CODE_NON_FULLY_QUALIFIED_CLASS_NAME);

if (!$fix) {
Expand Down Expand Up @@ -120,7 +129,7 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
'%s name %s in %s should be referenced via a fully qualified name.',
$isClassConstant ? 'Class' : 'Constant',
$fullyQualifiedTypeHint,
$annotation->getName()
$annotationName
), $annotation->getStartPointer(), self::CODE_NON_FULLY_QUALIFIED_CLASS_NAME);

if (!$fix) {
Expand Down Expand Up @@ -158,4 +167,14 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
}
}

/**
* @return list<string>
*/
private function getIgnoredAnnotationNames(): array
{
$this->ignoredAnnotationNames = SniffSettingsHelper::normalizeArray($this->ignoredAnnotationNames);

return $this->ignoredAnnotationNames;
}

}

0 comments on commit cb3072a

Please sign in to comment.