/php/pear/PHP/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php

https://gitlab.com/trang1104/portable_project · PHP · 204 lines · 90 code · 30 blank · 84 comment · 28 complexity · 9a3612dd653e0ba95cf9d2cbd93eed9a MD5 · raw file

  1. <?php
  2. /**
  3. * Squiz_Sniffs_WhiteSpace_SuperfluousWhitespaceSniff.
  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-2011 Squiz Pty Ltd (ABN 77 084 670 600)
  12. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  13. * @link http://pear.php.net/package/PHP_CodeSniffer
  14. */
  15. /**
  16. * Squiz_Sniffs_WhiteSpace_SuperfluousWhitespaceSniff.
  17. *
  18. * Checks that no whitespace proceeds the first content of the file, exists
  19. * after the last content of the file, resides after content on any line, or
  20. * are two empty lines in functions.
  21. *
  22. * @category PHP
  23. * @package PHP_CodeSniffer
  24. * @author Greg Sherwood <gsherwood@squiz.net>
  25. * @author Marc McIntyre <mmcintyre@squiz.net>
  26. * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
  27. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  28. * @version Release: 1.3.3
  29. * @link http://pear.php.net/package/PHP_CodeSniffer
  30. */
  31. class Squiz_Sniffs_WhiteSpace_SuperfluousWhitespaceSniff implements PHP_CodeSniffer_Sniff
  32. {
  33. /**
  34. * A list of tokenizers this sniff supports.
  35. *
  36. * @var array
  37. */
  38. public $supportedTokenizers = array(
  39. 'PHP',
  40. 'JS',
  41. 'CSS',
  42. );
  43. /**
  44. * Returns an array of tokens this test wants to listen for.
  45. *
  46. * @return array
  47. */
  48. public function register()
  49. {
  50. return array(
  51. T_OPEN_TAG,
  52. T_CLOSE_TAG,
  53. T_WHITESPACE,
  54. T_COMMENT,
  55. );
  56. }//end register()
  57. /**
  58. * Processes this sniff, when one of its tokens is encountered.
  59. *
  60. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  61. * @param int $stackPtr The position of the current token in the
  62. * stack passed in $tokens.
  63. *
  64. * @return void
  65. */
  66. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  67. {
  68. $tokens = $phpcsFile->getTokens();
  69. if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) {
  70. /*
  71. Check for start of file whitespace.
  72. */
  73. if ($phpcsFile->tokenizerType !== 'PHP') {
  74. // The first token is always the open tag inserted when tokenizsed
  75. // and the second token is always the first piece of content in
  76. // the file. If the second token is whitespace, there was
  77. // whitespace at the start of the file.
  78. if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
  79. return;
  80. }
  81. } else {
  82. // If its the first token, then there is no space.
  83. if ($stackPtr === 0) {
  84. return;
  85. }
  86. for ($i = ($stackPtr - 1); $i >= 0; $i--) {
  87. // If we find something that isn't inline html then there is something previous in the file.
  88. if ($tokens[$i]['type'] !== 'T_INLINE_HTML') {
  89. return;
  90. }
  91. // If we have ended up with inline html make sure it isn't just whitespace.
  92. $tokenContent = trim($tokens[$i]['content']);
  93. if ($tokenContent !== '') {
  94. return;
  95. }
  96. }
  97. }//end if
  98. $phpcsFile->addError('Additional whitespace found at start of file', $stackPtr, 'StartFile');
  99. } else if ($tokens[$stackPtr]['code'] === T_CLOSE_TAG) {
  100. /*
  101. Check for end of file whitespace.
  102. */
  103. if ($phpcsFile->tokenizerType === 'JS') {
  104. // The last token is always the close tag inserted when tokenizsed
  105. // and the second last token is always the last piece of content in
  106. // the file. If the second last token is whitespace, there was
  107. // whitespace at the end of the file.
  108. if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE) {
  109. return;
  110. }
  111. } else if ($phpcsFile->tokenizerType === 'CSS') {
  112. // The last two tokens are always the close tag and whitespace
  113. // inserted when tokenizsed and the third last token is always the
  114. // last piece of content in the file. If the third last token is
  115. // whitespace, there was whitespace at the end of the file.
  116. if ($tokens[($stackPtr - 3)]['code'] !== T_WHITESPACE) {
  117. return;
  118. }
  119. // Adjust the pointer to give the correct line number for the error.
  120. $stackPtr -= 2;
  121. } else {
  122. if (isset($tokens[($stackPtr + 1)]) === false) {
  123. // The close PHP token is the last in the file.
  124. return;
  125. }
  126. for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) {
  127. // If we find something that isn't inline html then there
  128. // is more to the file.
  129. if ($tokens[$i]['type'] !== 'T_INLINE_HTML') {
  130. return;
  131. }
  132. // If we have ended up with inline html make sure it
  133. // isn't just whitespace.
  134. $tokenContent = trim($tokens[$i]['content']);
  135. if (empty($tokenContent) === false) {
  136. return;
  137. }
  138. }
  139. }
  140. $phpcsFile->addError('Additional whitespace found at end of file', $stackPtr, 'EndFile');
  141. } else {
  142. /*
  143. Check for end of line whitespace.
  144. */
  145. if (strpos($tokens[$stackPtr]['content'], $phpcsFile->eolChar) === false) {
  146. return;
  147. }
  148. $tokenContent = rtrim($tokens[$stackPtr]['content'], $phpcsFile->eolChar);
  149. if (empty($tokenContent) === false) {
  150. if (preg_match('|^.*\s+$|', $tokenContent) !== 0) {
  151. $phpcsFile->addError('Whitespace found at end of line', $stackPtr, 'EndLine');
  152. }
  153. }
  154. /*
  155. Check for multiple blanks lines in a function.
  156. */
  157. if ($phpcsFile->hasCondition($stackPtr, T_FUNCTION) === true) {
  158. if ($tokens[($stackPtr - 1)]['line'] < $tokens[$stackPtr]['line'] && $tokens[($stackPtr - 2)]['line'] === $tokens[($stackPtr - 1)]['line']) {
  159. // This is an empty line and the line before this one is not
  160. // empty, so this could be the start of a multiple empty
  161. // line block.
  162. $next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr, null, true);
  163. $lines = $tokens[$next]['line'] - $tokens[$stackPtr]['line'];
  164. if ($lines > 1) {
  165. $error = 'Functions must not contain multiple empty lines in a row; found %s empty lines';
  166. $data = array($lines);
  167. $phpcsFile->addError($error, $stackPtr, 'EmptyLines', $data);
  168. }
  169. }
  170. }
  171. }//end if
  172. }//end process()
  173. }//end class
  174. ?>