-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathClassAndInterfacePHPDocFormattingSniff.php
158 lines (132 loc) · 4.7 KB
/
ClassAndInterfacePHPDocFormattingSniff.php
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* Copyright 2019 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
namespace Magento2\Sniffs\Commenting;
use Magento2\Helpers\Commenting\PHPDocFormattingValidator;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
/**
* Detects PHPDoc formatting for classes and interfaces.
*/
class ClassAndInterfacePHPDocFormattingSniff implements Sniff
{
/**
* @var PHPDocFormattingValidator
*/
private $PHPDocFormattingValidator;
/**
* @var string[] List of tags that can not be used in comments
*/
public $forbiddenTags = [
'@author',
'@category',
'@package',
'@subpackage'
];
/**
* Helper initialisation
*/
public function __construct()
{
$this->PHPDocFormattingValidator = new PHPDocFormattingValidator();
}
/**
* @inheritDoc
*/
public function register()
{
return [
T_CLASS,
T_INTERFACE
];
}
/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$namePtr = $phpcsFile->findNext(T_STRING, $stackPtr + 1, null, false, null, true);
$commentStartPtr = $this->PHPDocFormattingValidator->findPHPDoc($stackPtr, $phpcsFile);
if ($commentStartPtr === -1) {
return;
}
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
if ($this->PHPDocFormattingValidator->providesMeaning($namePtr, $commentStartPtr, $tokens) !== true) {
$fix = $phpcsFile->addFixableWarning(
sprintf(
'%s description must contain meaningful information beyond what its name provides or be removed.',
ucfirst($tokens[$stackPtr]['content'])
),
$stackPtr,
'InvalidDescription'
);
if ($fix) {
for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}
if ($tokens[$commentStartPtr - 1]['code'] === T_WHITESPACE
&& $tokens[$commentCloserPtr + 1]['code'] === T_WHITESPACE
) {
$phpcsFile->fixer->replaceToken($commentCloserPtr + 1, '');
}
}
}
if ($this->PHPDocFormattingValidator->hasDeprecatedWellFormatted($commentStartPtr, $tokens) !== true) {
$phpcsFile->addWarning(
'Motivation behind the added @deprecated tag MUST be explained. '
. '@see tag MUST be used with reference to new implementation when code is deprecated '
. 'and there is a new alternative.',
$stackPtr,
'InvalidDeprecatedTagUsage'
);
}
$this->validateTags($phpcsFile, $commentStartPtr, $tokens);
}
/**
* Validates that forbidden tags are not used in comment
*
* @param File $phpcsFile
* @param int $commentStartPtr
* @param array $tokens
* @return bool
*/
private function validateTags(File $phpcsFile, $commentStartPtr, $tokens)
{
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {
if ($tokens[$i]['code'] !== T_DOC_COMMENT_TAG) {
continue;
}
if (in_array($tokens[$i]['content'], $this->forbiddenTags) === true) {
$fix = $phpcsFile->addFixableWarning(
sprintf('Tag %s MUST NOT be used.', $tokens[$i]['content']),
$i,
'ForbiddenTags'
);
if ($fix) {
for ($j = $i - 1; $j > $commentStartPtr; $j--) {
if (!in_array($tokens[$j]['code'], [T_DOC_COMMENT_STAR, T_DOC_COMMENT_WHITESPACE], true)) {
break;
}
if ($tokens[$j]['code'] === T_DOC_COMMENT_WHITESPACE && $tokens[$j]['content'] === "\n") {
break;
}
$phpcsFile->fixer->replaceToken($j, '');
}
$phpcsFile->fixer->replaceToken($i, '');
for ($j = $i + 1; $j < $commentCloserPtr; $j++) {
$phpcsFile->fixer->replaceToken($j, '');
if ($tokens[$j]['code'] === T_DOC_COMMENT_WHITESPACE && $tokens[$j]['content'] === "\n") {
break;
}
}
}
}
}
return false;
}
}