forked from FiveLab/CiRules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProhibitedDocCommentsSniff.php
More file actions
62 lines (50 loc) · 1.58 KB
/
Copy pathProhibitedDocCommentsSniff.php
File metadata and controls
62 lines (50 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types = 1);
/*
* This file is part of the FiveLab CiRules package
*
* (c) FiveLab
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*/
namespace FiveLab\Component\CiRules\PhpCs\FiveLab\Sniffs\Commenting;
use FiveLab\Component\CiRules\PhpCs\FiveLab\ErrorCodes;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
class ProhibitedDocCommentsSniff implements Sniff
{
public function register(): array
{
return [
T_DOC_COMMENT_OPEN_TAG,
];
}
public function process(File $phpcsFile, mixed $stackPtr): void
{
$tokens = $phpcsFile->getTokens();
$commentToken = $tokens[$stackPtr];
$closePtr = $phpcsFile->findNext(T_DOC_COMMENT_CLOSE_TAG, $stackPtr + 1);
$possiblyDeclaration = $phpcsFile->findNext([
T_CLASS, T_INTERFACE, T_TRAIT, T_FUNCTION,
T_ABSTRACT, T_FINAL, T_ENUM,
], $commentToken['comment_closer'] + 1, local: true);
if ($possiblyDeclaration) {
return;
}
$hasMetaTag = false;
for ($i = $stackPtr + 1; $i < $closePtr; $i++) {
if ($tokens[$i]['code'] === T_DOC_COMMENT_TAG) {
$hasMetaTag = true;
break;
}
}
if (!$hasMetaTag) {
$phpcsFile->addError(
'PHPDoc must contain at least one meta tag like @param, @return, etc.',
$stackPtr,
ErrorCodes::PHPDOC_NOT_ALLOWED
);
}
}
}