PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/build/phpcs/Joomla/Sniffs/WhiteSpace/SuperfluousWhitespaceSniff.php

http://github.com/joomla/joomla-platform
PHP | 258 lines | 142 code | 23 blank | 93 comment | 38 complexity | 48a693c7020391f817313f632121662d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  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 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: SuperfluousWhitespaceSniff.php 509 2011-08-30 02:21:56Z elkuku $
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. /**
  17. * Checks for whitespace.
  18. *
  19. * Checks that.
  20. * <ul>
  21. * <li>No whitespace proceeds the first content of the file.</li>
  22. * <li>No whitespace resides after content on any line.</li>
  23. * <li>There are not two or more empty lines in functions and classes.</li>
  24. * <li>There is a exactly one empty line after the last content of the file.</li>
  25. * </ul>
  26. *
  27. * @category PHP
  28. * @package PHP_CodeSniffer
  29. * @author Greg Sherwood <gsherwood@squiz.net>
  30. * @author Marc McIntyre <mmcintyre@squiz.net>
  31. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  32. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  33. * @version Release: 1.2.2
  34. * @link http://pear.php.net/package/PHP_CodeSniffer
  35. */
  36. class Joomla_Sniffs_WhiteSpace_SuperfluousWhitespaceSniff implements PHP_CodeSniffer_Sniff
  37. {
  38. /**
  39. * A list of tokenizers this sniff supports.
  40. *
  41. * @var array
  42. */
  43. public $supportedTokenizers = array(
  44. 'PHP',
  45. 'JS',
  46. 'CSS',
  47. );
  48. /**
  49. * Returns an array of tokens this test wants to listen for.
  50. *
  51. * @return array
  52. */
  53. public function register()
  54. {
  55. return array(
  56. T_OPEN_TAG,
  57. T_CLOSE_TAG,
  58. T_WHITESPACE,
  59. T_COMMENT,
  60. T_CLOSE_CURLY_BRACKET,
  61. );
  62. }//function
  63. /**
  64. * Processes this sniff, when one of its tokens is encountered.
  65. *
  66. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  67. * @param integer $stackPtr The position of the current token in the stack passed in $tokens.
  68. *
  69. * @return void
  70. */
  71. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  72. {
  73. $tokens = $phpcsFile->getTokens();
  74. if($stackPtr == $phpcsFile->numTokens - 1)
  75. {
  76. //-- We reached the end of the file - without a closing tag
  77. if($tokens[$stackPtr]['code'] == T_WHITESPACE)
  78. {
  79. $lastCode = $tokens[$stackPtr - 1]['code'];
  80. if($lastCode != T_CLOSE_CURLY_BRACKET
  81. && $lastCode !== T_SEMICOLON)
  82. {
  83. //-- If the second last is not a curly bracket or a semicolon
  84. $phpcsFile->addError('Additional whitespace found at end of file', $stackPtr);
  85. }
  86. }
  87. else if(strpos($tokens[$stackPtr]['content'], $phpcsFile->eolChar) === false)
  88. {
  89. //-- Files must end with an empty line
  90. $phpcsFile->addError('Please end your files with an empty line.', $stackPtr);
  91. }
  92. }
  93. if($tokens[$stackPtr]['code'] === T_OPEN_TAG)
  94. {
  95. /*
  96. Check for start of file whitespace.
  97. */
  98. if($phpcsFile->tokenizerType !== 'PHP')
  99. {
  100. // The first token is always the open tag inserted when tokenizsed
  101. // and the second token is always the first piece of content in
  102. // the file. If the second token is whitespace, there was
  103. // whitespace at the start of the file.
  104. if($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE)
  105. {
  106. return;
  107. }
  108. }
  109. else
  110. {
  111. // If its the first token, then there is no space.
  112. if($stackPtr === 0)
  113. {
  114. return;
  115. }
  116. for($i = ($stackPtr - 1); $i >= 0; $i--)
  117. {
  118. // If we find something that isn't inline html
  119. // then there is something previous in the file.
  120. if($tokens[$i]['type'] !== 'T_INLINE_HTML')
  121. {
  122. return;
  123. }
  124. // If we have ended up with inline html make sure it isn't just whitespace.
  125. $tokenContent = trim($tokens[$i]['content']);
  126. if($tokenContent !== '')
  127. {
  128. return;
  129. }
  130. }//for
  131. }//end if
  132. $phpcsFile->addError('Additional whitespace found at start of file', $stackPtr);
  133. }
  134. else if($phpcsFile->numTokens == ($stackPtr - 1))
  135. {
  136. //-- Wereached the end of the file - without a closing tag
  137. die('BB');
  138. }
  139. else if($tokens[$stackPtr]['code'] === T_CLOSE_TAG)
  140. {
  141. /*
  142. Check for end of file whitespace.
  143. */
  144. if($phpcsFile->tokenizerType === 'JS')
  145. {
  146. // The last token is always the close tag inserted when tokenizsed
  147. // and the second last token is always the last piece of content in
  148. // the file. If the second last token is whitespace, there was
  149. // whitespace at the end of the file.
  150. if($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE)
  151. {
  152. return;
  153. }
  154. }
  155. else if($phpcsFile->tokenizerType === 'CSS')
  156. {
  157. // The last two tokens are always the close tag and whitespace
  158. // inserted when tokenizsed and the third last token is always the
  159. // last piece of content in the file. If the third last token is
  160. // whitespace, there was whitespace at the end of the file.
  161. if($tokens[($stackPtr - 3)]['code'] !== T_WHITESPACE)
  162. {
  163. return;
  164. }
  165. // Adjust the pointer to give the correct line number for the error.
  166. $stackPtr -= 2;
  167. }
  168. else
  169. {
  170. if(isset($tokens[($stackPtr + 1)]) === false)
  171. {
  172. // The close PHP token is the last in the file.
  173. return;
  174. }
  175. for($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++)
  176. {
  177. // If we find something that isn't inline html then there
  178. // is more to the file.
  179. if($tokens[$i]['type'] !== 'T_INLINE_HTML')
  180. {
  181. return;
  182. }
  183. // If we have ended up with inline html make sure it
  184. // isn't just whitespace.
  185. $tokenContent = trim($tokens[$i]['content']);
  186. if(empty($tokenContent) === false)
  187. {
  188. return;
  189. }
  190. }//for
  191. }
  192. $phpcsFile->addError('Additional whitespace found at end of file', $stackPtr);
  193. }
  194. else
  195. {
  196. /*
  197. Check for end of line whitespace.
  198. */
  199. if(strpos($tokens[$stackPtr]['content'], $phpcsFile->eolChar) === false)
  200. {
  201. return;
  202. }
  203. $tokenContent = rtrim($tokens[$stackPtr]['content'], $phpcsFile->eolChar);
  204. if(empty($tokenContent) === false)
  205. {
  206. if(preg_match('|^.*\s+$|', $tokenContent) !== 0)
  207. {
  208. $phpcsFile->addError('Whitespace found at end of line', $stackPtr);
  209. }
  210. }
  211. /*
  212. Check for multiple blanks lines in a function or class.
  213. */
  214. if($phpcsFile->hasCondition($stackPtr, T_FUNCTION) === true
  215. || $phpcsFile->hasCondition($stackPtr, T_CLASS) === true)
  216. {
  217. if($tokens[($stackPtr - 1)]['line'] < $tokens[$stackPtr]['line']
  218. && $tokens[($stackPtr - 2)]['line'] === $tokens[($stackPtr - 1)]['line'])
  219. {
  220. // This is an empty line and the line before this one is not
  221. // empty, so this could be the start of a multiple empty
  222. // line block.
  223. $next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr, null, true);
  224. $lines = $tokens[$next]['line'] - $tokens[$stackPtr]['line'];
  225. if($lines > 1)
  226. {
  227. $error = 'Functions and classes must not contain multiple empty lines in a row;'
  228. ." found $lines empty lines";
  229. $phpcsFile->addError($error, $stackPtr);
  230. }
  231. }
  232. }
  233. }//end if
  234. }//function
  235. }//class