PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 345 lines | 199 code | 46 blank | 100 comment | 38 complexity | 339a24d0b217f7d986b60299e52090bc MD5 | raw file
  1. <?php
  2. /**
  3. * Parses and verifies the variable doc comment.
  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-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
  14. */
  15. if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) {
  16. throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found');
  17. }
  18. if (class_exists('PHP_CodeSniffer_CommentParser_MemberCommentParser', true) === false) {
  19. throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_CommentParser_MemberCommentParser not found');
  20. }
  21. /**
  22. * Parses and verifies the variable doc comment.
  23. *
  24. * Verifies that :
  25. * <ul>
  26. * <li>A variable doc comment exists.</li>
  27. * <li>Short description ends with a full stop.</li>
  28. * <li>There is a blank line after the short description.</li>
  29. * <li>There is a blank line between the description and the tags.</li>
  30. * <li>Check the order, indentation and content of each tag.</li>
  31. * </ul>
  32. *
  33. * @category PHP
  34. * @package PHP_CodeSniffer
  35. * @author Greg Sherwood <gsherwood@squiz.net>
  36. * @author Marc McIntyre <mmcintyre@squiz.net>
  37. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  38. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  39. * @version Release: @package_version@
  40. * @link http://pear.php.net/package/PHP_CodeSniffer
  41. */
  42. class Squiz_Sniffs_Commenting_VariableCommentSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
  43. {
  44. /**
  45. * The header comment parser for the current file.
  46. *
  47. * @var PHP_CodeSniffer_Comment_Parser_ClassCommentParser
  48. */
  49. protected $commentParser = null;
  50. /**
  51. * Called to process class member vars.
  52. *
  53. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  54. * @param int $stackPtr The position of the current token
  55. * in the stack passed in $tokens.
  56. *
  57. * @return void
  58. */
  59. public function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  60. {
  61. $this->currentFile = $phpcsFile;
  62. $tokens = $phpcsFile->getTokens();
  63. $commentToken = array(
  64. T_COMMENT,
  65. T_DOC_COMMENT,
  66. );
  67. // Extract the var comment docblock.
  68. $commentEnd = $phpcsFile->findPrevious($commentToken, ($stackPtr - 3));
  69. if ($commentEnd !== false && $tokens[$commentEnd]['code'] === T_COMMENT) {
  70. $phpcsFile->addError('You must use "/**" style comments for a variable comment', $stackPtr, 'WrongStyle');
  71. return;
  72. } else if ($commentEnd === false || $tokens[$commentEnd]['code'] !== T_DOC_COMMENT) {
  73. $phpcsFile->addError('Missing variable doc comment', $stackPtr, 'Missing');
  74. return;
  75. } else {
  76. // Make sure the comment we have found belongs to us.
  77. $commentFor = $phpcsFile->findNext(array(T_VARIABLE, T_CLASS, T_INTERFACE), ($commentEnd + 1));
  78. if ($commentFor !== $stackPtr) {
  79. $phpcsFile->addError('Missing variable doc comment', $stackPtr, 'Missing');
  80. return;
  81. }
  82. }
  83. $commentStart = ($phpcsFile->findPrevious(T_DOC_COMMENT, ($commentEnd - 1), null, true) + 1);
  84. $commentString = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart + 1));
  85. // Parse the header comment docblock.
  86. try {
  87. $this->commentParser = new PHP_CodeSniffer_CommentParser_MemberCommentParser($commentString, $phpcsFile);
  88. $this->commentParser->parse();
  89. } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
  90. $line = ($e->getLineWithinComment() + $commentStart);
  91. $phpcsFile->addError($e->getMessage(), $line, 'ErrorParsing');
  92. return;
  93. }
  94. $comment = $this->commentParser->getComment();
  95. if (is_null($comment) === true) {
  96. $error = 'Variable doc comment is empty';
  97. $phpcsFile->addError($error, $commentStart, 'Empty');
  98. return;
  99. }
  100. // The first line of the comment should just be the /** code.
  101. $eolPos = strpos($commentString, $phpcsFile->eolChar);
  102. $firstLine = substr($commentString, 0, $eolPos);
  103. if ($firstLine !== '/**') {
  104. $error = 'The open comment tag must be the only content on the line';
  105. $phpcsFile->addError($error, $commentStart, 'ContentAfterOpen');
  106. }
  107. // Check for a comment description.
  108. $short = $comment->getShortComment();
  109. $long = '';
  110. if (trim($short) === '') {
  111. $error = 'Missing short description in variable doc comment';
  112. $phpcsFile->addError($error, $commentStart, 'MissingShort');
  113. $newlineCount = 1;
  114. } else {
  115. // No extra newline before short description.
  116. $newlineCount = 0;
  117. $newlineSpan = strspn($short, $phpcsFile->eolChar);
  118. if ($short !== '' && $newlineSpan > 0) {
  119. $error = 'Extra newline(s) found before variable comment short description';
  120. $phpcsFile->addError($error, ($commentStart + 1), 'SpacingBeforeShort');
  121. }
  122. $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1);
  123. // Exactly one blank line between short and long description.
  124. $long = $comment->getLongComment();
  125. if (empty($long) === false) {
  126. $between = $comment->getWhiteSpaceBetween();
  127. $newlineBetween = substr_count($between, $phpcsFile->eolChar);
  128. if ($newlineBetween !== 2) {
  129. $error = 'There must be exactly one blank line between descriptions in variable comment';
  130. $phpcsFile->addError($error, ($commentStart + $newlineCount + 1), 'SpacingBetween');
  131. }
  132. $newlineCount += $newlineBetween;
  133. $testLong = trim($long);
  134. if (preg_match('|\p{Lu}|u', $testLong[0]) === 0) {
  135. $error = 'Variable comment long description must start with a capital letter';
  136. $phpcsFile->addError($error, ($commentStart + $newlineCount), 'LongNotCapital');
  137. }
  138. }//end if
  139. // Short description must be single line and end with a full stop.
  140. $testShort = trim($short);
  141. $lastChar = $testShort[(strlen($testShort) - 1)];
  142. if (substr_count($testShort, $phpcsFile->eolChar) !== 0) {
  143. $error = 'Variable comment short description must be on a single line';
  144. $phpcsFile->addError($error, ($commentStart + 1), 'ShortSingleLine');
  145. }
  146. if (preg_match('|\p{Lu}|u', $testShort[0]) === 0) {
  147. $error = 'Variable comment short description must start with a capital letter';
  148. $phpcsFile->addError($error, ($commentStart + 1), 'ShortNotCapital');
  149. }
  150. if ($lastChar !== '.') {
  151. $error = 'Variable comment short description must end with a full stop';
  152. $phpcsFile->addError($error, ($commentStart + 1), 'ShortFullStop');
  153. }
  154. }//end if
  155. // Exactly one blank line before tags.
  156. $tags = $this->commentParser->getTagOrders();
  157. if (count($tags) > 1) {
  158. $newlineSpan = $comment->getNewlineAfter();
  159. if ($newlineSpan !== 2) {
  160. $error = 'There must be exactly one blank line before the tags in variable comment';
  161. if ($long !== '') {
  162. $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1);
  163. }
  164. $phpcsFile->addError($error, ($commentStart + $newlineCount), 'SpacingBeforeTags');
  165. $short = rtrim($short, $phpcsFile->eolChar.' ');
  166. }
  167. }
  168. // Check for unknown/deprecated tags.
  169. $unknownTags = $this->commentParser->getUnknown();
  170. foreach ($unknownTags as $errorTag) {
  171. // Unknown tags are not parsed, do not process further.
  172. $error = '@%s tag is not allowed in variable comment';
  173. $data = array($errorTag['tag']);
  174. $phpcsFile->addWarning($error, ($commentStart + $errorTag['line']), 'TagNotAllowed', $data);
  175. }
  176. // Check each tag.
  177. $this->processVar($commentStart, $commentEnd);
  178. $this->processSees($commentStart);
  179. // The last content should be a newline and the content before
  180. // that should not be blank. If there is more blank space
  181. // then they have additional blank lines at the end of the comment.
  182. $words = $this->commentParser->getWords();
  183. $lastPos = (count($words) - 1);
  184. if (trim($words[($lastPos - 1)]) !== ''
  185. || strpos($words[($lastPos - 1)], $this->currentFile->eolChar) === false
  186. || trim($words[($lastPos - 2)]) === ''
  187. ) {
  188. $error = 'Additional blank lines found at end of variable comment';
  189. $this->currentFile->addError($error, $commentEnd, 'SpacingAfter');
  190. }
  191. }//end processMemberVar()
  192. /**
  193. * Process the var tag.
  194. *
  195. * @param int $commentStart The position in the stack where the comment started.
  196. * @param int $commentEnd The position in the stack where the comment ended.
  197. *
  198. * @return void
  199. */
  200. protected function processVar($commentStart, $commentEnd)
  201. {
  202. $var = $this->commentParser->getVar();
  203. if ($var !== null) {
  204. $errorPos = ($commentStart + $var->getLine());
  205. $index = array_keys($this->commentParser->getTagOrders(), 'var');
  206. if (count($index) > 1) {
  207. $error = 'Only 1 @var tag is allowed in variable comment';
  208. $this->currentFile->addError($error, $errorPos, 'DuplicateVar');
  209. return;
  210. }
  211. if ($index[0] !== 1) {
  212. $error = 'The @var tag must be the first tag in a variable comment';
  213. $this->currentFile->addError($error, $errorPos, 'VarOrder');
  214. }
  215. $content = $var->getContent();
  216. if (empty($content) === true) {
  217. $error = 'Var type missing for @var tag in variable comment';
  218. $this->currentFile->addError($error, $errorPos, 'MissingVarType');
  219. return;
  220. } else {
  221. $suggestedType = PHP_CodeSniffer::suggestType($content);
  222. if ($content !== $suggestedType) {
  223. $error = 'Expected "%s"; found "%s" for @var tag in variable comment';
  224. $data = array(
  225. $suggestedType,
  226. $content,
  227. );
  228. $this->currentFile->addError($error, $errorPos, 'IncorrectVarType', $data);
  229. }
  230. }
  231. $spacing = substr_count($var->getWhitespaceBeforeContent(), ' ');
  232. if ($spacing !== 1) {
  233. $error = '@var tag indented incorrectly; expected 1 space but found %s';
  234. $data = array($spacing);
  235. $this->currentFile->addError($error, $errorPos, 'VarIndent', $data);
  236. }
  237. } else {
  238. $error = 'Missing @var tag in variable comment';
  239. $this->currentFile->addError($error, $commentEnd, 'MissingVar');
  240. }//end if
  241. }//end processVar()
  242. /**
  243. * Process the see tags.
  244. *
  245. * @param int $commentStart The position in the stack where the comment started.
  246. *
  247. * @return void
  248. */
  249. protected function processSees($commentStart)
  250. {
  251. $sees = $this->commentParser->getSees();
  252. if (empty($sees) === false) {
  253. foreach ($sees as $see) {
  254. $errorPos = ($commentStart + $see->getLine());
  255. $content = $see->getContent();
  256. if (empty($content) === true) {
  257. $error = 'Content missing for @see tag in variable comment';
  258. $this->currentFile->addError($error, $errorPos, 'EmptySees');
  259. continue;
  260. }
  261. $spacing = substr_count($see->getWhitespaceBeforeContent(), ' ');
  262. if ($spacing !== 1) {
  263. $error = '@see tag indented incorrectly; expected 1 spaces but found %s';
  264. $data = array($spacing);
  265. $this->currentFile->addError($error, $errorPos, 'SeesIndent', $data);
  266. }
  267. }
  268. }
  269. }//end processSees()
  270. /**
  271. * Called to process a normal variable.
  272. *
  273. * Not required for this sniff.
  274. *
  275. * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this token was found.
  276. * @param int $stackPtr The position where the double quoted
  277. * string was found.
  278. *
  279. * @return void
  280. */
  281. protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  282. {
  283. }//end processVariable()
  284. /**
  285. * Called to process variables found in double quoted strings.
  286. *
  287. * Not required for this sniff.
  288. *
  289. * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this token was found.
  290. * @param int $stackPtr The position where the double quoted
  291. * string was found.
  292. *
  293. * @return void
  294. */
  295. protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  296. {
  297. }//end processVariableInString()
  298. }//end class
  299. ?>