PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/share/pear/PHP/CodeSniffer/Standards_Backup/Squiz/Sniffs/Commenting/DocCommentAlignmentSniff.php

https://github.com/amumu/modev
PHP | 149 lines | 80 code | 18 blank | 51 comment | 14 complexity | 1dc417c86405135c2e7410f72b8f33d9 MD5 | raw file
  1. <?php
  2. /**
  3. * Squiz_Sniffs_Commenting_EmptyCatchCommentSniff.
  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: DocCommentAlignmentSniff.php 252415 2008-02-06 22:48:09Z squiz $
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. /**
  17. * Squiz_Sniffs_Commenting_DocCommentAlignmentSniff.
  18. *
  19. * Tests that the stars in a doc comment align correctly.
  20. *
  21. * @category PHP
  22. * @package PHP_CodeSniffer
  23. * @author Greg Sherwood <gsherwood@squiz.net>
  24. * @author Marc McIntyre <mmcintyre@squiz.net>
  25. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  26. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  27. * @version Release: 1.2.2
  28. * @link http://pear.php.net/package/PHP_CodeSniffer
  29. */
  30. class Squiz_Sniffs_Commenting_DocCommentAlignmentSniff implements PHP_CodeSniffer_Sniff
  31. {
  32. /**
  33. * Returns an array of tokens this test wants to listen for.
  34. *
  35. * @return array
  36. */
  37. public function register()
  38. {
  39. return array(T_DOC_COMMENT);
  40. }//end register()
  41. /**
  42. * Processes this test, when one of its tokens is encountered.
  43. *
  44. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  45. * @param int $stackPtr The position of the current token
  46. * in the stack passed in $tokens.
  47. *
  48. * @return void
  49. */
  50. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  51. {
  52. $tokens = $phpcsFile->getTokens();
  53. // We are only interested in function/class/interface doc block comments.
  54. $nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
  55. $ignore = array(
  56. T_CLASS,
  57. T_INTERFACE,
  58. T_FUNCTION,
  59. T_PUBLIC,
  60. T_PRIVATE,
  61. T_PROTECTED,
  62. T_STATIC,
  63. T_ABSTRACT,
  64. );
  65. if (in_array($tokens[$nextToken]['code'], $ignore) === false) {
  66. // Could be a file comment.
  67. $prevToken = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
  68. if ($tokens[$prevToken]['code'] !== T_OPEN_TAG) {
  69. return;
  70. }
  71. }
  72. // We only want to get the first comment in a block. If there is
  73. // a comment on the line before this one, return.
  74. $docComment = $phpcsFile->findPrevious(T_DOC_COMMENT, ($stackPtr - 1));
  75. if ($docComment !== false) {
  76. if ($tokens[$docComment]['line'] === ($tokens[$stackPtr]['line'] - 1)) {
  77. return;
  78. }
  79. }
  80. $comments = array($stackPtr);
  81. $currentComment = $stackPtr;
  82. $lastComment = $stackPtr;
  83. while (($currentComment = $phpcsFile->findNext(T_DOC_COMMENT, ($currentComment + 1))) !== false) {
  84. if ($tokens[$lastComment]['line'] === ($tokens[$currentComment]['line'] - 1)) {
  85. $comments[] = $currentComment;
  86. $lastComment = $currentComment;
  87. } else {
  88. break;
  89. }
  90. }
  91. // The $comments array now contains pointers to each token in the
  92. // comment block.
  93. $requiredColumn = strpos($tokens[$stackPtr]['content'], '*');
  94. $requiredColumn += $tokens[$stackPtr]['column'];
  95. foreach ($comments as $commentPointer) {
  96. // Check the spacing after each asterisk.
  97. $content = $tokens[$commentPointer]['content'];
  98. $firstChar = substr($content, 0, 1);
  99. $lastChar = substr($content, -1);
  100. if ($firstChar !== '/' && $lastChar !== '/') {
  101. $matches = array();
  102. preg_match('|^(\s+)?\*(\s+)?@|', $content, $matches);
  103. if (empty($matches) === false) {
  104. if (isset($matches[2]) === false) {
  105. $error = "Expected 1 space between asterisk and tag; 0 found";
  106. $phpcsFile->addError($error, $commentPointer);
  107. } else {
  108. $length = strlen($matches[2]);
  109. if ($length !== 1) {
  110. $error = "Expected 1 space between asterisk and tag; $length found";
  111. $phpcsFile->addError($error, $commentPointer);
  112. }
  113. }
  114. }
  115. }//end foreach
  116. // Check the alignment of each asterisk.
  117. $currentColumn = strpos($content, '*');
  118. $currentColumn += $tokens[$commentPointer]['column'];
  119. if ($currentColumn === $requiredColumn) {
  120. // Star is aligned correctly.
  121. continue;
  122. }
  123. $expected = ($requiredColumn - 1);
  124. $expected .= ($expected === 1) ? ' space' : ' spaces';
  125. $found = ($currentColumn - 1);
  126. $error = "Expected $expected before asterisk; $found found";
  127. $phpcsFile->addError($error, $commentPointer);
  128. }//end foreach
  129. }//end process()
  130. }//end class
  131. ?>