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