Skip to content

Commit 07f00b3

Browse files
committed
Squiz/VariableComment: make errorcode for "TagNotAllowed" warning modular
As things are, the `VariableComment` sniff only allows `@see` tags and `@var` tags in property docblocks. This practice feels dated to me, as commonly used tags, such as `@link`, `@deprecated` etc are not allowed. This commit replaces the generic `TagNotAllowed` error code in the sniff with a more modular `[TagName]TagNotAllowed` error code. This allows for rulesets to selectively ignore the `TagNotAllowed` error code for specific tags the ruleset maintainer wants to allow for the code base under scan. Includes tests.
1 parent 66c2064 commit 07f00b3

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ public function processMemberVar(File $phpcsFile, $stackPtr)
111111
} else {
112112
$error = '%s tag is not allowed in member variable comment';
113113
$data = [$tokens[$tag]['content']];
114-
$phpcsFile->addWarning($error, $tag, 'TagNotAllowed', $data);
114+
$code = ucwords(ltrim($tokens[$tag]['content'], '@')).'TagNotAllowed';
115+
$phpcsFile->addWarning($error, $tag, $code, $data);
115116
}//end if
116117
}//end foreach
117118

src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.inc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,26 @@ class PHP84FinalProperties {
476476
*/
477477
final int $hasDocblock;
478478
}
479+
480+
// phpcs:disable Squiz.Commenting.VariableComment.SinceTagNotAllowed
481+
// phpcs:disable Squiz.Commenting.VariableComment.DeprecatedTagNotAllowed
482+
class AllowMoreForSelectivelyIgnoringDisallowedTags {
483+
/**
484+
* @var string
485+
*
486+
* @since 3.1
487+
* @deprecated 10.5
488+
*/
489+
public string $propertyWithExtraTags;
490+
491+
/**
492+
* @var string
493+
*
494+
* @link https://example.org/some-link
495+
* @api
496+
* @param string This tag is not allowed.
497+
*/
498+
public $theAboveExtraTagsAreStillNotAllowed;
499+
}
500+
501+
// phpcs:enable Squiz.Commenting.VariableComment

src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.inc.fixed

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,26 @@ class PHP84FinalProperties {
476476
*/
477477
final int $hasDocblock;
478478
}
479+
480+
// phpcs:disable Squiz.Commenting.VariableComment.SinceTagNotAllowed
481+
// phpcs:disable Squiz.Commenting.VariableComment.DeprecatedTagNotAllowed
482+
class AllowMoreForSelectivelyIgnoringDisallowedTags {
483+
/**
484+
* @var string
485+
*
486+
* @since 3.1
487+
* @deprecated 10.5
488+
*/
489+
public string $propertyWithExtraTags;
490+
491+
/**
492+
* @var string
493+
*
494+
* @link https://example.org/some-link
495+
* @api
496+
* @param string This tag is not allowed.
497+
*/
498+
public $theAboveExtraTagsAreStillNotAllowed;
499+
}
500+
501+
// phpcs:enable Squiz.Commenting.VariableComment

src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ public function getErrorList()
8181
*/
8282
public function getWarningList()
8383
{
84-
return [93 => 1];
84+
return [
85+
93 => 1,
86+
494 => 1,
87+
495 => 1,
88+
496 => 1,
89+
];
8590

8691
}//end getWarningList()
8792

0 commit comments

Comments
 (0)