3
* Squiz_Sniffs_Functions_FunctionDeclarationArgumentSpacingSniff.
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,v 1.10 2007/10/19 07:05:35 squiz Exp $
14
* @link http://pear.php.net/package/PHP_CodeSniffer
18
* Squiz_Sniffs_Functions_FunctionDeclarationArgumentSpacingSniff.
20
* Checks that arguments in function declarations are spaced correctly.
23
* @package PHP_CodeSniffer
24
* @author Greg Sherwood <gsherwood@squiz.net>
25
* @author Marc McIntyre <mmcintyre@squiz.net>
26
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
27
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
28
* @version Release: 1.1.0
29
* @link http://pear.php.net/package/PHP_CodeSniffer
31
class Squiz_Sniffs_Functions_FunctionDeclarationArgumentSpacingSniff implements PHP_CodeSniffer_Sniff
36
* Returns an array of tokens this test wants to listen for.
40
public function register()
42
return array(T_FUNCTION);
48
* Processes this test, when one of its tokens is encountered.
50
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
51
* @param int $stackPtr The position of the current token in the
52
* stack passed in $tokens.
56
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
58
$tokens = $phpcsFile->getTokens();
60
$functionName = $phpcsFile->findNext(array(T_STRING), $stackPtr);
61
$openBracket = $tokens[$stackPtr]['parenthesis_opener'];
62
$closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
64
$nextParam = $openBracket;
66
while (($nextParam = $phpcsFile->findNext(T_VARIABLE, ($nextParam + 1), $closeBracket)) !== false) {
68
$nextToken = $phpcsFile->findNext(T_WHITESPACE, ($nextParam + 1), ($closeBracket + 1), true);
69
if ($nextToken === false) {
73
$nextCode = $tokens[$nextToken]['code'];
75
if ($nextCode === T_EQUAL) {
76
// Check parameter default spacing.
77
if (($nextToken - $nextParam) > 1) {
78
$gap = strlen($tokens[($nextParam + 1)]['content']);
79
$arg = $tokens[$nextParam]['content'];
80
$error = "Expected 0 spaces between argument \"$arg\" and equals sign; $gap found";
81
$phpcsFile->addError($error, $nextToken);
84
if ($tokens[($nextToken + 1)]['code'] === T_WHITESPACE) {
85
$gap = strlen($tokens[($nextToken + 1)]['content']);
86
$arg = $tokens[$nextParam]['content'];
87
$error = "Expected 0 spaces between default value and equals sign for argument \"$arg\"; $gap found";
88
$phpcsFile->addError($error, $nextToken);
92
// Find and check the comma (if there is one).
93
$nextComma = $phpcsFile->findNext(T_COMMA, ($nextParam + 1), $closeBracket);
94
if ($nextComma !== false) {
96
if ($tokens[($nextComma - 1)]['code'] === T_WHITESPACE) {
97
$space = strlen($tokens[($nextComma - 1)]['content']);
98
$arg = $tokens[$nextParam]['content'];
99
$error = "Expected 0 spaces between argument \"$arg\" and comma; $space found";
100
$phpcsFile->addError($error, $nextToken);
104
// Take references into account when expecting the
105
// location of whitespace.
106
if ($phpcsFile->isReference(($nextParam - 1)) === true) {
107
$whitespace = $tokens[($nextParam - 2)];
109
$whitespace = $tokens[($nextParam - 1)];
112
if (empty($params) === false) {
113
// This is not the first argument in the function declaration.
114
$arg = $tokens[$nextParam]['content'];
116
if ($whitespace['code'] === T_WHITESPACE) {
117
$gap = strlen($whitespace['content']);
119
// Before we throw an error, make sure there is no type hint.
120
$comma = $phpcsFile->findPrevious(T_COMMA, ($nextParam - 1));
121
$nextToken = $phpcsFile->findNext(T_WHITESPACE, ($comma + 1), null, true);
122
if ($phpcsFile->isReference($nextToken) === true) {
126
if ($nextToken !== $nextParam) {
127
// There was a type hint, so check the spacing between
128
// the hint and the variable as well.
129
$hint = $tokens[$nextToken]['content'];
132
$error = "Expected 1 space between type hint and argument \"$arg\"; $gap found";
133
$phpcsFile->addError($error, $nextToken);
136
if ($tokens[($comma + 1)]['code'] !== T_WHITESPACE) {
137
$error = "Expected 1 space between comma and type hint \"$hint\"; 0 found";
138
$phpcsFile->addError($error, $nextToken);
140
$gap = strlen($tokens[($comma + 1)]['content']);
142
$error = "Expected 1 space between comma and type hint \"$hint\"; $gap found";
143
$phpcsFile->addError($error, $nextToken);
146
} else if ($gap !== 1) {
147
$error = "Expected 1 space between comma and argument \"$arg\"; $gap found";
148
$phpcsFile->addError($error, $nextToken);
151
$error = "Expected 1 space between comma and argument \"$arg\"; 0 found";
152
$phpcsFile->addError($error, $nextToken);
155
// First argument in function declaration.
156
if ($whitespace['code'] === T_WHITESPACE) {
157
$gap = strlen($whitespace['content']);
158
$arg = $tokens[$nextParam]['content'];
160
// Before we throw an error, make sure there is no type hint.
161
$bracket = $phpcsFile->findPrevious(T_OPEN_PARENTHESIS, ($nextParam - 1));
162
$nextToken = $phpcsFile->findNext(T_WHITESPACE, ($bracket + 1), null, true);
163
if ($phpcsFile->isReference($nextToken) === true) {
167
if ($nextToken !== $nextParam) {
168
// There was a type hint, so check the spacing between
169
// the hint and the variable as well.
170
$hint = $tokens[$nextToken]['content'];
173
$error = "Expected 1 space between type hint and argument \"$arg\"; $gap found";
174
$phpcsFile->addError($error, $nextToken);
177
if ($tokens[($bracket + 1)]['code'] === T_WHITESPACE) {
178
$gap = strlen($tokens[($bracket + 1)]['content']);
179
$error = "Expected 0 spaces between opening bracket and type hint \"$hint\"; $gap found";
180
$phpcsFile->addError($error, $nextToken);
183
$error = "Expected 0 spaces between opening bracket and argument \"$arg\"; $gap found";
184
$phpcsFile->addError($error, $nextToken);
189
$params[] = $nextParam;
193
if (empty($params) === true) {
194
// There are no parameters for this function.
195
if (($closeBracket - $openBracket) !== 1) {
196
$space = strlen($tokens[($closeBracket - 1)]['content']);
197
$error = "Expected 0 spaces between brackets of function declaration; $space found";
198
$phpcsFile->addError($error, $stackPtr);
200
} else if ($tokens[($closeBracket - 1)]['code'] === T_WHITESPACE) {
201
$lastParam = array_pop($params);
202
$arg = $tokens[$lastParam]['content'];
203
$gap = strlen($tokens[($closeBracket - 1)]['content']);
204
$error = "Expected 0 spaces between argument \"$arg\" and closing bracket; $gap found";
205
$phpcsFile->addError($error, $closeBracket);