PageRenderTime 73ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/yousafsyed/easternglamor
PHP | 564 lines | 343 code | 78 blank | 143 comment | 79 complexity | 9fe0e73040bbae8f3c7f1b90977a6372 MD5 | raw file
  1. <?php
  2. /**
  3. * Parses and verifies the file 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_CommentParser_ClassCommentParser', true) === false) {
  16. throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_CommentParser_ClassCommentParser not found');
  17. }
  18. /**
  19. * Parses and verifies the file doc comment.
  20. *
  21. * Verifies that :
  22. * <ul>
  23. * <li>A file doc comment exists.</li>
  24. * <li>There is no blank line between the open tag and the file comment.</li>
  25. * <li>Short description ends with a full stop.</li>
  26. * <li>There is a blank line after the short description.</li>
  27. * <li>Each paragraph of the long description ends with a full stop.</li>
  28. * <li>There is a blank line between the description and the tags.</li>
  29. * <li>Check the order, indentation and content of each tag.</li>
  30. * <li>There is exactly one blank line after the file comment.</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_FileCommentSniff implements PHP_CodeSniffer_Sniff
  43. {
  44. /**
  45. * A list of tokenizers this sniff supports.
  46. *
  47. * @var array
  48. */
  49. public $supportedTokenizers = array(
  50. 'PHP',
  51. 'JS',
  52. );
  53. /**
  54. * The header comment parser for the current file.
  55. *
  56. * @var PHP_CodeSniffer_Comment_Parser_ClassCommentParser
  57. */
  58. protected $commentParser = null;
  59. /**
  60. * The current PHP_CodeSniffer_File object we are processing.
  61. *
  62. * @var PHP_CodeSniffer_File
  63. */
  64. protected $currentFile = null;
  65. /**
  66. * Returns an array of tokens this test wants to listen for.
  67. *
  68. * @return array
  69. */
  70. public function register()
  71. {
  72. return array(T_OPEN_TAG);
  73. }//end register()
  74. /**
  75. * Processes this test, when one of its tokens is encountered.
  76. *
  77. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  78. * @param int $stackPtr The position of the current token
  79. * in the stack passed in $tokens.
  80. *
  81. * @return void
  82. */
  83. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  84. {
  85. $this->currentFile = $phpcsFile;
  86. // We are only interested if this is the first open tag.
  87. if ($stackPtr !== 0) {
  88. if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) {
  89. return;
  90. }
  91. }
  92. $tokens = $phpcsFile->getTokens();
  93. $errorToken = ($stackPtr + 1);
  94. if (isset($tokens[$errorToken]) === false) {
  95. $errorToken--;
  96. }
  97. // Find the next non whitespace token.
  98. $commentStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
  99. if ($tokens[$commentStart]['code'] === T_CLOSE_TAG) {
  100. // We are only interested if this is the first open tag.
  101. return;
  102. } else if ($tokens[$commentStart]['code'] === T_COMMENT) {
  103. $phpcsFile->addError('You must use "/**" style comments for a file comment', $errorToken, 'WrongStyle');
  104. return;
  105. } else if ($commentStart === false || $tokens[$commentStart]['code'] !== T_DOC_COMMENT) {
  106. $phpcsFile->addError('Missing file doc comment', $errorToken, 'Missing');
  107. return;
  108. }
  109. // Extract the header comment docblock.
  110. $commentEnd = ($phpcsFile->findNext(T_DOC_COMMENT, ($commentStart + 1), null, true) - 1);
  111. // Check if there is only 1 doc comment between the open tag and class token.
  112. $nextToken = array(
  113. T_ABSTRACT,
  114. T_CLASS,
  115. T_DOC_COMMENT,
  116. );
  117. $commentNext = $phpcsFile->findNext($nextToken, ($commentEnd + 1));
  118. if ($commentNext !== false && $tokens[$commentNext]['code'] !== T_DOC_COMMENT) {
  119. // Found a class token right after comment doc block.
  120. $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), $commentNext, false, $phpcsFile->eolChar);
  121. if ($newlineToken !== false) {
  122. $newlineToken = $phpcsFile->findNext(T_WHITESPACE, ($newlineToken + 1), $commentNext, false, $phpcsFile->eolChar);
  123. if ($newlineToken === false) {
  124. // No blank line between the class token and the doc block.
  125. // The doc block is most likely a class comment.
  126. $phpcsFile->addError('Missing file doc comment', $errorToken, 'Missing');
  127. return;
  128. }
  129. }
  130. }
  131. // No blank line between the open tag and the file comment.
  132. $blankLineBefore = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, false, $phpcsFile->eolChar);
  133. if ($blankLineBefore !== false && $blankLineBefore < $commentStart) {
  134. $error = 'Extra newline found after the open tag';
  135. $phpcsFile->addError($error, $stackPtr, 'SpacingAfterOpen');
  136. }
  137. // Exactly one blank line after the file comment.
  138. $nextTokenStart = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), null, true);
  139. if ($nextTokenStart !== false) {
  140. $blankLineAfter = 0;
  141. for ($i = ($commentEnd + 1); $i < $nextTokenStart; $i++) {
  142. if ($tokens[$i]['code'] === T_WHITESPACE && $tokens[$i]['content'] === $phpcsFile->eolChar) {
  143. $blankLineAfter++;
  144. }
  145. }
  146. if ($blankLineAfter !== 2) {
  147. $error = 'There must be exactly one blank line after the file comment';
  148. $phpcsFile->addError($error, ($commentEnd + 1), 'SpacingAfterComment');
  149. }
  150. }
  151. $commentString = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart + 1));
  152. // Parse the header comment docblock.
  153. try {
  154. $this->commentParser = new PHP_CodeSniffer_CommentParser_ClassCommentParser($commentString, $phpcsFile);
  155. $this->commentParser->parse();
  156. } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
  157. $line = ($e->getLineWithinComment() + $commentStart);
  158. $phpcsFile->addError($e->getMessage(), $line, 'Exception');
  159. return;
  160. }
  161. $comment = $this->commentParser->getComment();
  162. if (is_null($comment) === true) {
  163. $error = 'File doc comment is empty';
  164. $phpcsFile->addError($error, $commentStart, 'Empty');
  165. return;
  166. }
  167. // The first line of the comment should just be the /** code.
  168. $eolPos = strpos($commentString, $phpcsFile->eolChar);
  169. $firstLine = substr($commentString, 0, $eolPos);
  170. if ($firstLine !== '/**') {
  171. $error = 'The open comment tag must be the only content on the line';
  172. $phpcsFile->addError($error, $commentStart, 'ContentAfterOpen');
  173. }
  174. // No extra newline before short description.
  175. $short = $comment->getShortComment();
  176. $newlineCount = 0;
  177. $newlineSpan = strspn($short, $phpcsFile->eolChar);
  178. if ($short !== '' && $newlineSpan > 0) {
  179. $error = 'Extra newline(s) found before file comment short description';
  180. $phpcsFile->addError($error, ($commentStart + 1), 'SpacingBeforeShort');
  181. }
  182. $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1);
  183. // Exactly one blank line between short and long description.
  184. $long = $comment->getLongComment();
  185. if (empty($long) === false) {
  186. $between = $comment->getWhiteSpaceBetween();
  187. $newlineBetween = substr_count($between, $phpcsFile->eolChar);
  188. if ($newlineBetween !== 2) {
  189. $error = 'There must be exactly one blank line between descriptions in file comment';
  190. $phpcsFile->addError($error, ($commentStart + $newlineCount + 1), 'SpacingBetween');
  191. }
  192. $newlineCount += $newlineBetween;
  193. $testLong = trim($long);
  194. if (preg_match('|\p{Lu}|u', $testLong[0]) === 0) {
  195. $error = 'File comment long description must start with a capital letter';
  196. $phpcsFile->addError($error, ($commentStart + $newlineCount), 'LongNotCapital');
  197. }
  198. }//end if
  199. // Exactly one blank line before tags.
  200. $tags = $this->commentParser->getTagOrders();
  201. if (count($tags) > 1) {
  202. $newlineSpan = $comment->getNewlineAfter();
  203. if ($newlineSpan !== 2) {
  204. $error = 'There must be exactly one blank line before the tags in file comment';
  205. if ($long !== '') {
  206. $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1);
  207. }
  208. $phpcsFile->addError($error, ($commentStart + $newlineCount), 'SpacingBeforeTags');
  209. $short = rtrim($short, $phpcsFile->eolChar.' ');
  210. }
  211. }
  212. // Short description must be single line and end with a full stop.
  213. $testShort = trim($short);
  214. if ($testShort === '') {
  215. $error = 'Missing short description in file comment';
  216. $phpcsFile->addError($error, ($commentStart + 1), 'MissingShort');
  217. } else {
  218. $lastChar = $testShort[(strlen($testShort) - 1)];
  219. if (substr_count($testShort, $phpcsFile->eolChar) !== 0) {
  220. $error = 'File comment short description must be on a single line';
  221. $phpcsFile->addError($error, ($commentStart + 1), 'ShortSingleLine');
  222. }
  223. if (preg_match('|\p{Lu}|u', $testShort[0]) === 0) {
  224. $error = 'File comment short description must start with a capital letter';
  225. $phpcsFile->addError($error, ($commentStart + 1), 'ShortNotCapital');
  226. }
  227. if ($lastChar !== '.') {
  228. $error = 'File comment short description must end with a full stop';
  229. $phpcsFile->addError($error, ($commentStart + 1), 'ShortFullStop');
  230. }
  231. }//end if
  232. // Check each tag.
  233. $this->processTags($commentStart, $commentEnd);
  234. // The last content should be a newline and the content before
  235. // that should not be blank. If there is more blank space
  236. // then they have additional blank lines at the end of the comment.
  237. $words = $this->commentParser->getWords();
  238. $lastPos = (count($words) - 1);
  239. if (trim($words[($lastPos - 1)]) !== ''
  240. || strpos($words[($lastPos - 1)], $this->currentFile->eolChar) === false
  241. || trim($words[($lastPos - 2)]) === ''
  242. ) {
  243. $error = 'Additional blank lines found at end of file comment';
  244. $this->currentFile->addError($error, $commentEnd, 'SpacingAfter');
  245. }
  246. }//end process()
  247. /**
  248. * Processes each required or optional tag.
  249. *
  250. * @param int $commentStart The position in the stack where the comment started.
  251. * @param int $commentEnd The position in the stack where the comment ended.
  252. *
  253. * @return void
  254. */
  255. protected function processTags($commentStart, $commentEnd)
  256. {
  257. // Required tags in correct order.
  258. $tags = array(
  259. 'package' => 'precedes @subpackage',
  260. 'subpackage' => 'follows @package',
  261. 'author' => 'follows @subpackage',
  262. 'copyright' => 'follows @author',
  263. );
  264. $foundTags = $this->commentParser->getTagOrders();
  265. $errorPos = 0;
  266. $orderIndex = 0;
  267. $longestTag = 0;
  268. $indentation = array();
  269. foreach ($tags as $tag => $orderText) {
  270. // Required tag missing.
  271. if (in_array($tag, $foundTags) === false) {
  272. $error = 'Missing @%s tag in file comment';
  273. $data = array($tag);
  274. $this->currentFile->addError($error, $commentEnd, 'Missing'.ucfirst($tag).'Tag', $data);
  275. continue;
  276. }
  277. // Get the line number for current tag.
  278. $tagName = ucfirst($tag);
  279. if ($tagName === 'Author' || $tagName === 'Copyright') {
  280. // These tags are different because they return an array.
  281. $tagName .= 's';
  282. }
  283. // Work out the line number for this tag.
  284. $getMethod = 'get'.$tagName;
  285. $tagElement = $this->commentParser->$getMethod();
  286. if (is_null($tagElement) === true || empty($tagElement) === true) {
  287. continue;
  288. } else if (is_array($tagElement) === true && empty($tagElement) === false) {
  289. $tagElement = $tagElement[0];
  290. }
  291. $errorPos = ($commentStart + $tagElement->getLine());
  292. // Make sure there is no duplicate tag.
  293. $foundIndexes = array_keys($foundTags, $tag);
  294. if (count($foundIndexes) > 1) {
  295. $error = 'Only 1 @%s tag is allowed in file comment';
  296. $data = array($tag);
  297. $this->currentFile->addError($error, $errorPos, 'Duplicate'.ucfirst($tag).'Tag', $data);
  298. }
  299. // Check tag order.
  300. if ($foundIndexes[0] > $orderIndex) {
  301. $orderIndex = $foundIndexes[0];
  302. } else {
  303. $error = 'The @%s tag is in the wrong order; the tag %s';
  304. $data = array(
  305. $tag,
  306. $orderText,
  307. );
  308. $this->currentFile->addError($error, $errorPos, ucfirst($tag).'TagOrder', $data);
  309. }
  310. // Store the indentation of each tag.
  311. $len = strlen($tag);
  312. if ($len > $longestTag) {
  313. $longestTag = $len;
  314. }
  315. $indentation[] = array(
  316. 'tag' => $tag,
  317. 'errorPos' => $errorPos,
  318. 'space' => $this->getIndentation($tag, $tagElement),
  319. );
  320. $method = 'process'.$tagName;
  321. if (method_exists($this, $method) === true) {
  322. // Process each tag if a method is defined.
  323. call_user_func(array($this, $method), $errorPos);
  324. } else {
  325. $tagElement->process($this->currentFile, $commentStart, 'file');
  326. }
  327. }//end foreach
  328. // Check tag indentation.
  329. foreach ($indentation as $indentInfo) {
  330. $tagName = ucfirst($indentInfo['tag']);
  331. if ($tagName === 'Author') {
  332. $tagName .= 's';
  333. }
  334. if ($indentInfo['space'] !== 0 && $indentInfo['space'] !== ($longestTag + 1)) {
  335. $expected = ($longestTag - strlen($indentInfo['tag']) + 1);
  336. $space = ($indentInfo['space'] - strlen($indentInfo['tag']));
  337. $error = '@%s tag comment indented incorrectly; expected %s spaces but found %s';
  338. $data = array(
  339. $indentInfo['tag'],
  340. $expected,
  341. $space,
  342. );
  343. $this->currentFile->addError($error, $indentInfo['errorPos'], ucfirst($indentInfo['tag']).'TagIndent', $data);
  344. }
  345. }
  346. }//end processTags()
  347. /**
  348. * Get the indentation information of each tag.
  349. *
  350. * @param string $tagName The name of the doc comment element.
  351. * @param PHP_CodeSniffer_CommentParser_DocElement $tagElement The doc comment element.
  352. *
  353. * @return void
  354. */
  355. protected function getIndentation($tagName, $tagElement)
  356. {
  357. if ($tagElement instanceof PHP_CodeSniffer_CommentParser_SingleElement) {
  358. if ($tagElement->getContent() !== '') {
  359. return (strlen($tagName) + substr_count($tagElement->getWhitespaceBeforeContent(), ' '));
  360. }
  361. } else if ($tagElement instanceof PHP_CodeSniffer_CommentParser_PairElement) {
  362. if ($tagElement->getValue() !== '') {
  363. return (strlen($tagName) + substr_count($tagElement->getWhitespaceBeforeValue(), ' '));
  364. }
  365. }
  366. return 0;
  367. }//end getIndentation()
  368. /**
  369. * The package name must be camel-cased.
  370. *
  371. * @param int $errorPos The line number where the error occurs.
  372. *
  373. * @return void
  374. */
  375. protected function processPackage($errorPos)
  376. {
  377. $package = $this->commentParser->getPackage();
  378. if ($package !== null) {
  379. $content = $package->getContent();
  380. if (empty($content) === true) {
  381. $error = 'Content missing for @package tag in file comment';
  382. $this->currentFile->addError($error, $errorPos, 'MissingPackage');
  383. } else if (PHP_CodeSniffer::isUnderscoreName($content) !== true) {
  384. // Package name must be properly camel-cased.
  385. $nameBits = explode('_', str_replace(' ', '', $content));
  386. $firstBit = array_shift($nameBits);
  387. $newName = strtoupper($firstBit{0}).substr($firstBit, 1).'_';
  388. foreach ($nameBits as $bit) {
  389. $newName .= strtoupper($bit{0}).substr($bit, 1).'_';
  390. }
  391. $error = 'Package name "%s" is not valid; consider "%s" instead';
  392. $data = array(
  393. $content,
  394. trim($newName, '_'),
  395. );
  396. $this->currentFile->addError($error, $errorPos, 'IncorrectPackage', $data);
  397. } else if (strpos($content, 'Squiz') === 0) {
  398. // Package name must not start with Squiz.
  399. $newName = substr($content, 5);
  400. $error = 'Package name "%s" is not valid; consider "%s" instead';
  401. $data = array(
  402. $content,
  403. $newName,
  404. );
  405. $this->currentFile->addError($error, $errorPos, 'SquizPackage', $data);
  406. }
  407. }
  408. }//end processPackage()
  409. /**
  410. * The subpackage name must be camel-cased.
  411. *
  412. * @param int $errorPos The line number where the error occurs.
  413. *
  414. * @return void
  415. */
  416. protected function processSubpackage($errorPos)
  417. {
  418. $subpackage = $this->commentParser->getSubpackage();
  419. if ($subpackage !== null) {
  420. $content = $subpackage->getContent();
  421. if (empty($content) === true) {
  422. $error = 'Content missing for @subpackage tag in file comment';
  423. $this->currentFile->addError($error, $errorPos, 'MissingSubpackage');
  424. } else if (PHP_CodeSniffer::isUnderscoreName($content) !== true) {
  425. // Subpackage name must be properly camel-cased.
  426. $nameBits = explode('_', $content);
  427. $firstBit = array_shift($nameBits);
  428. $newName = strtoupper($firstBit{0}).substr($firstBit, 1).'_';
  429. foreach ($nameBits as $bit) {
  430. $newName .= strtoupper($bit{0}).substr($bit, 1).'_';
  431. }
  432. $error = 'Subpackage name "%s" is not valid; consider "%s" instead';
  433. $data = array(
  434. $content,
  435. trim($newName, '_'),
  436. );
  437. $this->currentFile->addError($error, $errorPos, 'IncorrectSubpackage', $data);
  438. }
  439. }
  440. }//end processSubpackage()
  441. /**
  442. * Author tag must be 'Squiz Pty Ltd <mysource4@squiz.net>'.
  443. *
  444. * @param int $errorPos The line number where the error occurs.
  445. *
  446. * @return void
  447. */
  448. protected function processAuthors($errorPos)
  449. {
  450. $authors = $this->commentParser->getAuthors();
  451. if (empty($authors) === false) {
  452. $author = $authors[0];
  453. $content = $author->getContent();
  454. if (empty($content) === true) {
  455. $error = 'Content missing for @author tag in file comment';
  456. $this->currentFile->addError($error, $errorPos, 'MissingAuthor');
  457. } else if ($content !== 'Squiz Pty Ltd <products@squiz.net>') {
  458. $error = 'Expected "Squiz Pty Ltd <products@squiz.net>" for author tag';
  459. $this->currentFile->addError($error, $errorPos, 'IncorrectAuthor');
  460. }
  461. }
  462. }//end processAuthors()
  463. /**
  464. * Copyright tag must be in the form '2006-YYYY Squiz Pty Ltd (ABN 77 084 670 600)'.
  465. *
  466. * @param int $errorPos The line number where the error occurs.
  467. *
  468. * @return void
  469. */
  470. protected function processCopyrights($errorPos)
  471. {
  472. $copyrights = $this->commentParser->getCopyrights();
  473. $copyright = $copyrights[0];
  474. if ($copyright !== null) {
  475. $content = $copyright->getContent();
  476. if (empty($content) === true) {
  477. $error = 'Content missing for @copyright tag in file comment';
  478. $this->currentFile->addError($error, $errorPos, 'MissingCopyright');
  479. } else if (preg_match('/^([0-9]{4})(-[0-9]{4})? (Squiz Pty Ltd \(ABN 77 084 670 600\))$/', $content) === 0) {
  480. $error = 'Expected "xxxx-xxxx Squiz Pty Ltd (ABN 77 084 670 600)" for copyright declaration';
  481. $this->currentFile->addError($error, $errorPos, 'IncorrectCopyright');
  482. }
  483. }
  484. }//end processCopyrights()
  485. }//end class
  486. ?>