PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/script/lib/PHP/CodeSniffer/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 259 lines | 169 code | 34 blank | 56 comment | 33 complexity | 0c04861146d205f68a1d2f23e159da8f 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_Functions_FunctionDeclarationArgumentSpacingSniff.
  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: FunctionDeclarationArgumentSpacingSniff.php 274897 2009-01-29 23:39:52Z squiz $
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. /**
  17. * Squiz_Sniffs_Functions_FunctionDeclarationArgumentSpacingSniff.
  18. *
  19. * Checks that arguments in function declarations are spaced 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_Functions_FunctionDeclarationArgumentSpacingSniff 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_FUNCTION);
  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 in the
  46. * stack passed in $tokens.
  47. *
  48. * @return void
  49. */
  50. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  51. {
  52. $tokens = $phpcsFile->getTokens();
  53. $functionName = $phpcsFile->findNext(array(T_STRING), $stackPtr);
  54. $openBracket = $tokens[$stackPtr]['parenthesis_opener'];
  55. $closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
  56. $multiLine = ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']);
  57. $nextParam = $openBracket;
  58. $params = array();
  59. while (($nextParam = $phpcsFile->findNext(T_VARIABLE, ($nextParam + 1), $closeBracket)) !== false)
  60. {
  61. $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($nextParam + 1), ($closeBracket + 1), true);
  62. if ($nextToken === false)
  63. {
  64. break;
  65. }
  66. $nextCode = $tokens[$nextToken]['code'];
  67. if ($nextCode === T_EQUAL)
  68. {
  69. // Check parameter default spacing.
  70. if (($nextToken - $nextParam) > 1)
  71. {
  72. $gap = strlen($tokens[($nextParam + 1)]['content']);
  73. $arg = $tokens[$nextParam]['content'];
  74. $error = "Expected 0 spaces between argument \"$arg\" and equals sign; $gap found";
  75. $phpcsFile->addError($error, $nextToken);
  76. }
  77. if ($tokens[($nextToken + 1)]['code'] === T_WHITESPACE)
  78. {
  79. $gap = strlen($tokens[($nextToken + 1)]['content']);
  80. $arg = $tokens[$nextParam]['content'];
  81. $error = "Expected 0 spaces between default value and equals sign for argument \"$arg\"; $gap found";
  82. $phpcsFile->addError($error, $nextToken);
  83. }
  84. }
  85. // Find and check the comma (if there is one).
  86. $nextComma = $phpcsFile->findNext(T_COMMA, ($nextParam + 1), $closeBracket);
  87. if ($nextComma !== false)
  88. {
  89. // Comma found.
  90. if ($tokens[($nextComma - 1)]['code'] === T_WHITESPACE)
  91. {
  92. $space = strlen($tokens[($nextComma - 1)]['content']);
  93. $arg = $tokens[$nextParam]['content'];
  94. $error = "Expected 0 spaces between argument \"$arg\" and comma; $space found";
  95. $phpcsFile->addError($error, $nextToken);
  96. }
  97. }
  98. // Take references into account when expecting the
  99. // location of whitespace.
  100. if ($phpcsFile->isReference(($nextParam - 1)) === true)
  101. {
  102. $whitespace = $tokens[($nextParam - 2)];
  103. }
  104. else
  105. {
  106. $whitespace = $tokens[($nextParam - 1)];
  107. }
  108. if (empty($params) === false)
  109. {
  110. // This is not the first argument in the function declaration.
  111. $arg = $tokens[$nextParam]['content'];
  112. if ($whitespace['code'] === T_WHITESPACE)
  113. {
  114. $gap = strlen($whitespace['content']);
  115. // Before we throw an error, make sure there is no type hint.
  116. $comma = $phpcsFile->findPrevious(T_COMMA, ($nextParam - 1));
  117. $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($comma + 1), null, true);
  118. if ($phpcsFile->isReference($nextToken) === true)
  119. {
  120. $nextToken ++;
  121. }
  122. if ($nextToken !== $nextParam)
  123. {
  124. // There was a type hint, so check the spacing between
  125. // the hint and the variable as well.
  126. $hint = $tokens[$nextToken]['content'];
  127. if ($gap !== 1)
  128. {
  129. $error = "Expected 1 space between type hint and argument \"$arg\"; $gap found";
  130. $phpcsFile->addError($error, $nextToken);
  131. }
  132. if ($multiLine === false)
  133. {
  134. if ($tokens[($comma + 1)]['code'] !== T_WHITESPACE)
  135. {
  136. $error = "Expected 1 space between comma and type hint \"$hint\"; 0 found";
  137. $phpcsFile->addError($error, $nextToken);
  138. }
  139. else
  140. {
  141. $gap = strlen($tokens[($comma + 1)]['content']);
  142. if ($gap !== 1)
  143. {
  144. $error = "Expected 1 space between comma and type hint \"$hint\"; $gap found";
  145. $phpcsFile->addError($error, $nextToken);
  146. }
  147. }
  148. }
  149. }
  150. else
  151. if ($multiLine === false && $gap !== 1)
  152. {
  153. $error = "Expected 1 space between comma and argument \"$arg\"; $gap found";
  154. $phpcsFile->addError($error, $nextToken);
  155. } //end if
  156. }
  157. else
  158. {
  159. $error = "Expected 1 space between comma and argument \"$arg\"; 0 found";
  160. $phpcsFile->addError($error, $nextToken);
  161. } //end if
  162. }
  163. else
  164. {
  165. // First argument in function declaration.
  166. if ($whitespace['code'] === T_WHITESPACE)
  167. {
  168. $gap = strlen($whitespace['content']);
  169. $arg = $tokens[$nextParam]['content'];
  170. // Before we throw an error, make sure there is no type hint.
  171. $bracket = $phpcsFile->findPrevious(T_OPEN_PARENTHESIS, ($nextParam - 1));
  172. $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($bracket + 1), null, true);
  173. if ($phpcsFile->isReference($nextToken) === true)
  174. {
  175. $nextToken ++;
  176. }
  177. if ($nextToken !== $nextParam)
  178. {
  179. // There was a type hint, so check the spacing between
  180. // the hint and the variable as well.
  181. $hint = $tokens[$nextToken]['content'];
  182. if ($gap !== 1)
  183. {
  184. $error = "Expected 1 space between type hint and argument \"$arg\"; $gap found";
  185. $phpcsFile->addError($error, $nextToken);
  186. }
  187. if ($multiLine === false && $tokens[($bracket + 1)]['code'] === T_WHITESPACE)
  188. {
  189. $gap = strlen($tokens[($bracket + 1)]['content']);
  190. $error = "Expected 0 spaces between opening bracket and type hint \"$hint\"; $gap found";
  191. $phpcsFile->addError($error, $nextToken);
  192. }
  193. }
  194. else
  195. if ($multiLine === false)
  196. {
  197. $error = "Expected 0 spaces between opening bracket and argument \"$arg\"; $gap found";
  198. $phpcsFile->addError($error, $nextToken);
  199. }
  200. } //end if
  201. } //end if
  202. $params[] = $nextParam;
  203. } //end while
  204. if (empty($params) === true)
  205. {
  206. // There are no parameters for this function.
  207. if (($closeBracket - $openBracket) !== 1)
  208. {
  209. $space = strlen($tokens[($closeBracket - 1)]['content']);
  210. $error = "Expected 0 spaces between brackets of function declaration; $space found";
  211. $phpcsFile->addError($error, $stackPtr);
  212. }
  213. }
  214. else
  215. if ($multiLine === false && $tokens[($closeBracket - 1)]['code'] === T_WHITESPACE)
  216. {
  217. $lastParam = array_pop($params);
  218. $arg = $tokens[$lastParam]['content'];
  219. $gap = strlen($tokens[($closeBracket - 1)]['content']);
  220. $error = "Expected 0 spaces between argument \"$arg\" and closing bracket; $gap found";
  221. $phpcsFile->addError($error, $closeBracket);
  222. }
  223. } //end process()
  224. } //end class
  225. ?>