3
* Squiz_Sniffs_Commenting_InlineCommentSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @author Marc McIntyre <mmcintyre@squiz.net>
11
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
12
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
13
* @link http://pear.php.net/package/PHP_CodeSniffer
17
* Squiz_Sniffs_Commenting_InlineCommentSniff.
19
* Checks that there is adequate spacing between comments.
22
* @package PHP_CodeSniffer
23
* @author Greg Sherwood <gsherwood@squiz.net>
24
* @author Marc McIntyre <mmcintyre@squiz.net>
25
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
26
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
27
* @version Release: 1.5.4
28
* @link http://pear.php.net/package/PHP_CodeSniffer
30
class Squiz_Sniffs_Commenting_InlineCommentSniff implements PHP_CodeSniffer_Sniff
34
* A list of tokenizers this sniff supports.
38
public $supportedTokenizers = array(
44
* Returns an array of tokens this test wants to listen for.
48
public function register()
59
* Processes this test, when one of its tokens is encountered.
61
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
62
* @param int $stackPtr The position of the current token in the
63
* stack passed in $tokens.
67
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
69
$tokens = $phpcsFile->getTokens();
71
// If this is a function/class/interface doc block comment, skip it.
72
// We are only interested in inline doc block comments, which are
74
if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT) {
75
$nextToken = $phpcsFile->findNext(
76
PHP_CodeSniffer_Tokens::$emptyTokens,
98
if (in_array($tokens[$nextToken]['code'], $ignore) === true) {
101
if ($phpcsFile->tokenizerType === 'JS') {
102
// We allow block comments if a function is being assigned
104
$ignore = PHP_CodeSniffer_Tokens::$emptyTokens;
106
$ignore[] = T_STRING;
107
$ignore[] = T_OBJECT_OPERATOR;
108
$nextToken = $phpcsFile->findNext($ignore, ($nextToken + 1), null, true);
109
if ($tokens[$nextToken]['code'] === T_FUNCTION) {
114
$prevToken = $phpcsFile->findPrevious(
115
PHP_CodeSniffer_Tokens::$emptyTokens,
121
if ($tokens[$prevToken]['code'] === T_OPEN_TAG) {
125
// Only error once per comment.
126
if (substr($tokens[$stackPtr]['content'], 0, 3) === '/**') {
127
$error = 'Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead';
128
$phpcsFile->addError($error, $stackPtr, 'DocBlock');
133
if ($tokens[$stackPtr]['content']{0} === '#') {
134
$error = 'Perl-style comments are not allowed; use "// Comment" instead';
135
$phpcsFile->addError($error, $stackPtr, 'WrongStyle');
138
// We don't want end of block comments. If the last comment is a closing
140
$previousContent = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
141
if ($tokens[$previousContent]['line'] === $tokens[$stackPtr]['line']) {
142
if ($tokens[$previousContent]['code'] === T_CLOSE_CURLY_BRACKET) {
146
// Special case for JS files.
147
if ($tokens[$previousContent]['code'] === T_COMMA
148
|| $tokens[$previousContent]['code'] === T_SEMICOLON
150
$lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($previousContent - 1), null, true);
151
if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) {
157
$comment = rtrim($tokens[$stackPtr]['content']);
159
// Only want inline comments.
160
if (substr($comment, 0, 2) !== '//') {
165
for ($i = 2; $i < strlen($comment); $i++) {
166
if ($comment[$i] !== ' ') {
173
if ($spaceCount === 0) {
174
$error = 'No space before comment text; expected "// %s" but found "%s"';
179
$phpcsFile->addError($error, $stackPtr, 'NoSpaceBefore', $data);
182
if ($spaceCount > 1) {
183
$error = '%s spaces found before inline comment line; use block comment if you need indentation';
186
substr($comment, (2 + $spaceCount)),
189
$phpcsFile->addError($error, $stackPtr, 'SpacingBefore', $data);
193
// The below section determines if a comment block is correctly capitalised,
194
// and ends in a full-stop. It will find the last comment in a block, and
196
$nextComment = $phpcsFile->findNext(array(T_COMMENT), ($stackPtr + 1), null, false);
198
if (($nextComment !== false) && (($tokens[$nextComment]['line']) === ($tokens[$stackPtr]['line'] + 1))) {
202
$topComment = $stackPtr;
203
$lastComment = $stackPtr;
204
while (($topComment = $phpcsFile->findPrevious(array(T_COMMENT), ($lastComment - 1), null, false)) !== false) {
205
if ($tokens[$topComment]['line'] !== ($tokens[$lastComment]['line'] - 1)) {
209
$lastComment = $topComment;
212
$topComment = $lastComment;
215
for ($i = $topComment; $i <= $stackPtr; $i++) {
216
if ($tokens[$i]['code'] === T_COMMENT) {
217
$commentText .= trim(substr($tokens[$i]['content'], 2));
221
if ($commentText === '') {
222
$error = 'Blank comments are not allowed';
223
$phpcsFile->addError($error, $stackPtr, 'Empty');
227
if (preg_match('|\p{Lu}|u', $commentText[0]) === 0) {
228
$error = 'Inline comments must start with a capital letter';
229
$phpcsFile->addError($error, $topComment, 'NotCapital');
232
$commentCloser = $commentText[(strlen($commentText) - 1)];
233
$acceptedClosers = array(
235
'exclamation marks' => '!',
236
'or question marks' => '?',
239
if (in_array($commentCloser, $acceptedClosers) === false) {
240
$error = 'Inline comments must end in %s';
242
foreach ($acceptedClosers as $closerName => $symbol) {
243
$ender .= ' '.$closerName.',';
246
$ender = rtrim($ender, ',');
247
$data = array($ender);
248
$phpcsFile->addError($error, $stackPtr, 'InvalidEndChar', $data);
251
// Finally, the line below the last comment cannot be empty if this inline
252
// comment is on a line by itself.
253
if ($tokens[$previousContent]['line'] < $tokens[$stackPtr]['line']) {
255
for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) {
256
if ($tokens[$i]['line'] === ($tokens[$stackPtr]['line'] + 1)) {
257
if ($tokens[$i]['code'] !== T_WHITESPACE) {
260
} else if ($tokens[$i]['line'] > ($tokens[$stackPtr]['line'] + 1)) {
265
$error = 'There must be no blank line following an inline comment';
266
$phpcsFile->addError($error, $stackPtr, 'SpacingAfter');