Skip to content

Commit 43497d2

Browse files
committed
Add checks for forbidden tasks
1 parent 949b5e5 commit 43497d2

4 files changed

+77
-3
lines changed

Magento2/Sniffs/Commenting/ClassAndInterfacePHPDocFormattingSniff.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ class ClassAndInterfacePHPDocFormattingSniff implements Sniff
1919
*/
2020
private $PHPDocFormattingValidator;
2121

22+
/**
23+
* @var string[] List of tags that can not be used in comments
24+
*/
25+
private $forbiddenTags = [
26+
'@author',
27+
'@category',
28+
'@package',
29+
'@subpackage'
30+
];
31+
2232
/**
2333
* Helper initialisation
2434
*/
@@ -75,5 +85,35 @@ public function process(File $phpcsFile, $stackPtr)
7585
);
7686
}
7787

88+
$this->validateTags($phpcsFile, $commentStartPtr, $tokens);
89+
}
90+
91+
/**
92+
* Validates that forbidden tags are not used in comment
93+
*
94+
* @param File $phpcsFile
95+
* @param int $commentStartPtr
96+
* @param array $tokens
97+
* @return bool
98+
*/
99+
private function validateTags(File $phpcsFile, $commentStartPtr, $tokens)
100+
{
101+
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
102+
103+
for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {
104+
if ($tokens[$i]['code'] !== T_DOC_COMMENT_TAG) {
105+
continue;
106+
}
107+
108+
if (in_array($tokens[$i]['content'], $this->forbiddenTags) === true) {
109+
$phpcsFile->addWarning(
110+
sprintf('Tag %s MUST NOT be used.', $tokens[$i]['content']),
111+
$i,
112+
'ForbiddenTags'
113+
);
114+
}
115+
}
116+
117+
return false;
78118
}
79119
}

Magento2/Tests/Commenting/ClassAndInterfacePHPDocFormattingUnitTest.1.inc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class YetAnotherHandler
3939

4040
/**
4141
* GreenHandler
42-
* @package NotFromArroundHere
42+
* @api Do not confuse tag for faulty short description
4343
*/
4444
class GreenHandler
4545
{
@@ -52,4 +52,19 @@ class GreenHandler
5252
class EmptyHandler
5353
{
5454

55+
}
56+
57+
/**
58+
* Handler for PHP errors/warnings/notices that converts them to exceptions.
59+
*
60+
* @api is ok here
61+
* @deprecated can be used in this context
62+
* @author must not be mentioned
63+
* @category is irrelevant
64+
* @package is not ment to be used
65+
* @subpackage does not belong here
66+
*/
67+
class ExampleHandler
68+
{
69+
5570
}

Magento2/Tests/Commenting/ClassAndInterfacePHPDocFormattingUnitTest.2.inc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ interface YetAnotherHandler
3939

4040
/**
4141
* GreenHandler
42-
* @package NotFromArroundHere
42+
* @api Do not confuse tag for faulty short description
4343
*/
4444
interface GreenHandler
4545
{
@@ -52,4 +52,19 @@ interface GreenHandler
5252
interface EmptyHandler
5353
{
5454

55+
}
56+
57+
/**
58+
* Handler for PHP errors/warnings/notices that converts them to exceptions.
59+
*
60+
* @api is ok here
61+
* @deprecated can be used in this context
62+
* @author must not be mentioned
63+
* @category is irrelevant
64+
* @package is not ment to be used
65+
* @subpackage does not belong here
66+
*/
67+
interface ExampleHandler
68+
{
69+
5570
}

Magento2/Tests/Commenting/ClassAndInterfacePHPDocFormattingUnitTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ public function getWarningList($testFile = '')
3131
27 => 1,
3232
35 => 1,
3333
44 => 1,
34-
52 => 1
34+
52 => 1,
35+
62 => 1,
36+
63 => 1,
37+
64 => 1,
38+
65 => 1,
3539
];
3640
}
3741
}

0 commit comments

Comments
 (0)