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 267 268 269 270 271
|
<?php
/**
* Squiz_Sniffs_Commenting_InlineCommentSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Squiz_Sniffs_Commenting_InlineCommentSniff.
*
* Checks that there is adequate spacing between comments.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.4
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Squiz_Sniffs_Commenting_InlineCommentSniff implements PHP_CodeSniffer_Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = array(
'PHP',
'JS',
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_COMMENT,
T_DOC_COMMENT,
);
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// If this is a function/class/interface doc block comment, skip it.
// We are only interested in inline doc block comments, which are
// not allowed.
if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT) {
$nextToken = $phpcsFile->findNext(
PHP_CodeSniffer_Tokens::$emptyTokens,
($stackPtr + 1),
null,
true
);
$ignore = array(
T_CLASS,
T_INTERFACE,
T_FUNCTION,
T_PUBLIC,
T_PRIVATE,
T_PROTECTED,
T_FINAL,
T_STATIC,
T_ABSTRACT,
T_CONST,
T_OBJECT,
T_PROPERTY,
);
if (in_array($tokens[$nextToken]['code'], $ignore) === true) {
return;
} else {
if ($phpcsFile->tokenizerType === 'JS') {
// We allow block comments if a function is being assigned
// to a variable.
$ignore = PHP_CodeSniffer_Tokens::$emptyTokens;
$ignore[] = T_EQUAL;
$ignore[] = T_STRING;
$ignore[] = T_OBJECT_OPERATOR;
$nextToken = $phpcsFile->findNext($ignore, ($nextToken + 1), null, true);
if ($tokens[$nextToken]['code'] === T_FUNCTION) {
return;
}
}
$prevToken = $phpcsFile->findPrevious(
PHP_CodeSniffer_Tokens::$emptyTokens,
($stackPtr - 1),
null,
true
);
if ($tokens[$prevToken]['code'] === T_OPEN_TAG) {
return;
}
// Only error once per comment.
if (substr($tokens[$stackPtr]['content'], 0, 3) === '/**') {
$error = 'Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead';
$phpcsFile->addError($error, $stackPtr, 'DocBlock');
}
}//end if
}//end if
if ($tokens[$stackPtr]['content']{0} === '#') {
$error = 'Perl-style comments are not allowed; use "// Comment" instead';
$phpcsFile->addError($error, $stackPtr, 'WrongStyle');
}
// We don't want end of block comments. If the last comment is a closing
// curly brace.
$previousContent = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
if ($tokens[$previousContent]['line'] === $tokens[$stackPtr]['line']) {
if ($tokens[$previousContent]['code'] === T_CLOSE_CURLY_BRACKET) {
return;
}
// Special case for JS files.
if ($tokens[$previousContent]['code'] === T_COMMA
|| $tokens[$previousContent]['code'] === T_SEMICOLON
) {
$lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($previousContent - 1), null, true);
if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) {
return;
}
}
}
$comment = rtrim($tokens[$stackPtr]['content']);
// Only want inline comments.
if (substr($comment, 0, 2) !== '//') {
return;
}
$spaceCount = 0;
for ($i = 2; $i < strlen($comment); $i++) {
if ($comment[$i] !== ' ') {
break;
}
$spaceCount++;
}
if ($spaceCount === 0) {
$error = 'No space before comment text; expected "// %s" but found "%s"';
$data = array(
substr($comment, 2),
$comment,
);
$phpcsFile->addError($error, $stackPtr, 'NoSpaceBefore', $data);
}
if ($spaceCount > 1) {
$error = '%s spaces found before inline comment; expected "// %s" but found "%s"';
$data = array(
$spaceCount,
substr($comment, (2 + $spaceCount)),
$comment,
);
$phpcsFile->addError($error, $stackPtr, 'SpacingBefore', $data);
}
// The below section determines if a comment block is correctly capitalised,
// and ends in a full-stop. It will find the last comment in a block, and
// work its way up.
$nextComment = $phpcsFile->findNext(array(T_COMMENT), ($stackPtr + 1), null, false);
if (($nextComment !== false) && (($tokens[$nextComment]['line']) === ($tokens[$stackPtr]['line'] + 1))) {
return;
}
$topComment = $stackPtr;
$lastComment = $stackPtr;
while (($topComment = $phpcsFile->findPrevious(array(T_COMMENT), ($lastComment - 1), null, false)) !== false) {
if ($tokens[$topComment]['line'] !== ($tokens[$lastComment]['line'] - 1)) {
break;
}
$lastComment = $topComment;
}
$topComment = $lastComment;
$commentText = '';
for ($i = $topComment; $i <= $stackPtr; $i++) {
if ($tokens[$i]['code'] === T_COMMENT) {
$commentText .= trim(substr($tokens[$i]['content'], 2));
}
}
if ($commentText === '') {
$error = 'Blank comments are not allowed';
$phpcsFile->addError($error, $stackPtr, 'Empty');
return;
}
if (preg_match('|[A-Z]|', $commentText[0]) === 0) {
$error = 'Inline comments must start with a capital letter';
$phpcsFile->addError($error, $topComment, 'NotCapital');
}
$commentCloser = $commentText[(strlen($commentText) - 1)];
$acceptedClosers = array(
'full-stops' => '.',
'exclamation marks' => '!',
'or question marks' => '?',
);
if (in_array($commentCloser, $acceptedClosers) === false) {
$error = 'Inline comments must end in %s';
$ender = '';
foreach ($acceptedClosers as $closerName => $symbol) {
$ender .= ' '.$closerName.',';
}
$ender = rtrim($ender, ',');
$data = array($ender);
$phpcsFile->addError($error, $stackPtr, 'InvalidEndChar', $data);
}
// Finally, the line below the last comment cannot be empty.
$start = false;
for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) {
if ($tokens[$i]['line'] === ($tokens[$stackPtr]['line'] + 1)) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
return;
}
} else if ($tokens[$i]['line'] > ($tokens[$stackPtr]['line'] + 1)) {
break;
}
}
$error = 'There must be no blank line following an inline comment';
$phpcsFile->addError($error, $stackPtr, 'SpacingAfter');
}//end process()
}//end class
?>
|