|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CodingStandard_Sniffs_Commenting_InlineCommentSniff. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category PHP |
|
8
|
|
|
* @package PHP_CodeSniffer |
|
9
|
|
|
* @author Greg Sherwood <[email protected]> |
|
10
|
|
|
* @author Marc McIntyre <[email protected]> |
|
11
|
|
|
* @author Alexander Obuhovich <[email protected]> |
|
12
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
|
13
|
|
|
* @link https://github.com/aik099/CodingStandard |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace CodingStandard\Sniffs\Commenting; |
|
17
|
|
|
|
|
18
|
|
|
use PHP_CodeSniffer\Files\File; |
|
19
|
|
|
use PHP_CodeSniffer\Sniffs\Sniff; |
|
20
|
|
|
use PHP_CodeSniffer\Util\Tokens; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* CodingStandard_Sniffs_Commenting_InlineCommentSniff. |
|
24
|
|
|
* |
|
25
|
|
|
* Checks that there is adequate spacing between comments. |
|
26
|
|
|
* |
|
27
|
|
|
* @category PHP |
|
28
|
|
|
* @package PHP_CodeSniffer |
|
29
|
|
|
* @author Greg Sherwood <[email protected]> |
|
30
|
|
|
* @author Marc McIntyre <[email protected]> |
|
31
|
|
|
* @author Alexander Obuhovich <[email protected]> |
|
32
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
|
33
|
|
|
* @link https://github.com/aik099/CodingStandard |
|
34
|
|
|
*/ |
|
35
|
|
|
class InlineCommentSniff implements Sniff |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* A list of tokenizers this sniff supports. |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
public $supportedTokenizers = array( |
|
44
|
|
|
'PHP', |
|
45
|
|
|
'JS', |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Returns an array of tokens this test wants to listen for. |
|
51
|
|
|
* |
|
52
|
|
|
* @return integer[] |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function register() |
|
55
|
|
|
{ |
|
56
|
|
|
return array( |
|
57
|
1 |
|
T_COMMENT, |
|
58
|
1 |
|
T_DOC_COMMENT_OPEN_TAG, |
|
59
|
1 |
|
); |
|
60
|
|
|
}//end register() |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Processes this test, when one of its tokens is encountered. |
|
65
|
|
|
* |
|
66
|
|
|
* @param File $phpcsFile The file being scanned. |
|
67
|
|
|
* @param int $stackPtr The position of the current token in the |
|
68
|
|
|
* stack passed in $tokens. |
|
69
|
|
|
* |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function process(File $phpcsFile, $stackPtr) |
|
73
|
|
|
{ |
|
74
|
1 |
|
$tokens = $phpcsFile->getTokens(); |
|
75
|
|
|
|
|
76
|
|
|
// If this is a function/class/interface doc block comment, skip it. |
|
77
|
|
|
// We are only interested in inline doc block comments, which are |
|
78
|
|
|
// not allowed. |
|
79
|
1 |
|
if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_OPEN_TAG) { |
|
80
|
1 |
|
$nextToken = $phpcsFile->findNext( |
|
81
|
1 |
|
Tokens::$emptyTokens, |
|
82
|
1 |
|
($stackPtr + 1), |
|
83
|
1 |
|
null, |
|
84
|
|
|
true |
|
85
|
1 |
|
); |
|
86
|
|
|
|
|
87
|
|
|
$ignore = array( |
|
88
|
1 |
|
T_CLASS, |
|
89
|
1 |
|
T_INTERFACE, |
|
90
|
1 |
|
T_TRAIT, |
|
91
|
1 |
|
T_FUNCTION, |
|
92
|
1 |
|
T_CLOSURE, |
|
93
|
1 |
|
T_PUBLIC, |
|
94
|
1 |
|
T_PRIVATE, |
|
95
|
1 |
|
T_PROTECTED, |
|
96
|
1 |
|
T_FINAL, |
|
97
|
1 |
|
T_STATIC, |
|
98
|
1 |
|
T_ABSTRACT, |
|
99
|
1 |
|
T_CONST, |
|
100
|
1 |
|
T_PROPERTY, |
|
101
|
1 |
|
); |
|
102
|
|
|
|
|
103
|
1 |
|
if (in_array($tokens[$nextToken]['code'], $ignore) === true) { |
|
104
|
1 |
|
return; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
if ($phpcsFile->tokenizerType === 'JS') { |
|
108
|
|
|
// We allow block comments if a function or object |
|
109
|
|
|
// is being assigned to a variable. |
|
110
|
1 |
|
$ignore = Tokens::$emptyTokens; |
|
111
|
1 |
|
$ignore[] = T_EQUAL; |
|
112
|
1 |
|
$ignore[] = T_STRING; |
|
113
|
1 |
|
$ignore[] = T_OBJECT_OPERATOR; |
|
114
|
1 |
|
$nextToken = $phpcsFile->findNext($ignore, ($nextToken + 1), null, true); |
|
115
|
1 |
|
if ($tokens[$nextToken]['code'] === T_FUNCTION |
|
116
|
1 |
|
|| $tokens[$nextToken]['code'] === T_CLOSURE |
|
117
|
1 |
|
|| $tokens[$nextToken]['code'] === T_OBJECT |
|
118
|
1 |
|
|| $tokens[$nextToken]['code'] === T_PROTOTYPE |
|
119
|
1 |
|
) { |
|
120
|
1 |
|
return; |
|
121
|
|
|
} |
|
122
|
1 |
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
$prevToken = $phpcsFile->findPrevious( |
|
125
|
1 |
|
Tokens::$emptyTokens, |
|
126
|
1 |
|
($stackPtr - 1), |
|
127
|
1 |
|
null, |
|
128
|
|
|
true |
|
129
|
1 |
|
); |
|
130
|
|
|
|
|
131
|
1 |
|
if ($tokens[$prevToken]['code'] === T_OPEN_TAG) { |
|
132
|
|
|
return; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
// Only error once per comment. |
|
136
|
1 |
|
if ($tokens[$stackPtr]['content'] === '/**') { |
|
137
|
1 |
|
$commentEnd = $phpcsFile->findNext(T_DOC_COMMENT_CLOSE_TAG, ($stackPtr + 1)); |
|
138
|
1 |
|
$commentText = $phpcsFile->getTokensAsString($stackPtr, (($commentEnd - $stackPtr) + 1)); |
|
139
|
|
|
|
|
140
|
1 |
|
if (strpos($commentText, '@var') === false && strpos($commentText, '@type') === false) { |
|
141
|
1 |
|
$error = 'Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead'; |
|
142
|
1 |
|
$phpcsFile->addError($error, $stackPtr, 'DocBlock'); |
|
143
|
1 |
|
} |
|
144
|
1 |
|
} |
|
145
|
1 |
|
}//end if |
|
146
|
|
|
|
|
147
|
1 |
|
if ($tokens[$stackPtr]['content']{0} === '#') { |
|
148
|
1 |
|
$error = 'Perl-style comments are not allowed; use "// Comment" instead'; |
|
149
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'WrongStyle'); |
|
150
|
1 |
|
if ($fix === true) { |
|
151
|
1 |
|
$comment = ltrim($tokens[$stackPtr]['content'], "# \t"); |
|
152
|
1 |
|
$phpcsFile->fixer->replaceToken($stackPtr, "// $comment"); |
|
153
|
1 |
|
} |
|
154
|
1 |
|
} |
|
155
|
|
|
|
|
156
|
|
|
// We don't want: |
|
157
|
|
|
// - end of block comments, if the last comment is a closing curly brace |
|
158
|
|
|
// - closing comment of previous control structure (see "WhiteSpace.ControlStructureSpacing") |
|
159
|
1 |
|
$previousContent = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
|
160
|
1 |
|
if ($tokens[$previousContent]['line'] === $tokens[$stackPtr]['line'] |
|
161
|
1 |
|
|| $tokens[$previousContent]['line'] === ($tokens[$stackPtr]['line'] - 1) |
|
162
|
1 |
|
) { |
|
163
|
1 |
|
if ($tokens[$previousContent]['code'] === T_CLOSE_CURLY_BRACKET) { |
|
164
|
1 |
|
return; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
// Special case for JS files. |
|
168
|
1 |
|
if ($tokens[$previousContent]['code'] === T_COMMA |
|
169
|
1 |
|
|| $tokens[$previousContent]['code'] === T_SEMICOLON |
|
170
|
1 |
|
) { |
|
171
|
1 |
|
$lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($previousContent - 1), null, true); |
|
172
|
1 |
|
if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) { |
|
173
|
1 |
|
return; |
|
174
|
|
|
} |
|
175
|
1 |
|
} |
|
176
|
1 |
|
} |
|
177
|
|
|
|
|
178
|
1 |
|
$comment = rtrim($tokens[$stackPtr]['content']); |
|
179
|
|
|
|
|
180
|
|
|
// Only want inline comments. |
|
181
|
1 |
|
if (substr($comment, 0, 2) !== '//') { |
|
182
|
1 |
|
return; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
1 |
|
if (trim(substr($comment, 2)) !== '') { |
|
186
|
1 |
|
$spaceCount = 0; |
|
187
|
1 |
|
$tabFound = false; |
|
188
|
|
|
|
|
189
|
1 |
|
$commentLength = strlen($comment); |
|
190
|
1 |
|
for ($i = 2; $i < $commentLength; $i++) { |
|
191
|
1 |
|
if ($comment[$i] === "\t") { |
|
192
|
1 |
|
$tabFound = true; |
|
193
|
1 |
|
break; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
1 |
|
if ($comment[$i] !== ' ') { |
|
197
|
1 |
|
break; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
1 |
|
$spaceCount++; |
|
201
|
1 |
|
} |
|
202
|
|
|
|
|
203
|
1 |
|
$fix = false; |
|
204
|
1 |
|
if ($tabFound === true) { |
|
205
|
1 |
|
$error = 'Tab found before comment text; expected "// %s" but found "%s"'; |
|
206
|
|
|
$data = array( |
|
207
|
1 |
|
ltrim(substr($comment, 2)), |
|
208
|
1 |
|
$comment, |
|
209
|
1 |
|
); |
|
210
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'TabBefore', $data); |
|
211
|
1 |
|
} elseif ($spaceCount === 0) { |
|
212
|
1 |
|
$error = 'No space found before comment text; expected "// %s" but found "%s"'; |
|
213
|
|
|
$data = array( |
|
214
|
1 |
|
substr($comment, 2), |
|
215
|
1 |
|
$comment, |
|
216
|
1 |
|
); |
|
217
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBefore', $data); |
|
218
|
1 |
|
} elseif ($spaceCount > 1) { |
|
219
|
1 |
|
$error = 'Expected 1 space before comment text but found %s; use block comment if you need indentation'; |
|
220
|
|
|
$data = array( |
|
221
|
1 |
|
$spaceCount, |
|
222
|
1 |
|
substr($comment, (2 + $spaceCount)), |
|
223
|
1 |
|
$comment, |
|
224
|
1 |
|
); |
|
225
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBefore', $data); |
|
226
|
1 |
|
}//end if |
|
227
|
|
|
|
|
228
|
1 |
|
if ($fix === true) { |
|
229
|
1 |
|
$newComment = '// '.ltrim($tokens[$stackPtr]['content'], "/\t "); |
|
230
|
1 |
|
$phpcsFile->fixer->replaceToken($stackPtr, $newComment); |
|
231
|
1 |
|
} |
|
232
|
1 |
|
}//end if |
|
233
|
|
|
|
|
234
|
|
|
// The below section determines if a comment block is correctly capitalised, |
|
235
|
|
|
// and ends in a full-stop. It will find the last comment in a block, and |
|
236
|
|
|
// work its way up. |
|
237
|
1 |
|
$nextComment = $phpcsFile->findNext(array(T_COMMENT), ($stackPtr + 1), null, false); |
|
238
|
|
|
|
|
239
|
1 |
|
if (($nextComment !== false) && (($tokens[$nextComment]['line']) === ($tokens[$stackPtr]['line'] + 1))) { |
|
240
|
1 |
|
return; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
1 |
|
$lastComment = $stackPtr; |
|
244
|
1 |
|
while (($topComment = $phpcsFile->findPrevious(array(T_COMMENT), ($lastComment - 1), null, false)) !== false) { |
|
245
|
1 |
|
if ($tokens[$topComment]['line'] !== ($tokens[$lastComment]['line'] - 1)) { |
|
246
|
1 |
|
break; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
1 |
|
$lastComment = $topComment; |
|
250
|
1 |
|
} |
|
251
|
|
|
|
|
252
|
1 |
|
$topComment = $lastComment; |
|
253
|
1 |
|
$commentText = ''; |
|
254
|
|
|
|
|
255
|
1 |
|
for ($i = $topComment; $i <= $stackPtr; $i++) { |
|
256
|
1 |
|
if ($tokens[$i]['code'] === T_COMMENT) { |
|
257
|
1 |
|
$commentText .= trim(substr($tokens[$i]['content'], 2)); |
|
258
|
1 |
|
} |
|
259
|
1 |
|
} |
|
260
|
|
|
|
|
261
|
1 |
|
if ($commentText === '') { |
|
262
|
1 |
|
$error = 'Blank comments are not allowed'; |
|
263
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Empty'); |
|
264
|
1 |
|
if ($fix === true) { |
|
265
|
1 |
|
$phpcsFile->fixer->replaceToken($stackPtr, ''); |
|
266
|
1 |
|
} |
|
267
|
|
|
|
|
268
|
1 |
|
return; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
// If the first character is now uppercase and is not |
|
272
|
|
|
// a non-letter character, throw an error. |
|
273
|
|
|
// \p{Lu} : an uppercase letter that has a lowercase variant. |
|
274
|
|
|
// \P{L} : a non-letter character. |
|
275
|
1 |
|
if (preg_match('/\p{Lu}|\P{L}/u', $commentText[0]) === 0) { |
|
276
|
1 |
|
$error = 'Inline comments must start with a capital letter'; |
|
277
|
1 |
|
$phpcsFile->addError($error, $topComment, 'NotCapital'); |
|
278
|
1 |
|
} |
|
279
|
|
|
|
|
280
|
|
|
// Only check the end of comment character if the start of the comment |
|
281
|
|
|
// is a letter, indicating that the comment is just standard text. |
|
282
|
1 |
|
if (preg_match('/\P{L}/u', $commentText[0]) === 0) { |
|
283
|
1 |
|
$commentCloser = $commentText[(strlen($commentText) - 1)]; |
|
284
|
|
|
$acceptedClosers = array( |
|
285
|
1 |
|
'full-stops' => '.', |
|
286
|
1 |
|
'exclamation marks' => '!', |
|
287
|
1 |
|
'or question marks' => '?', |
|
288
|
1 |
|
); |
|
289
|
|
|
|
|
290
|
1 |
|
if (in_array($commentCloser, $acceptedClosers) === false) { |
|
291
|
1 |
|
$error = 'Inline comments must end in %s'; |
|
292
|
1 |
|
$ender = ''; |
|
293
|
1 |
|
foreach ($acceptedClosers as $closerName => $symbol) { |
|
294
|
1 |
|
$ender .= ' '.$closerName.','; |
|
295
|
1 |
|
} |
|
296
|
|
|
|
|
297
|
1 |
|
$ender = trim($ender, ' ,'); |
|
298
|
1 |
|
$data = array($ender); |
|
299
|
1 |
|
$phpcsFile->addError($error, $stackPtr, 'InvalidEndChar', $data); |
|
300
|
1 |
|
} |
|
301
|
1 |
|
} |
|
302
|
|
|
|
|
303
|
|
|
// Finally, the line below the last comment cannot be empty if this inline |
|
304
|
|
|
// comment is on a line by itself. |
|
305
|
1 |
|
if ($tokens[$previousContent]['line'] < $tokens[$stackPtr]['line']) { |
|
306
|
1 |
|
for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) { |
|
307
|
1 |
|
if ($tokens[$i]['line'] === ($tokens[$stackPtr]['line'] + 1)) { |
|
308
|
1 |
|
if ($tokens[$i]['code'] !== T_WHITESPACE) { |
|
309
|
1 |
|
return; |
|
310
|
|
|
} |
|
311
|
1 |
|
} elseif ($tokens[$i]['line'] > ($tokens[$stackPtr]['line'] + 1)) { |
|
312
|
1 |
|
break; |
|
313
|
|
|
} |
|
314
|
1 |
|
} |
|
315
|
|
|
|
|
316
|
1 |
|
$error = 'There must be no blank line following an inline comment'; |
|
317
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfter'); |
|
318
|
1 |
|
if ($fix === true) { |
|
319
|
1 |
|
$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
|
320
|
1 |
|
$phpcsFile->fixer->beginChangeset(); |
|
321
|
1 |
|
for ($i = ($stackPtr + 1); $i < $next; $i++) { |
|
322
|
1 |
|
if ($tokens[$i]['line'] === $tokens[$next]['line']) { |
|
323
|
1 |
|
break; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
1 |
|
$phpcsFile->fixer->replaceToken($i, ''); |
|
327
|
1 |
|
} |
|
328
|
|
|
|
|
329
|
1 |
|
$phpcsFile->fixer->endChangeset(); |
|
330
|
1 |
|
} |
|
331
|
1 |
|
}//end if |
|
332
|
1 |
|
}//end process() |
|
333
|
|
|
}//end class |
|
334
|
|
|
|