/sources/CodeSniffer/Standards/Zend/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php

https://github.com/proofek/SQLI_CodeSniffer · PHP · 211 lines · 136 code · 23 blank · 52 comment · 35 complexity · 63ec3fd6e63f51e894443a585be9e1b9 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_CodingStandard
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: $
  20. */
  21. /**
  22. * Zend_Sniffs_Functions_FunctionDeclarationArgumentSpacingSniff.
  23. *
  24. * Checks that arguments in function declarations are spaced correctly.
  25. *
  26. * @category Zend
  27. * @package Zend_CodingStandard
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Sniffs_Functions_FunctionDeclarationArgumentSpacingSniff implements PHP_CodeSniffer_Sniff
  32. {
  33. /**
  34. * Returns an array of tokens this test wants to listen for
  35. *
  36. * @return array
  37. */
  38. public function register()
  39. {
  40. return array(T_FUNCTION);
  41. }
  42. /**
  43. * Processes this test, when one of its tokens is encountered
  44. *
  45. * @param PHP_CodeSniffer_File $phpcsFile The file being scanned
  46. * @param integer $stackPtr The position of the current token in the stack passed in $tokens
  47. * @return void
  48. */
  49. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  50. {
  51. $tokens = $phpcsFile->getTokens();
  52. $functionName = $phpcsFile->findNext(array(T_STRING), $stackPtr);
  53. $openBracket = $tokens[$stackPtr]['parenthesis_opener'];
  54. $closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
  55. $nextParam = $openBracket;
  56. $params = array();
  57. $nextParam = $phpcsFile->findNext(T_VARIABLE, ($nextParam + 1), $closeBracket);
  58. while ($nextParam !== false) {
  59. $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($nextParam + 1), ($closeBracket + 1), true);
  60. if (($nextToken === false) and ($tokens[($nextParam + 1)]['code'] === T_CLOSE_PARENTHESIS)) {
  61. break;
  62. }
  63. $nextCode = $tokens[$nextToken]['code'];
  64. if ($nextCode === T_EQUAL) {
  65. // Check parameter default spacing
  66. $content = $tokens[($nextParam + 1)]['content'];
  67. if ($content !== ' ') {
  68. $gap = strlen($content);
  69. if ($gap === 1) {
  70. $gap = 0;
  71. }
  72. $arg = $tokens[$nextParam]['content'];
  73. $error = "Expected 1 space between argument \"$arg\" and equals sign; $gap found";
  74. $phpcsFile->addError($error, $nextToken);
  75. }
  76. $content = $tokens[($nextToken + 1)]['content'];
  77. if ($content !== ' ') {
  78. if ($tokens[($nextToken + 1)]['code'] !== T_WHITESPACE) {
  79. $gap = 0;
  80. } else {
  81. $gap = strlen($content);
  82. }
  83. $arg = $tokens[$nextParam]['content'];
  84. $error = "Expected 1 space between default value and equals sign for argument \"$arg\"; $gap found";
  85. $phpcsFile->addError($error, $nextToken);
  86. }
  87. }
  88. // Find and check the comma (if there is one)
  89. $nextComma = $phpcsFile->findNext(T_COMMA, ($nextParam + 1), $closeBracket);
  90. if ($nextComma !== false) {
  91. // Comma found
  92. if ($tokens[($nextComma - 1)]['code'] === T_WHITESPACE) {
  93. $space = strlen($tokens[($nextComma - 1)]['content']);
  94. $arg = $tokens[$nextParam]['content'];
  95. $error = "Expected 0 spaces between argument \"$arg\" and comma; $space found";
  96. $phpcsFile->addError($error, $nextToken);
  97. }
  98. }
  99. // Take references into account when expecting the location of whitespace
  100. if ($phpcsFile->isReference(($nextParam - 1)) === true) {
  101. $whitespace = $tokens[($nextParam - 2)];
  102. } else {
  103. $whitespace = $tokens[($nextParam - 1)];
  104. }
  105. if (empty($params) === false) {
  106. // This is not the first argument in the function declaration
  107. $arg = $tokens[$nextParam]['content'];
  108. if ($whitespace['code'] === T_WHITESPACE) {
  109. $gap = strlen($whitespace['content']);
  110. // Before we throw an error, make sure there is no type hint
  111. $comma = $phpcsFile->findPrevious(T_COMMA, ($nextParam - 1));
  112. $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($comma + 1), null, true);
  113. if ($phpcsFile->isReference($nextToken) === true) {
  114. $nextToken++;
  115. }
  116. if ($nextToken !== $nextParam) {
  117. // There was a type hint, so check the spacing between 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 \"$arg\"; $gap found";
  121. $phpcsFile->addError($error, $nextToken);
  122. }
  123. if ($tokens[($comma + 1)]['code'] !== T_WHITESPACE) {
  124. $error = "Expected 1 space between comma and type hint \"$hint\"; 0 found";
  125. $phpcsFile->addError($error, $nextToken);
  126. } else {
  127. $gap = strlen($tokens[($comma + 1)]['content']);
  128. if ($gap !== 1) {
  129. $error = "Expected 1 space between comma and type hint \"$hint\"; $gap found";
  130. $phpcsFile->addError($error, $nextToken);
  131. }
  132. }
  133. } else if ($gap !== 1) {
  134. $error = "Expected 1 space between comma and argument \"$arg\"; $gap found";
  135. $phpcsFile->addError($error, $nextToken);
  136. }
  137. } else {
  138. $error = "Expected 1 space between comma and argument \"$arg\"; 0 found";
  139. $phpcsFile->addError($error, $nextToken);
  140. }
  141. } else {
  142. // First argument in function declaration
  143. if ($whitespace['code'] === T_WHITESPACE) {
  144. $gap = strlen($whitespace['content']);
  145. $arg = $tokens[$nextParam]['content'];
  146. // Before we throw an error, make sure there is no type hint
  147. $bracket = $phpcsFile->findPrevious(T_OPEN_PARENTHESIS, ($nextParam - 1));
  148. $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($bracket + 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 the hint and the variable as well
  154. $hint = $tokens[$nextToken]['content'];
  155. if ($gap !== 1) {
  156. $error = "Expected 1 space between type hint and argument \"$arg\"; $gap found";
  157. $phpcsFile->addError($error, $nextToken);
  158. }
  159. if ($tokens[($bracket + 1)]['code'] === T_WHITESPACE) {
  160. $gap = strlen($tokens[($bracket + 1)]['content']);
  161. $error = "Expected 0 spaces between opening bracket and type hint \"$hint\"; $gap found";
  162. $phpcsFile->addError($error, $nextToken);
  163. }
  164. } else {
  165. $error = "Expected 0 spaces between opening bracket and argument \"$arg\"; $gap found";
  166. $phpcsFile->addError($error, $nextToken);
  167. }
  168. }
  169. }
  170. $params[] = $nextParam;
  171. $nextParam = $phpcsFile->findNext(T_VARIABLE, ($nextParam + 1), $closeBracket);
  172. }
  173. if (empty($params) === true) {
  174. // There are no parameters for this function
  175. if (($closeBracket - $openBracket) !== 1) {
  176. $space = strlen($tokens[($closeBracket - 1)]['content']);
  177. $error = "Expected 0 spaces between brackets of function declaration; $space found";
  178. $phpcsFile->addError($error, $stackPtr);
  179. }
  180. } else if ($tokens[($closeBracket - 1)]['code'] === T_WHITESPACE) {
  181. $lastParam = array_pop($params);
  182. $arg = $tokens[$lastParam]['content'];
  183. $gap = strlen($tokens[($closeBracket - 1)]['content']);
  184. $error = "Expected 0 spaces between argument \"$arg\" and closing bracket; $gap found";
  185. $phpcsFile->addError($error, $closeBracket);
  186. }
  187. }
  188. }