5 |
aurelien |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Parses and verifies the doc comments for classes.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PHP_CodeSniffer
|
|
|
9 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
10 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
11 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
12 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
13 |
* @version CVS: $Id: ClassCommentSniff.php,v 1.19 2008/12/02 02:38:34 squiz Exp $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
if (class_exists('PHP_CodeSniffer_CommentParser_ClassCommentParser', true) === false) {
|
|
|
18 |
$error = 'Class PHP_CodeSniffer_CommentParser_ClassCommentParser not found';
|
|
|
19 |
throw new PHP_CodeSniffer_Exception($error);
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
if (class_exists('PEAR_Sniffs_Commenting_FileCommentSniff', true) === false) {
|
|
|
23 |
$error = 'Class PEAR_Sniffs_Commenting_FileCommentSniff not found';
|
|
|
24 |
throw new PHP_CodeSniffer_Exception($error);
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Parses and verifies the doc comments for classes.
|
|
|
29 |
*
|
|
|
30 |
* Verifies that :
|
|
|
31 |
* <ul>
|
|
|
32 |
* <li>A doc comment exists.</li>
|
|
|
33 |
* <li>There is a blank newline after the short description.</li>
|
|
|
34 |
* <li>There is a blank newline between the long and short description.</li>
|
|
|
35 |
* <li>There is a blank newline between the long description and tags.</li>
|
|
|
36 |
* <li>Check the order of the tags.</li>
|
|
|
37 |
* <li>Check the indentation of each tag.</li>
|
|
|
38 |
* <li>Check required and optional tags and the format of their content.</li>
|
|
|
39 |
* </ul>
|
|
|
40 |
*
|
|
|
41 |
* @category PHP
|
|
|
42 |
* @package PHP_CodeSniffer
|
|
|
43 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
44 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
45 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
46 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
47 |
* @version Release: 1.2.0RC1
|
|
|
48 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
49 |
*/
|
|
|
50 |
class PEAR_Sniffs_Commenting_ClassCommentSniff extends PEAR_Sniffs_Commenting_FileCommentSniff
|
|
|
51 |
{
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Returns an array of tokens this test wants to listen for.
|
|
|
56 |
*
|
|
|
57 |
* @return array
|
|
|
58 |
*/
|
|
|
59 |
public function register()
|
|
|
60 |
{
|
|
|
61 |
return array(
|
|
|
62 |
T_CLASS,
|
|
|
63 |
T_INTERFACE,
|
|
|
64 |
);
|
|
|
65 |
|
|
|
66 |
}//end register()
|
|
|
67 |
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Processes this test, when one of its tokens is encountered.
|
|
|
71 |
*
|
|
|
72 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
73 |
* @param int $stackPtr The position of the current token
|
|
|
74 |
* in the stack passed in $tokens.
|
|
|
75 |
*
|
|
|
76 |
* @return void
|
|
|
77 |
*/
|
|
|
78 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
79 |
{
|
|
|
80 |
$this->currentFile = $phpcsFile;
|
|
|
81 |
|
|
|
82 |
$tokens = $phpcsFile->getTokens();
|
|
|
83 |
$type = strtolower($tokens[$stackPtr]['content']);
|
|
|
84 |
$find = array(
|
|
|
85 |
T_ABSTRACT,
|
|
|
86 |
T_WHITESPACE,
|
|
|
87 |
T_FINAL,
|
|
|
88 |
);
|
|
|
89 |
|
|
|
90 |
// Extract the class comment docblock.
|
|
|
91 |
$commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
|
|
|
92 |
|
|
|
93 |
if ($commentEnd !== false && $tokens[$commentEnd]['code'] === T_COMMENT) {
|
|
|
94 |
$error = "You must use \"/**\" style comments for a $type comment";
|
|
|
95 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
96 |
return;
|
|
|
97 |
} else if ($commentEnd === false
|
|
|
98 |
|| $tokens[$commentEnd]['code'] !== T_DOC_COMMENT
|
|
|
99 |
) {
|
|
|
100 |
$phpcsFile->addError("Missing $type doc comment", $stackPtr);
|
|
|
101 |
return;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
$commentStart = ($phpcsFile->findPrevious(T_DOC_COMMENT, ($commentEnd - 1), null, true) + 1);
|
|
|
105 |
$commentNext = $phpcsFile->findPrevious(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, $phpcsFile->eolChar);
|
|
|
106 |
|
|
|
107 |
// Distinguish file and class comment.
|
|
|
108 |
$prevClassToken = $phpcsFile->findPrevious(T_CLASS, ($stackPtr - 1));
|
|
|
109 |
if ($prevClassToken === false) {
|
|
|
110 |
// This is the first class token in this file, need extra checks.
|
|
|
111 |
$prevNonComment = $phpcsFile->findPrevious(T_DOC_COMMENT, ($commentStart - 1), null, true);
|
|
|
112 |
if ($prevNonComment !== false) {
|
|
|
113 |
$prevComment = $phpcsFile->findPrevious(T_DOC_COMMENT, ($prevNonComment - 1));
|
|
|
114 |
if ($prevComment === false) {
|
|
|
115 |
// There is only 1 doc comment between open tag and class token.
|
|
|
116 |
$newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, $phpcsFile->eolChar);
|
|
|
117 |
if ($newlineToken !== false) {
|
|
|
118 |
$newlineToken = $phpcsFile->findNext(
|
|
|
119 |
T_WHITESPACE,
|
|
|
120 |
($newlineToken + 1),
|
|
|
121 |
$stackPtr,
|
|
|
122 |
false,
|
|
|
123 |
$phpcsFile->eolChar
|
|
|
124 |
);
|
|
|
125 |
|
|
|
126 |
if ($newlineToken !== false) {
|
|
|
127 |
// Blank line between the class and the doc block.
|
|
|
128 |
// The doc block is most likely a file comment.
|
|
|
129 |
$error = "Missing $type doc comment";
|
|
|
130 |
$phpcsFile->addError($error, ($stackPtr + 1));
|
|
|
131 |
return;
|
|
|
132 |
}
|
|
|
133 |
}//end if
|
|
|
134 |
}//end if
|
|
|
135 |
}//end if
|
|
|
136 |
}//end if
|
|
|
137 |
|
|
|
138 |
$comment = $phpcsFile->getTokensAsString(
|
|
|
139 |
$commentStart,
|
|
|
140 |
($commentEnd - $commentStart + 1)
|
|
|
141 |
);
|
|
|
142 |
|
|
|
143 |
// Parse the class comment.docblock.
|
|
|
144 |
try {
|
|
|
145 |
$this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($comment, $phpcsFile);
|
|
|
146 |
$this->commentParser->parse();
|
|
|
147 |
} catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
|
|
|
148 |
$line = ($e->getLineWithinComment() + $commentStart);
|
|
|
149 |
$phpcsFile->addError($e->getMessage(), $line);
|
|
|
150 |
return;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
$comment = $this->commentParser->getComment();
|
|
|
154 |
if (is_null($comment) === true) {
|
|
|
155 |
$error = ucfirst($type).' doc comment is empty';
|
|
|
156 |
$phpcsFile->addError($error, $commentStart);
|
|
|
157 |
return;
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
// No extra newline before short description.
|
|
|
161 |
$short = $comment->getShortComment();
|
|
|
162 |
$newlineCount = 0;
|
|
|
163 |
$newlineSpan = strspn($short, $phpcsFile->eolChar);
|
|
|
164 |
if ($short !== '' && $newlineSpan > 0) {
|
|
|
165 |
$line = ($newlineSpan > 1) ? 'newlines' : 'newline';
|
|
|
166 |
$error = "Extra $line found before $type comment short description";
|
|
|
167 |
$phpcsFile->addError($error, ($commentStart + 1));
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
$newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1);
|
|
|
171 |
|
|
|
172 |
// Exactly one blank line between short and long description.
|
|
|
173 |
$long = $comment->getLongComment();
|
|
|
174 |
if (empty($long) === false) {
|
|
|
175 |
$between = $comment->getWhiteSpaceBetween();
|
|
|
176 |
$newlineBetween = substr_count($between, $phpcsFile->eolChar);
|
|
|
177 |
if ($newlineBetween !== 2) {
|
|
|
178 |
$error = "There must be exactly one blank line between descriptions in $type comments";
|
|
|
179 |
$phpcsFile->addError($error, ($commentStart + $newlineCount + 1));
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
$newlineCount += $newlineBetween;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
// Exactly one blank line before tags.
|
|
|
186 |
$tags = $this->commentParser->getTagOrders();
|
|
|
187 |
if (count($tags) > 1) {
|
|
|
188 |
$newlineSpan = $comment->getNewlineAfter();
|
|
|
189 |
if ($newlineSpan !== 2) {
|
|
|
190 |
$error = "There must be exactly one blank line before the tags in $type comments";
|
|
|
191 |
if ($long !== '') {
|
|
|
192 |
$newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1);
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
$phpcsFile->addError($error, ($commentStart + $newlineCount));
|
|
|
196 |
$short = rtrim($short, $phpcsFile->eolChar.' ');
|
|
|
197 |
}
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
// Check each tag.
|
|
|
201 |
$this->processTags($commentStart, $commentEnd);
|
|
|
202 |
|
|
|
203 |
}//end process()
|
|
|
204 |
|
|
|
205 |
|
|
|
206 |
/**
|
|
|
207 |
* Process the version tag.
|
|
|
208 |
*
|
|
|
209 |
* @param int $errorPos The line number where the error occurs.
|
|
|
210 |
*
|
|
|
211 |
* @return void
|
|
|
212 |
*/
|
|
|
213 |
protected function processVersion($errorPos)
|
|
|
214 |
{
|
|
|
215 |
$version = $this->commentParser->getVersion();
|
|
|
216 |
if ($version !== null) {
|
|
|
217 |
$content = $version->getContent();
|
|
|
218 |
$matches = array();
|
|
|
219 |
if (empty($content) === true) {
|
|
|
220 |
$error = 'Content missing for @version tag in doc comment';
|
|
|
221 |
$this->currentFile->addError($error, $errorPos);
|
|
|
222 |
} else if ((strstr($content, 'Release:') === false)) {
|
|
|
223 |
$error = "Invalid version \"$content\" in doc comment; consider \"Release: <package_version>\" instead";
|
|
|
224 |
$this->currentFile->addWarning($error, $errorPos);
|
|
|
225 |
}
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
}//end processVersion()
|
|
|
229 |
|
|
|
230 |
|
|
|
231 |
}//end class
|
|
|
232 |
|
|
|
233 |
?>
|