PageRenderTime 31ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/_cs/DokuWiki/Sniffs/WhiteSpace/ScopeIndentSniff.php

https://gitlab.com/michield/dokuwiki
PHP | 319 lines | 155 code | 46 blank | 118 comment | 40 complexity | a8e5b088a48087da8c6f1e1910754212 MD5 | raw file
  1. <?php
  2. /**
  3. * DokuWiki_Sniffs_Whitespace_ScopeIndentSniff based on
  4. * Generic_Sniffs_Whitespace_ScopeIndentSniff.
  5. *
  6. * PHP version 5
  7. *
  8. * @category PHP
  9. * @package PHP_CodeSniffer
  10. * @author Andreas Gohr <andi@splitbrain.org>
  11. * @author Greg Sherwood <gsherwood@squiz.net>
  12. * @author Marc McIntyre <mmcintyre@squiz.net>
  13. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  14. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  15. * @version CVS: $Id: ScopeIndentSniff.php 270281 2008-12-02 02:38:34Z squiz $
  16. * @link http://pear.php.net/package/PHP_CodeSniffer
  17. */
  18. /**
  19. * Generic_Sniffs_Whitespace_ScopeIndentSniff.
  20. *
  21. * Checks that control structures are structured correctly, and their content
  22. * is indented correctly.
  23. *
  24. * @category PHP
  25. * @package PHP_CodeSniffer
  26. * @author Greg Sherwood <gsherwood@squiz.net>
  27. * @author Marc McIntyre <mmcintyre@squiz.net>
  28. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  29. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  30. * @version Release: 1.2.0
  31. * @link http://pear.php.net/package/PHP_CodeSniffer
  32. */
  33. class DokuWiki_Sniffs_WhiteSpace_ScopeIndentSniff implements PHP_CodeSniffer_Sniff
  34. {
  35. /**
  36. * The number of spaces code should be indented.
  37. *
  38. * @var int
  39. */
  40. protected $indent = 4;
  41. /**
  42. * Does the indent need to be exactly right.
  43. *
  44. * If TRUE, indent needs to be exactly $ident spaces. If FALSE,
  45. * indent needs to be at least $ident spaces (but can be more).
  46. *
  47. * @var bool
  48. */
  49. protected $exact = false;
  50. /**
  51. * Any scope openers that should not cause an indent.
  52. *
  53. * @var array(int)
  54. */
  55. protected $nonIndentingScopes = array();
  56. /**
  57. * Returns an array of tokens this test wants to listen for.
  58. *
  59. * @return array
  60. */
  61. public function register()
  62. {
  63. return PHP_CodeSniffer_Tokens::$scopeOpeners;
  64. }//end register()
  65. /**
  66. * Processes this test, when one of its tokens is encountered.
  67. *
  68. * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
  69. * @param int $stackPtr The position of the current token
  70. * in the stack passed in $tokens.
  71. *
  72. * @return void
  73. */
  74. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  75. {
  76. $tokens = $phpcsFile->getTokens();
  77. // If this is an inline condition (ie. there is no scope opener), then
  78. // return, as this is not a new scope.
  79. if (isset($tokens[$stackPtr]['scope_opener']) === false) {
  80. return;
  81. }
  82. if ($tokens[$stackPtr]['code'] === T_ELSE) {
  83. $next = $phpcsFile->findNext(
  84. PHP_CodeSniffer_Tokens::$emptyTokens,
  85. ($stackPtr + 1),
  86. null,
  87. true
  88. );
  89. // We will handle the T_IF token in another call to process.
  90. if ($tokens[$next]['code'] === T_IF) {
  91. return;
  92. }
  93. }
  94. // Find the first token on this line.
  95. $firstToken = $stackPtr;
  96. for ($i = $stackPtr; $i >= 0; $i--) {
  97. // Record the first code token on the line.
  98. if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
  99. $firstToken = $i;
  100. }
  101. // It's the start of the line, so we've found our first php token.
  102. if ($tokens[$i]['column'] === 1) {
  103. break;
  104. }
  105. }
  106. // Based on the conditions that surround this token, determine the
  107. // indent that we expect this current content to be.
  108. $expectedIndent = $this->calculateExpectedIndent($tokens, $firstToken);
  109. if ($tokens[$firstToken]['column'] !== $expectedIndent) {
  110. if($this->exact || $tokens[$firstToken]['column'] < $expectedIndent){
  111. $error = 'Line indented incorrectly; expected ';
  112. $error .= ($expectedIndent - 1).' spaces, found ';
  113. $error .= ($tokens[$firstToken]['column'] - 1);
  114. $phpcsFile->addError($error, $stackPtr);
  115. }elseif((($tokens[$firstToken]['column'] - 1) % $this->indent)){
  116. $error = 'Line indented not by multiple of '.$this->indent.'; expected ';
  117. $error .= ($expectedIndent - 1).' spaces, found ';
  118. $error .= ($tokens[$firstToken]['column'] - 1);
  119. $phpcsFile->addError($error, $stackPtr);
  120. }
  121. }
  122. $scopeOpener = $tokens[$stackPtr]['scope_opener'];
  123. $scopeCloser = $tokens[$stackPtr]['scope_closer'];
  124. // Some scopes are expected not to have indents.
  125. if (in_array($tokens[$firstToken]['code'], $this->nonIndentingScopes) === false) {
  126. $indent = ($expectedIndent + $this->indent);
  127. } else {
  128. $indent = $expectedIndent;
  129. }
  130. $newline = false;
  131. $commentOpen = false;
  132. $inHereDoc = false;
  133. // Only loop over the content beween the opening and closing brace, not
  134. // the braces themselves.
  135. for ($i = ($scopeOpener + 1); $i < $scopeCloser; $i++) {
  136. // If this token is another scope, skip it as it will be handled by
  137. // another call to this sniff.
  138. if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$scopeOpeners) === true) {
  139. if (isset($tokens[$i]['scope_opener']) === true) {
  140. $i = $tokens[$i]['scope_closer'];
  141. } else {
  142. // If this token does not have a scope_opener indice, then
  143. // it's probably an inline scope, so let's skip to the next
  144. // semicolon. Inline scopes include inline if's, abstract
  145. // methods etc.
  146. $nextToken = $phpcsFile->findNext(T_SEMICOLON, $i, $scopeCloser);
  147. if ($nextToken !== false) {
  148. $i = $nextToken;
  149. }
  150. }
  151. continue;
  152. }
  153. // If this is a HEREDOC then we need to ignore it as the
  154. // whitespace before the contents within the HEREDOC are
  155. // considered part of the content.
  156. if ($tokens[$i]['code'] === T_START_HEREDOC) {
  157. $inHereDoc = true;
  158. continue;
  159. } else if ($inHereDoc === true) {
  160. if ($tokens[$i]['code'] === T_END_HEREDOC) {
  161. $inHereDoc = false;
  162. }
  163. continue;
  164. }
  165. if ($tokens[$i]['column'] === 1) {
  166. // We started a newline.
  167. $newline = true;
  168. }
  169. if ($newline === true && $tokens[$i]['code'] !== T_WHITESPACE) {
  170. // If we started a newline and we find a token that is not
  171. // whitespace, then this must be the first token on the line that
  172. // must be indented.
  173. $newline = false;
  174. $firstToken = $i;
  175. $column = $tokens[$firstToken]['column'];
  176. // Special case for non-PHP code.
  177. if ($tokens[$firstToken]['code'] === T_INLINE_HTML) {
  178. $trimmedContentLength
  179. = strlen(ltrim($tokens[$firstToken]['content']));
  180. if ($trimmedContentLength === 0) {
  181. continue;
  182. }
  183. $contentLength = strlen($tokens[$firstToken]['content']);
  184. $column = ($contentLength - $trimmedContentLength + 1);
  185. }
  186. // Check to see if this constant string spans multiple lines.
  187. // If so, then make sure that the strings on lines other than the
  188. // first line are indented appropriately, based on their whitespace.
  189. if (in_array($tokens[$firstToken]['code'], PHP_CodeSniffer_Tokens::$stringTokens) === true) {
  190. if (in_array($tokens[($firstToken - 1)]['code'], PHP_CodeSniffer_Tokens::$stringTokens) === true) {
  191. // If we find a string that directly follows another string
  192. // then its just a string that spans multiple lines, so we
  193. // don't need to check for indenting.
  194. continue;
  195. }
  196. }
  197. // This is a special condition for T_DOC_COMMENT and C-style
  198. // comments, which contain whitespace between each line.
  199. $comments = array(
  200. T_COMMENT,
  201. T_DOC_COMMENT
  202. );
  203. if (in_array($tokens[$firstToken]['code'], $comments) === true) {
  204. $content = trim($tokens[$firstToken]['content']);
  205. if (preg_match('|^/\*|', $content) !== 0) {
  206. // Check to see if the end of the comment is on the same line
  207. // as the start of the comment. If it is, then we don't
  208. // have to worry about opening a comment.
  209. if (preg_match('|\*/$|', $content) === 0) {
  210. // We don't have to calculate the column for the
  211. // start of the comment as there is a whitespace
  212. // token before it.
  213. $commentOpen = true;
  214. }
  215. } else if ($commentOpen === true) {
  216. if ($content === '') {
  217. // We are in a comment, but this line has nothing on it
  218. // so let's skip it.
  219. continue;
  220. }
  221. $contentLength = strlen($tokens[$firstToken]['content']);
  222. $trimmedContentLength
  223. = strlen(ltrim($tokens[$firstToken]['content']));
  224. $column = ($contentLength - $trimmedContentLength + 1);
  225. if (preg_match('|\*/$|', $content) !== 0) {
  226. $commentOpen = false;
  227. }
  228. }//end if
  229. }//end if
  230. // The token at the start of the line, needs to have its' column
  231. // greater than the relative indent we set above. If it is less,
  232. // an error should be shown.
  233. if ($column !== $indent) {
  234. if ($this->exact === true || $column < $indent) {
  235. $error = 'Line indented incorrectly; expected ';
  236. if ($this->exact === false) {
  237. $error .= 'at least ';
  238. }
  239. $error .= ($indent - 1).' spaces, found ';
  240. $error .= ($column - 1);
  241. $phpcsFile->addError($error, $firstToken);
  242. }
  243. }
  244. }//end if
  245. }//end for
  246. }//end process()
  247. /**
  248. * Calculates the expected indent of a token.
  249. *
  250. * @param array $tokens The stack of tokens for this file.
  251. * @param int $stackPtr The position of the token to get indent for.
  252. *
  253. * @return int
  254. */
  255. protected function calculateExpectedIndent(array $tokens, $stackPtr)
  256. {
  257. $conditionStack = array();
  258. // Empty conditions array (top level structure).
  259. if (empty($tokens[$stackPtr]['conditions']) === true) {
  260. return 1;
  261. }
  262. $tokenConditions = $tokens[$stackPtr]['conditions'];
  263. foreach ($tokenConditions as $id => $condition) {
  264. // If it's an indenting scope ie. it's not in our array of
  265. // scopes that don't indent, add it to our condition stack.
  266. if (in_array($condition, $this->nonIndentingScopes) === false) {
  267. $conditionStack[$id] = $condition;
  268. }
  269. }
  270. return ((count($conditionStack) * $this->indent) + 1);
  271. }//end calculateExpectedIndent()
  272. }//end class
  273. ?>