-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathClassPropertyPHPDocFormattingSniff.php
266 lines (239 loc) · 7.8 KB
/
ClassPropertyPHPDocFormattingSniff.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento2\Sniffs\Commenting;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
use Magento2\Helpers\Commenting\PHPDocFormattingValidator;
/**
* Class ClassPropertyPHPDocFormattingSniff
*/
class ClassPropertyPHPDocFormattingSniff extends AbstractVariableSniff
{
/**
* @var array
*/
private const TOKENS_ALLOWED_BETWEEN_PHPDOC_AND_PROPERTY_NAME = [
T_PUBLIC,
T_PRIVATE,
T_PROTECTED,
T_VAR,
T_STATIC,
T_WHITESPACE,
T_NS_SEPARATOR,
T_STRING,
T_COMMENT,
T_NULLABLE
];
/**
* @var PHPDocFormattingValidator
*/
private $PHPDocFormattingValidator;
/**
* @var array
*/
private $invalidTypes = [
'null',
'false',
'true',
'self'
];
/**
* Constructs an ClassPropertyPHPDocFormattingSniff.
*/
public function __construct()
{
$this->PHPDocFormattingValidator = new PHPDocFormattingValidator();
parent::__construct();
}
/**
* @inheritDoc
*/
public function processMemberVar(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$commentEnd = $phpcsFile->findPrevious(
self::TOKENS_ALLOWED_BETWEEN_PHPDOC_AND_PROPERTY_NAME,
$stackPtr - 1,
null,
true
);
if ($commentEnd === false || $tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) {
// if the property has a valid type definition, we don't require a doc block
if (!$this->hasValidType($phpcsFile, $stackPtr)) {
$phpcsFile->addWarning(
'Missing PHP DocBlock for class property without valid type.',
$stackPtr,
'Missing'
);
}
// no comment, so nothing further to process
return;
}
$commentStart = $tokens[$commentEnd]['comment_opener'];
if ($this->PHPDocFormattingValidator->hasDeprecatedWellFormatted($commentStart, $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'
);
}
$varAnnotationPosition = null;
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
if ($tokens[$tag]['content'] === '@var') {
if ($varAnnotationPosition !== null) {
$phpcsFile->addWarning(
'Only one @var tag is allowed for class property declaration.',
$tag,
'DuplicateVar'
);
} else {
$varAnnotationPosition = $tag;
}
}
}
if ($varAnnotationPosition === null) {
$phpcsFile->addWarning(
'Class properties must have type declaration using @var tag.',
$stackPtr,
'MissingVar'
);
return;
}
$string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $varAnnotationPosition, $commentEnd);
if ($string === false || $tokens[$string]['line'] !== $tokens[$varAnnotationPosition]['line']) {
$phpcsFile->addWarning(
'Content missing for @var tag in class property declaration.',
$varAnnotationPosition,
'EmptyVar'
);
return;
}
// Check if class has already have meaningful description after @var tag
$shortDescriptionAfterVarPosition = $phpcsFile->findNext(
T_DOC_COMMENT_STRING,
$varAnnotationPosition + 4,
$commentEnd,
false,
null,
false
);
if ($this->PHPDocFormattingValidator->providesMeaning(
$shortDescriptionAfterVarPosition,
$commentStart,
$tokens
) !== true) {
preg_match(
'`^((?:\|?(?:array\([^\)]*\)|[\\\\\[\]]+))*)( .*)?`i',
$tokens[($varAnnotationPosition + 2)]['content'],
$varParts
);
if ($varParts[1]) {
return;
}
$phpcsFile->addWarning(
'Short description must be before @var tag.',
$shortDescriptionAfterVarPosition,
'ShortDescriptionAfterVar'
);
}
$this->processPropertyShortDescription($phpcsFile, $stackPtr, $varAnnotationPosition, $commentStart);
}
/**
* Check if class property has a valid (not specifically invalid) type
*
* @param File $phpcsFile
* @param int $propertyPosition
* @return bool
*/
private function hasValidType(File $phpcsFile, int $propertyPosition): bool
{
// type token should be 2 before property. If not present, visibility token should be in its place
$typePosition = $phpcsFile->findPrevious(
[T_STRING],
$propertyPosition,
$propertyPosition - 2
);
if (!$typePosition) {
return false;
}
$type = $phpcsFile->getTokensAsString($typePosition, 1);
return !in_array(strtolower($type), $this->invalidTypes);
}
/**
* Check if class has already have meaningful description before var tag
*
* @param File $phpcsFile
* @param int $propertyNamePosition
* @param int $varAnnotationPosition
* @param int $commentStart
*/
private function processPropertyShortDescription(
File $phpcsFile,
int $propertyNamePosition,
int $varAnnotationPosition,
int $commentStart
): void {
$propertyShortDescriptionPosition = $phpcsFile->findPrevious(
T_DOC_COMMENT_STRING,
$varAnnotationPosition,
$commentStart,
false,
null,
false
);
if ($propertyShortDescriptionPosition === false) {
return;
}
$tokens = $phpcsFile->getTokens();
$propertyName = trim($tokens[$propertyNamePosition]['content'], '$');
$shortDescription = strtolower($tokens[$propertyShortDescriptionPosition]['content']);
if ($this->isDuplicate($propertyName, $shortDescription)) {
$phpcsFile->addWarning(
'Short description duplicates class property name.',
$propertyShortDescriptionPosition,
'AlreadyHaveMeaningfulNameVar'
);
}
}
/**
* Does short description duplicate the property name
*
* @param string $propertyName
* @param string $shortDescription
* @return bool
*/
private function isDuplicate(string $propertyName, string $shortDescription): bool
{
return $this->clean($propertyName) === $this->clean($shortDescription);
}
/**
* Return only A-Za-z characters converted to lowercase from the string
*
* @param string $string
* @return string
*/
private function clean(string $string): string
{
return strtolower(preg_replace('/[^A-Za-z]/', '', $string));
}
/**
* @inheritDoc
* phpcs:disable Magento2.CodeAnalysis.EmptyBlock
*/
protected function processVariable(File $phpcsFile, $stackPtr)
{
}
/**
* @inheritDoc
* phpcs:disable Magento2.CodeAnalysis.EmptyBlock
*/
protected function processVariableInString(File $phpcsFile, $stackPtr)
{
}
}