PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/lordgnu/php_codesniffer
PHP | 300 lines | 192 code | 38 blank | 70 comment | 40 complexity | c83ed27cea88f92160af515ff15ac955 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-2012 Squiz Pty Ltd (ABN 77 084 670 600)
  12. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  13. * @link http://pear.php.net/package/PHP_CodeSniffer
  14. */
  15. /**
  16. * Squiz_Sniffs_Functions_FunctionDeclarationArgumentSpacingSniff.
  17. *
  18. * Checks that arguments in function declarations are spaced correctly.
  19. *
  20. * @category PHP
  21. * @package PHP_CodeSniffer
  22. * @author Greg Sherwood <gsherwood@squiz.net>
  23. * @author Marc McIntyre <mmcintyre@squiz.net>
  24. * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
  25. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  26. * @version Release: @package_version@
  27. * @link http://pear.php.net/package/PHP_CodeSniffer
  28. */
  29. class Squiz_Sniffs_Functions_FunctionDeclarationArgumentSpacingSniff implements PHP_CodeSniffer_Sniff
  30. {
  31. /**
  32. * How many spaces should surround the equals signs.
  33. *
  34. * @var int
  35. */
  36. public $equalsSpacing = 0;
  37. /**
  38. * Returns an array of tokens this test wants to listen for.
  39. *
  40. * @return array
  41. */
  42. public function register()
  43. {
  44. return array(
  45. T_FUNCTION,
  46. T_CLOSURE,
  47. );
  48. }//end register()
  49. /**
  50. * Processes this test, when one of its tokens is encountered.
  51. *
  52. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  53. * @param int $stackPtr The position of the current token in the
  54. * stack passed in $tokens.
  55. *
  56. * @return void
  57. */
  58. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  59. {
  60. $this->equalsSpacing = (int) $this->equalsSpacing;
  61. $tokens = $phpcsFile->getTokens();
  62. $openBracket = $tokens[$stackPtr]['parenthesis_opener'];
  63. $this->processBracket($phpcsFile, $openBracket);
  64. if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
  65. $use = $phpcsFile->findNext(T_USE, ($tokens[$openBracket]['parenthesis_closer'] + 1), $tokens[$stackPtr]['scope_opener']);
  66. if ($use !== false) {
  67. $openBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1), null);
  68. $this->processBracket($phpcsFile, $openBracket);
  69. }
  70. }
  71. }//end process()
  72. /**
  73. * Processes the contents of a single set of brackets.
  74. *
  75. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  76. * @param int $openBracket The position of the open bracket
  77. * in the stack passed in $tokens.
  78. *
  79. * @return void
  80. */
  81. public function processBracket(PHP_CodeSniffer_File $phpcsFile, $openBracket)
  82. {
  83. $tokens = $phpcsFile->getTokens();
  84. $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
  85. $multiLine = ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']);
  86. $nextParam = $openBracket;
  87. $params = array();
  88. while (($nextParam = $phpcsFile->findNext(T_VARIABLE, ($nextParam + 1), $closeBracket)) !== false) {
  89. $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($nextParam + 1), ($closeBracket + 1), true);
  90. if ($nextToken === false) {
  91. break;
  92. }
  93. $nextCode = $tokens[$nextToken]['code'];
  94. if ($nextCode === T_EQUAL) {
  95. // Check parameter default spacing.
  96. $spacesBefore = 0;
  97. if (($nextToken - $nextParam) > 1) {
  98. $spacesBefore = strlen($tokens[($nextParam + 1)]['content']);
  99. }
  100. if ($spacesBefore !== $this->equalsSpacing) {
  101. $error = 'Incorrect spacing between argument "%s" and equals sign; expected '.$this->equalsSpacing.' but found %s';
  102. $data = array(
  103. $tokens[$nextParam]['content'],
  104. $spacesBefore,
  105. );
  106. $phpcsFile->addError($error, $nextToken, 'SpaceBeforeEquals', $data);
  107. }
  108. $spacesAfter = 0;
  109. if ($tokens[($nextToken + 1)]['code'] === T_WHITESPACE) {
  110. $spacesAfter = strlen($tokens[($nextParam + 1)]['content']);
  111. }
  112. if ($spacesAfter !== $this->equalsSpacing) {
  113. $error = 'Incorrect spacing between default value and equals sign for argument "%s"; expected '.$this->equalsSpacing.' but found %s';
  114. $data = array(
  115. $tokens[$nextParam]['content'],
  116. $spacesAfter,
  117. );
  118. $phpcsFile->addError($error, $nextToken, 'SpaceAfterDefault', $data);
  119. }
  120. }//end if
  121. // Find and check the comma (if there is one).
  122. $nextComma = $phpcsFile->findNext(T_COMMA, ($nextParam + 1), $closeBracket);
  123. if ($nextComma !== false) {
  124. // Comma found.
  125. if ($tokens[($nextComma - 1)]['code'] === T_WHITESPACE) {
  126. $error = 'Expected 0 spaces between argument "%s" and comma; %s found';
  127. $data = array(
  128. $tokens[$nextParam]['content'],
  129. strlen($tokens[($nextComma - 1)]['content']),
  130. );
  131. $phpcsFile->addError($error, $nextToken, 'SpaceBeforeComma', $data);
  132. }
  133. }
  134. // Take references into account when expecting the
  135. // location of whitespace.
  136. if ($phpcsFile->isReference(($nextParam - 1)) === true) {
  137. $whitespace = ($nextParam - 2);
  138. } else {
  139. $whitespace = ($nextParam - 1);
  140. }
  141. if (empty($params) === false) {
  142. // This is not the first argument in the function declaration.
  143. $arg = $tokens[$nextParam]['content'];
  144. if ($tokens[$whitespace]['code'] === T_WHITESPACE) {
  145. $gap = strlen($tokens[$whitespace]['content']);
  146. // Before we throw an error, make sure there is no type hint.
  147. $comma = $phpcsFile->findPrevious(T_COMMA, ($nextParam - 1));
  148. $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($comma + 1), null, true);
  149. if ($phpcsFile->isReference($nextToken) === true) {
  150. $nextToken++;
  151. }
  152. if ($nextToken !== $nextParam) {
  153. // There was a type hint, so check the spacing between
  154. // the hint and the variable as well.
  155. $hint = $tokens[$nextToken]['content'];
  156. if ($gap !== 1) {
  157. $error = 'Expected 1 space between type hint and argument "%s"; %s found';
  158. $data = array(
  159. $arg,
  160. $gap,
  161. );
  162. $phpcsFile->addError($error, $nextToken, 'SpacingAfterHint', $data);
  163. }
  164. if ($multiLine === false) {
  165. if ($tokens[($comma + 1)]['code'] !== T_WHITESPACE) {
  166. $error = 'Expected 1 space between comma and type hint "%s"; 0 found';
  167. $data = array($hint);
  168. $phpcsFile->addError($error, $nextToken, 'NoSapceBeforeHint', $data);
  169. } else {
  170. $gap = strlen($tokens[($comma + 1)]['content']);
  171. if ($gap !== 1) {
  172. $error = 'Expected 1 space between comma and type hint "%s"; %s found';
  173. $data = array(
  174. $hint,
  175. $gap,
  176. );
  177. $phpcsFile->addError($error, $nextToken, 'SpacingBeforeHint', $data);
  178. }
  179. }
  180. }
  181. } else if ($gap !== 1) {
  182. // Just make sure this is not actually an indent.
  183. if ($tokens[$whitespace]['line'] === $tokens[($whitespace - 1)]['line']) {
  184. $error = 'Expected 1 space between comma and argument "%s"; %s found';
  185. $data = array(
  186. $arg,
  187. $gap,
  188. );
  189. $phpcsFile->addError($error, $nextToken, 'SpacingBeforeArg', $data);
  190. }
  191. }//end if
  192. } else {
  193. $error = 'Expected 1 space between comma and argument "%s"; 0 found';
  194. $data = array($arg);
  195. $phpcsFile->addError($error, $nextToken, 'NoSpaceBeforeArg', $data);
  196. }//end if
  197. } else {
  198. // First argument in function declaration.
  199. if ($tokens[$whitespace]['code'] === T_WHITESPACE) {
  200. $gap = strlen($tokens[$whitespace]['content']);
  201. $arg = $tokens[$nextParam]['content'];
  202. // Before we throw an error, make sure there is no type hint.
  203. $bracket = $phpcsFile->findPrevious(T_OPEN_PARENTHESIS, ($nextParam - 1));
  204. $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($bracket + 1), null, true);
  205. if ($phpcsFile->isReference($nextToken) === true) {
  206. $nextToken++;
  207. }
  208. if ($nextToken !== $nextParam) {
  209. // There was a type hint, so check the spacing between
  210. // the hint and the variable as well.
  211. $hint = $tokens[$nextToken]['content'];
  212. if ($gap !== 1) {
  213. $error = 'Expected 1 space between type hint and argument "%s"; %s found';
  214. $data = array(
  215. $arg,
  216. $gap,
  217. );
  218. $phpcsFile->addError($error, $nextToken, 'SpacingAfterHint', $data);
  219. }
  220. if ($multiLine === false
  221. && $tokens[($bracket + 1)]['code'] === T_WHITESPACE
  222. ) {
  223. $error = 'Expected 0 spaces between opening bracket and type hint "%s"; %s found';
  224. $data = array(
  225. $hint,
  226. strlen($tokens[($bracket + 1)]['content']),
  227. );
  228. $phpcsFile->addError($error, $nextToken, 'SpacingAfterOpenHint', $data);
  229. }
  230. } else if ($multiLine === false) {
  231. $error = 'Expected 0 spaces between opening bracket and argument "%s"; %s found';
  232. $data = array(
  233. $arg,
  234. $gap,
  235. );
  236. $phpcsFile->addError($error, $nextToken, 'SpacingAfterOpen', $data);
  237. }
  238. }//end if
  239. }//end if
  240. $params[] = $nextParam;
  241. }//end while
  242. if (empty($params) === true) {
  243. // There are no parameters for this function.
  244. if (($closeBracket - $openBracket) !== 1) {
  245. $error = 'Expected 0 spaces between brackets of function declaration; %s found';
  246. $data = array(strlen($tokens[($closeBracket - 1)]['content']));
  247. $phpcsFile->addError($error, $openBracket, 'SpacingBetween', $data);
  248. }
  249. } else if ($multiLine === false
  250. && $tokens[($closeBracket - 1)]['code'] === T_WHITESPACE
  251. ) {
  252. $lastParam = array_pop($params);
  253. $error = 'Expected 0 spaces between argument "%s" and closing bracket; %s found';
  254. $data = array(
  255. $tokens[$lastParam]['content'],
  256. strlen($tokens[($closeBracket - 1)]['content']),
  257. );
  258. $phpcsFile->addError($error, $closeBracket, 'SpacingBeforeClose', $data);
  259. }
  260. }//end processBracket()
  261. }//end class
  262. ?>