Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore annotation names #1604

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
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 in_array;
use function sprintf;
use function strtolower;
use const T_DOC_COMMENT_OPEN_TAG;
Expand All @@ -23,6 +25,9 @@ class FullyQualifiedClassNameInAnnotationSniff implements Sniff

public const CODE_NON_FULLY_QUALIFIED_CLASS_NAME = 'NonFullyQualifiedClassName';

/** @var list<string> */
public $ignoredAnnotationNames = [];
gemal marked this conversation as resolved.
Show resolved Hide resolved

/**
* @return array<int, (int|string)>
*/
Expand All @@ -40,11 +45,14 @@ public function register(): array
public function process(File $phpcsFile, $docCommentOpenPointer): void
{
$annotations = AnnotationHelper::getAnnotations($phpcsFile, $docCommentOpenPointer);
$this->ignoredAnnotationNames = SniffSettingsHelper::normalizeArray($this->ignoredAnnotationNames);

foreach ($annotations as $annotation) {
/** @var list<IdentifierTypeNode> $identifierTypeNodes */
$identifierTypeNodes = AnnotationHelper::getAnnotationNodesByType($annotation->getNode(), IdentifierTypeNode::class);

$annotationName = $annotation->getName();

foreach ($identifierTypeNodes as $typeHintNode) {
$typeHint = $typeHintNode->name;

Expand All @@ -58,6 +66,10 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
continue;
}

if (in_array($annotationName, $this->ignoredAnnotationNames, true)) {
continue;
}

$fullyQualifiedTypeHint = TypeHintHelper::getFullyQualifiedTypeHint($phpcsFile, $docCommentOpenPointer, $typeHint);
if ($fullyQualifiedTypeHint === $typeHint) {
continue;
Expand All @@ -66,7 +78,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 +132,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
4 changes: 4 additions & 0 deletions doc/namespaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ Sniff provides the following settings:

Enforces fully qualified names of classes and interfaces in phpDocs - in annotations. This results in unambiguous phpDocs.

Sniff provides the following settings:

* `ignoredAnnotationNames`: case-sensitive list of annotation names that the sniff should ignore. Useful for custom annotation names like `@apiParam`

#### SlevomatCodingStandard.Namespaces.MultipleUsesPerLine

Prohibits multiple uses separated by commas:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,4 +528,13 @@ public function testErrors(): void
self::assertAllFixedInFile($report);
}

public function testIgnoredAnnotationNames(): void
{
$report = self::checkFile(__DIR__ . '/data/fullyQualifiedClassNameInAnnotationErrors.php', [
'ignoredAnnotationNames' => ['@return', '@param', '@var'],
]);

self::assertSame(41, $report->getErrorCount());
}

}