/pear/PHP/CodeSniffer/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php

https://github.com/ravivare/moodle-local_codechecker · PHP · 254 lines · 166 code · 32 blank · 56 comment · 36 complexity · d7c333aa0a4cdf3c7800880276e7a5d5 MD5 · raw file

  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 301632 2010-07-28 01:57:56Z 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.3.0
  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. $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($nextParam + 1), ($closeBracket + 1), true);
  61. if ($nextToken === false) {
  62. break;
  63. }
  64. $nextCode = $tokens[$nextToken]['code'];
  65. if ($nextCode === T_EQUAL) {
  66. // Check parameter default spacing.
  67. if (($nextToken - $nextParam) > 1) {
  68. $error = 'Expected 0 spaces between argument "%s" and equals sign; %s found';
  69. $data = array(
  70. $tokens[$nextParam]['content'],
  71. strlen($tokens[($nextParam + 1)]['content']),
  72. );
  73. $phpcsFile->addError($error, $nextToken, 'SpaceBeforeEquals', $data);
  74. }
  75. if ($tokens[($nextToken + 1)]['code'] === T_WHITESPACE) {
  76. $error = 'Expected 0 spaces between default value and equals sign for argument "%s"; %s found';
  77. $data = array(
  78. $tokens[$nextParam]['content'],
  79. strlen($tokens[($nextToken + 1)]['content']),
  80. );
  81. $phpcsFile->addError($error, $nextToken, 'SpaceAfterDefault', $data);
  82. }
  83. }
  84. // Find and check the comma (if there is one).
  85. $nextComma = $phpcsFile->findNext(T_COMMA, ($nextParam + 1), $closeBracket);
  86. if ($nextComma !== false) {
  87. // Comma found.
  88. if ($tokens[($nextComma - 1)]['code'] === T_WHITESPACE) {
  89. $error = 'Expected 0 spaces between argument "%s" and comma; %s found';
  90. $data = array(
  91. $tokens[$nextParam]['content'],
  92. strlen($tokens[($nextComma - 1)]['content']),
  93. );
  94. $phpcsFile->addError($error, $nextToken, 'SpaceBeforeComma', $data);
  95. }
  96. }
  97. // Take references into account when expecting the
  98. // location of whitespace.
  99. if ($phpcsFile->isReference(($nextParam - 1)) === true) {
  100. $whitespace = $tokens[($nextParam - 2)];
  101. } else {
  102. $whitespace = $tokens[($nextParam - 1)];
  103. }
  104. if (empty($params) === false) {
  105. // This is not the first argument in the function declaration.
  106. $arg = $tokens[$nextParam]['content'];
  107. if ($whitespace['code'] === T_WHITESPACE) {
  108. $gap = strlen($whitespace['content']);
  109. // Before we throw an error, make sure there is no type hint.
  110. $comma = $phpcsFile->findPrevious(T_COMMA, ($nextParam - 1));
  111. $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($comma + 1), null, true);
  112. if ($phpcsFile->isReference($nextToken) === true) {
  113. $nextToken++;
  114. }
  115. if ($nextToken !== $nextParam) {
  116. // There was a type hint, so check the spacing between
  117. // the hint and the variable as well.
  118. $hint = $tokens[$nextToken]['content'];
  119. if ($gap !== 1) {
  120. $error = 'Expected 1 space between type hint and argument "%s"; %s found';
  121. $data = array(
  122. $arg,
  123. $gap,
  124. );
  125. $phpcsFile->addError($error, $nextToken, 'SpacingAfterHint', $data);
  126. }
  127. if ($multiLine === false) {
  128. if ($tokens[($comma + 1)]['code'] !== T_WHITESPACE) {
  129. $error = 'Expected 1 space between comma and type hint "%s"; 0 found';
  130. $data = array($hint);
  131. $phpcsFile->addError($error, $nextToken, 'NoSapceBeforeHint', $data);
  132. } else {
  133. $gap = strlen($tokens[($comma + 1)]['content']);
  134. if ($gap !== 1) {
  135. $error = 'Expected 1 space between comma and type hint "%s"; %s found';
  136. $data = array(
  137. $hint,
  138. $gap,
  139. );
  140. $phpcsFile->addError($error, $nextToken, 'SpacingBeforeHint', $data);
  141. }
  142. }
  143. }
  144. } else if ($multiLine === false && $gap !== 1) {
  145. $error = 'Expected 1 space between comma and argument "%s"; %s found';
  146. $data = array(
  147. $arg,
  148. $gap,
  149. );
  150. $phpcsFile->addError($error, $nextToken, 'SpacingBeforeArg', $data);
  151. }//end if
  152. } else {
  153. $error = 'Expected 1 space between comma and argument "%s"; 0 found';
  154. $data = array($arg);
  155. $phpcsFile->addError($error, $nextToken, 'NoSpaceBeforeArg', $data);
  156. }//end if
  157. } else {
  158. // First argument in function declaration.
  159. if ($whitespace['code'] === T_WHITESPACE) {
  160. $gap = strlen($whitespace['content']);
  161. $arg = $tokens[$nextParam]['content'];
  162. // Before we throw an error, make sure there is no type hint.
  163. $bracket = $phpcsFile->findPrevious(T_OPEN_PARENTHESIS, ($nextParam - 1));
  164. $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($bracket + 1), null, true);
  165. if ($phpcsFile->isReference($nextToken) === true) {
  166. $nextToken++;
  167. }
  168. if ($nextToken !== $nextParam) {
  169. // There was a type hint, so check the spacing between
  170. // the hint and the variable as well.
  171. $hint = $tokens[$nextToken]['content'];
  172. if ($gap !== 1) {
  173. $error = 'Expected 1 space between type hint and argument "%s"; %s found';
  174. $data = array(
  175. $arg,
  176. $gap,
  177. );
  178. $phpcsFile->addError($error, $nextToken, 'SpacingAfterHint', $data);
  179. }
  180. if ($multiLine === false
  181. && $tokens[($bracket + 1)]['code'] === T_WHITESPACE
  182. ) {
  183. $error = 'Expected 0 spaces between opening bracket and type hint "%s"; %s found';
  184. $data = array(
  185. $hint,
  186. strlen($tokens[($bracket + 1)]['content']),
  187. );
  188. $phpcsFile->addError($error, $nextToken, 'SpacingAfterOpenHint', $data);
  189. }
  190. } else if ($multiLine === false) {
  191. $error = 'Expected 0 spaces between opening bracket and argument "%s"; %s found';
  192. $data = array(
  193. $arg,
  194. $gap,
  195. );
  196. $phpcsFile->addError($error, $nextToken, 'SpacingAfterOpen', $data);
  197. }
  198. }//end if
  199. }//end if
  200. $params[] = $nextParam;
  201. }//end while
  202. if (empty($params) === true) {
  203. // There are no parameters for this function.
  204. if (($closeBracket - $openBracket) !== 1) {
  205. $error = 'Expected 0 spaces between brackets of function declaration; %s found';
  206. $data = array(strlen($tokens[($closeBracket - 1)]['content']));
  207. $phpcsFile->addError($error, $stackPtr, 'SpacingBetween', $data);
  208. }
  209. } else if ($multiLine === false
  210. && $tokens[($closeBracket - 1)]['code'] === T_WHITESPACE
  211. ) {
  212. $lastParam = array_pop($params);
  213. $error = 'Expected 0 spaces between argument "%s" and closing bracket; %s found';
  214. $data = array(
  215. $tokens[$lastParam]['content'],
  216. strlen($tokens[($closeBracket - 1)]['content']),
  217. );
  218. $phpcsFile->addError($error, $closeBracket, 'SpacingBeforeClose', $data);
  219. }
  220. }//end process()
  221. }//end class
  222. ?>