PageRenderTime 70ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/script/lib/PHP/CodeSniffer/Standards/Squiz/Sniffs/PHP/CommentedOutCodeSniff.php

https://bitbucket.org/chamilo/chamilo/
PHP | 214 lines | 97 code | 41 blank | 76 comment | 22 complexity | cef3ff928a46645fd8a827b9eada5d82 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * Squiz_Sniffs_PHP_CommentedOutCodeSniff.
  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: CommentedOutCodeSniff.php 292392 2009-12-21 00:47:28Z squiz $
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. /**
  17. * Squiz_Sniffs_PHP_CommentedOutCodeSniff.
  18. *
  19. * Warn about commented out code.
  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_PHP_CommentedOutCodeSniff implements PHP_CodeSniffer_Sniff
  31. {
  32. /**
  33. * A list of tokenizers this sniff supports.
  34. *
  35. * @var array
  36. */
  37. public $supportedTokenizers = array(
  38. 'PHP',
  39. 'CSS',
  40. );
  41. /**
  42. * If a comment is more than $maxPercentage% code, a warning will be shown.
  43. *
  44. * @var int
  45. */
  46. protected $maxPercentage = 35;
  47. /**
  48. * Returns an array of tokens this test wants to listen for.
  49. *
  50. * @return array
  51. */
  52. public function register()
  53. {
  54. return PHP_CodeSniffer_Tokens::$commentTokens;
  55. }//end register()
  56. /**
  57. * Processes this test, when one of its tokens is encountered.
  58. *
  59. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  60. * @param int $stackPtr The position of the current token
  61. * in the stack passed in $tokens.
  62. *
  63. * @return void
  64. */
  65. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  66. {
  67. $tokens = $phpcsFile->getTokens();
  68. // Process whole comment blocks at once, so skip all but the first token.
  69. if ($stackPtr > 0 && $tokens[$stackPtr]['code'] === $tokens[($stackPtr - 1)]['code']) {
  70. return;
  71. }
  72. // Ignore comments at the end of code blocks.
  73. if (substr($tokens[$stackPtr]['content'], 0, 6) === '//end ') {
  74. return;
  75. }
  76. $content = '';
  77. if ($phpcsFile->tokenizerType === 'PHP') {
  78. $content = '<?php ';
  79. }
  80. for ($i = $stackPtr; $i < $phpcsFile->numTokens; $i++) {
  81. if ($tokens[$stackPtr]['code'] !== $tokens[$i]['code']) {
  82. break;
  83. }
  84. /*
  85. Trim as much off the comment as possible so we don't
  86. have additional whitespace tokens or comment tokens
  87. */
  88. $tokenContent = trim($tokens[$i]['content']);
  89. if (substr($tokenContent, 0, 2) === '//') {
  90. $tokenContent = substr($tokenContent, 2);
  91. }
  92. if (substr($tokenContent, 0, 1) === '#') {
  93. $tokenContent = substr($tokenContent, 1);
  94. }
  95. if (substr($tokenContent, 0, 3) === '/**') {
  96. $tokenContent = substr($tokenContent, 3);
  97. }
  98. if (substr($tokenContent, 0, 2) === '/*') {
  99. $tokenContent = substr($tokenContent, 2);
  100. }
  101. if (substr($tokenContent, -2) === '*/') {
  102. $tokenContent = substr($tokenContent, 0, -2);
  103. }
  104. if (substr($tokenContent, 0, 1) === '*') {
  105. $tokenContent = substr($tokenContent, 1);
  106. }
  107. $content .= $tokenContent.$phpcsFile->eolChar;
  108. }//end for
  109. $content = trim($content);
  110. if ($phpcsFile->tokenizerType === 'PHP') {
  111. $content .= ' ?>';
  112. }
  113. // Quite a few comments use multiple dashes, equals signs etc
  114. // to frame comments and licence headers.
  115. $content = preg_replace('/[-=*]+/', '-', $content);
  116. $stringTokens = PHP_CodeSniffer_File::tokenizeString($content, $phpcsFile->tokenizer, $phpcsFile->eolChar);
  117. $emptyTokens = array(
  118. T_WHITESPACE,
  119. T_STRING,
  120. T_STRING_CONCAT,
  121. T_ENCAPSED_AND_WHITESPACE,
  122. T_NONE,
  123. );
  124. $numTokens = count($stringTokens);
  125. /*
  126. We know what the first two and last two tokens should be
  127. (because we put them there) so ignore this comment if those
  128. tokens were not parsed correctly. It obvously means this is not
  129. valid code.
  130. */
  131. // First token is always the opening PHP tag.
  132. if ($stringTokens[0]['code'] !== T_OPEN_TAG) {
  133. return;
  134. }
  135. // Last token is always the closing PHP tag.
  136. if ($stringTokens[($numTokens - 1)]['code'] !== T_CLOSE_TAG) {
  137. return;
  138. }
  139. // Second last token is always whitespace or a comment, depending
  140. // on the code inside the comment.
  141. if (in_array($stringTokens[($numTokens - 2)]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
  142. return;
  143. }
  144. $numComment = 0;
  145. $numCode = 0;
  146. for ($i = 0; $i < $numTokens; $i++) {
  147. if (in_array($stringTokens[$i]['code'], $emptyTokens) === true) {
  148. // Looks like comment.
  149. $numComment++;
  150. } else {
  151. // Looks like code.
  152. $numCode++;
  153. }
  154. }
  155. // We subtract 3 from the token number so we ignore the start/end tokens
  156. // and their surrounding whitespace. We take 2 off the number of code
  157. // tokens so we ignore the start/end tokens.
  158. if ($numTokens > 3) {
  159. $numTokens -= 3;
  160. }
  161. if ($numCode >= 2) {
  162. $numCode -= 2;
  163. }
  164. $percentCode = ceil((($numCode / $numTokens) * 100));
  165. if ($percentCode > $this->maxPercentage) {
  166. // Just in case.
  167. $percentCode = min(100, $percentCode);
  168. $error = "This comment is ${percentCode}% valid code; is this commented out code?";
  169. $phpcsFile->addWarning($error, $stackPtr);
  170. }
  171. }//end process()
  172. }//end class
  173. ?>