PageRenderTime 22ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/test/CodeSniffer/Standards/Xodoa/Sniffs/Commenting/FunctionCommentSniff.php

http://github.com/ruflin/Elastica
PHP | 419 lines | 218 code | 74 blank | 127 comment | 42 complexity | d823d486ca3921cc43c811b13b1fe5e6 MD5 | raw file
  1. <?php
  2. /**
  3. * Parses and verifies the doc comments for functions.
  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: FunctionCommentSniff.php,v 1.24 2007/11/23 01:04:48 squiz Exp $
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. if (class_exists('PHP_CodeSniffer_CommentParser_FunctionCommentParser', true) === false) {
  17. throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_CommentParser_FunctionCommentParser not found');
  18. }
  19. /**
  20. * Parses and verifies the doc comments for functions.
  21. *
  22. * Verifies that :
  23. * <ul>
  24. * <li>A comment exists</li>
  25. * <li>There is a blank newline after the short description.</li>
  26. * <li>There is a blank newline between the long and short description.</li>
  27. * <li>There is a blank newline between the long description and tags.</li>
  28. * <li>Parameter names represent those in the method.</li>
  29. * <li>Parameter comments are in the correct order</li>
  30. * <li>Parameter comments are complete</li>
  31. * <li>A space is present before the first and after the last parameter</li>
  32. * <li>There must be one blank line between body and headline comments.</li>
  33. * <li>Any throw tag must have an exception class.</li>
  34. * </ul>
  35. *
  36. * @category PHP
  37. * @package PHP_CodeSniffer
  38. * @author Greg Sherwood <gsherwood@squiz.net>
  39. * @author Marc McIntyre <mmcintyre@squiz.net>
  40. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  41. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  42. * @version Release: 1.0.1
  43. * @link http://pear.php.net/package/PHP_CodeSniffer
  44. */
  45. class Xodoa_Sniffs_Commenting_FunctionCommentSniff implements PHP_CodeSniffer_Sniff
  46. {
  47. /**
  48. * The name of the method that we are currently processing.
  49. *
  50. * @var string
  51. */
  52. private $_methodName = '';
  53. /**
  54. * The position in the stack where the fucntion token was found.
  55. *
  56. * @var int
  57. */
  58. private $_functionToken = null;
  59. /**
  60. * The position in the stack where the class token was found.
  61. *
  62. * @var int
  63. */
  64. private $_classToken = null;
  65. /**
  66. * The function comment parser for the current method.
  67. *
  68. * @var PHP_CodeSniffer_Comment_Parser_FunctionCommentParser
  69. */
  70. protected $_commentParser = null;
  71. /**
  72. * The current PHP_CodeSniffer_File object we are processing.
  73. *
  74. * @var PHP_CodeSniffer_File
  75. */
  76. protected $_currentFile = null;
  77. /**
  78. * Returns an array of tokens this test wants to listen for.
  79. *
  80. * @return array
  81. */
  82. public function register()
  83. {
  84. return array(T_FUNCTION);
  85. }//end register()
  86. /**
  87. * Processes this test, when one of its tokens is encountered.
  88. *
  89. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  90. * @param int $stackPtr The position of the current token
  91. * in the stack passed in $tokens.
  92. *
  93. * @return void
  94. */
  95. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  96. {
  97. $find = array(
  98. T_COMMENT,
  99. T_DOC_COMMENT,
  100. T_CLASS,
  101. T_FUNCTION,
  102. T_OPEN_TAG,
  103. );
  104. $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1));
  105. if ($commentEnd === false) {
  106. return;
  107. }
  108. $this->_currentFile = $phpcsFile;
  109. $tokens = $phpcsFile->getTokens();
  110. // If the token that we found was a class or a function, then this
  111. // function has no doc comment.
  112. $code = $tokens[$commentEnd]['code'];
  113. if ($code === T_COMMENT) {
  114. $error = 'You must use "/**" style comments for a function comment';
  115. $phpcsFile->addError($error, $stackPtr);
  116. return;
  117. } else if ($code !== T_DOC_COMMENT) {
  118. $phpcsFile->addError('Missing function doc comment', $stackPtr);
  119. return;
  120. }
  121. // If there is any code between the function keyword and the doc block
  122. // then the doc block is not for us.
  123. $ignore = PHP_CodeSniffer_Tokens::$scopeModifiers;
  124. $ignore[] = T_STATIC;
  125. $ignore[] = T_WHITESPACE;
  126. $ignore[] = T_ABSTRACT;
  127. $ignore[] = T_FINAL;
  128. $prevToken = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
  129. if ($prevToken !== $commentEnd) {
  130. $phpcsFile->addError('Missing function doc comment', $stackPtr);
  131. return;
  132. }
  133. $this->_functionToken = $stackPtr;
  134. foreach ($tokens[$stackPtr]['conditions'] as $condPtr => $condition) {
  135. if ($condition === T_CLASS || $condition === T_INTERFACE) {
  136. $this->_classToken = $condPtr;
  137. break;
  138. }
  139. }
  140. // If the first T_OPEN_TAG is right before the comment, it is probably
  141. // a file comment.
  142. $commentStart = ($phpcsFile->findPrevious(T_DOC_COMMENT, ($commentEnd - 1), null, true) + 1);
  143. $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($commentStart - 1), null, true);
  144. if ($tokens[$prevToken]['code'] === T_OPEN_TAG) {
  145. // Is this the first open tag?
  146. if ($stackPtr === 0 || $phpcsFile->findPrevious(T_OPEN_TAG, ($prevToken - 1)) === false) {
  147. $phpcsFile->addError('Missing function doc comment', $stackPtr);
  148. return;
  149. }
  150. }
  151. $comment = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart + 1));
  152. $this->_methodName = $phpcsFile->getDeclarationName($stackPtr);
  153. try {
  154. $this->_commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsFile);
  155. $this->_commentParser->parse();
  156. } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
  157. $line = ($e->getLineWithinComment() + $commentStart);
  158. $phpcsFile->addError($e->getMessage(), $line);
  159. return;
  160. }
  161. $comment = $this->_commentParser->getComment();
  162. if (is_null($comment) === true) {
  163. $error = 'Function doc comment is empty';
  164. $phpcsFile->addError($error, $commentStart);
  165. return;
  166. }
  167. $this->processParams($commentStart);
  168. $this->processReturn($commentStart, $commentEnd);
  169. $this->processThrows($commentStart);
  170. // No extra newline before short description.
  171. $short = $comment->getShortComment();
  172. $newlineCount = 0;
  173. $newlineSpan = strspn($short, $phpcsFile->eolChar);
  174. if ($short !== '' && $newlineSpan > 0) {
  175. $line = ($newlineSpan > 1) ? 'newlines' : 'newline';
  176. $error = "Extra $line found before function comment short description";
  177. $phpcsFile->addError($error, ($commentStart + 1));
  178. }
  179. $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1);
  180. // Exactly one blank line between short and long description.
  181. $long = $comment->getLongComment();
  182. if (empty($long) === false) {
  183. $between = $comment->getWhiteSpaceBetween();
  184. $newlineBetween = substr_count($between, $phpcsFile->eolChar);
  185. if ($newlineBetween !== 2) {
  186. $error = 'There must be exactly one blank line between descriptions in function comment';
  187. $phpcsFile->addError($error, ($commentStart + $newlineCount + 1));
  188. }
  189. $newlineCount += $newlineBetween;
  190. }
  191. // Exactly one blank line before tags or no line -> can be used for getters
  192. $params = $this->_commentParser->getTagOrders();
  193. if (count($params) > 1) {
  194. $newlineSpan = $comment->getNewlineAfter();
  195. if ($newlineSpan !== 2 && $newlineSpan !== 0) {
  196. $error = 'There must be exactly one blank line before the tags in function comment';
  197. if ($long !== '') {
  198. $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1);
  199. }
  200. $phpcsFile->addError($error, ($commentStart + $newlineCount));
  201. $short = rtrim($short, $phpcsFile->eolChar.' ');
  202. }
  203. }
  204. }//end process()
  205. /**
  206. * Process any throw tags that this function comment has.
  207. *
  208. * @param int $commentStart The position in the stack where the
  209. * comment started.
  210. *
  211. * @return void
  212. */
  213. protected function processThrows($commentStart)
  214. {
  215. if (count($this->_commentParser->getThrows()) === 0) {
  216. return;
  217. }
  218. foreach ($this->_commentParser->getThrows() as $throw) {
  219. $exception = $throw->getValue();
  220. $errorPos = ($commentStart + $throw->getLine());
  221. if ($exception === '') {
  222. $error = '@throws tag must contain the exception class name';
  223. $this->_currentFile->addError($error, $errorPos);
  224. }
  225. }
  226. }//end processThrows()
  227. /**
  228. * Process the return comment of this function comment.
  229. *
  230. * @param int $commentStart The position in the stack where the comment started.
  231. * @param int $commentEnd The position in the stack where the comment ended.
  232. *
  233. * @return void
  234. */
  235. protected function processReturn($commentStart, $commentEnd)
  236. {
  237. }//end processReturn()
  238. /**
  239. * Process the function parameter comments.
  240. *
  241. * @param int $commentStart The position in the stack where
  242. * the comment started.
  243. *
  244. * @return void
  245. */
  246. protected function processParams($commentStart)
  247. {
  248. $realParams = $this->_currentFile->getMethodParameters($this->_functionToken);
  249. $params = $this->_commentParser->getParams();
  250. $foundParams = array();
  251. if (empty($params) === false) {
  252. // Parameters must appear immediately after the comment.
  253. if ($params[0]->getOrder() !== 2) {
  254. $error = 'Parameters must appear immediately after the comment';
  255. $errorPos = ($params[0]->getLine() + $commentStart);
  256. $this->_currentFile->addError($error, $errorPos);
  257. }
  258. $previousParam = null;
  259. $spaceBeforeVar = 10000;
  260. $spaceBeforeComment = 10000;
  261. $longestType = 0;
  262. $longestVar = 0;
  263. foreach ($params as $param) {
  264. $paramComment = trim($param->getComment());
  265. $errorPos = ($param->getLine() + $commentStart);
  266. // Make sure that there is only one space before the var type.
  267. if ($param->getWhitespaceBeforeType() !== ' ') {
  268. $error = 'Expected 1 space before variable type';
  269. $this->_currentFile->addError($error, $errorPos);
  270. }
  271. $spaceCount = substr_count($param->getWhitespaceBeforeVarName(), ' ');
  272. if ($spaceCount < $spaceBeforeVar) {
  273. $spaceBeforeVar = $spaceCount;
  274. $longestType = $errorPos;
  275. }
  276. $spaceCount = substr_count($param->getWhitespaceBeforeComment(), ' ');
  277. if ($spaceCount < $spaceBeforeComment && $paramComment !== '') {
  278. $spaceBeforeComment = $spaceCount;
  279. $longestVar = $errorPos;
  280. }
  281. // Make sure they are in the correct order,
  282. // and have the correct name.
  283. $pos = $param->getPosition();
  284. $paramName = ($param->getVarName() !== '') ? $param->getVarName() : '[ UNKNOWN ]';
  285. // Make sure the names of the parameter comment matches the
  286. // actual parameter.
  287. if (isset($realParams[($pos - 1)]) === true) {
  288. $realName = $realParams[($pos - 1)]['name'];
  289. $foundParams[] = $realName;
  290. // Append ampersand to name if passing by reference.
  291. if ($realParams[($pos - 1)]['pass_by_reference'] === true) {
  292. $realName = '&'.$realName;
  293. }
  294. if ($realName !== $param->getVarName()) {
  295. $error = 'Doc comment var "'.$paramName;
  296. $error .= '" does not match actual variable name "'.$realName;
  297. $error .= '" at position '.$pos;
  298. $this->_currentFile->addError($error, $errorPos);
  299. }
  300. } else {
  301. // We must have an extra parameter comment.
  302. // Ignore..
  303. //$error = 'Superfluous doc comment at position '.$pos;
  304. //$this->_currentFile->addError($error, $errorPos);
  305. }
  306. if ($param->getVarName() === '') {
  307. $error = 'Missing parameter name at position '.$pos;
  308. $this->_currentFile->addError($error, $errorPos);
  309. }
  310. if ($param->getType() === '') {
  311. $error = 'Missing type at position '.$pos;
  312. $this->_currentFile->addError($error, $errorPos);
  313. }
  314. if ($paramComment === '') {
  315. //$error = 'Missing comment for param "'.$paramName.'" at position '.$pos;
  316. //$this->_currentFile->addError($error, $errorPos);
  317. }
  318. $previousParam = $param;
  319. }//end foreach
  320. if ($spaceBeforeVar !== 1 && $spaceBeforeVar !== 10000 && $spaceBeforeComment !== 10000) {
  321. $error = 'Expected 1 space after the longest type';
  322. $this->_currentFile->addError($error, $longestType);
  323. }
  324. if ($spaceBeforeComment !== 1 && $spaceBeforeComment !== 10000) {
  325. $error = 'Expected 1 space after the longest variable name';
  326. $this->_currentFile->addError($error, $longestVar);
  327. }
  328. }//end if
  329. $realNames = array();
  330. foreach ($realParams as $realParam) {
  331. $realNames[] = $realParam['name'];
  332. }
  333. // Report and missing comments.
  334. $diff = array_diff($realNames, $foundParams);
  335. foreach ($diff as $neededParam) {
  336. if (count($params) !== 0) {
  337. $errorPos = ($params[(count($params) - 1)]->getLine() + $commentStart);
  338. } else {
  339. $errorPos = $commentStart;
  340. }
  341. $error = 'Doc comment for "'.$neededParam.'" missing';
  342. $this->_currentFile->addError($error, $errorPos);
  343. }
  344. }//end processParams()
  345. }//end class