5 |
aurelien |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* PEAR_Sniffs_Functions_FunctionCallSignatureSniff.
|
|
|
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: FunctionCallSignatureSniff.php,v 1.7 2008/12/05 02:45:08 squiz Exp $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* PEAR_Sniffs_Functions_FunctionCallSignatureSniff.
|
|
|
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 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
25 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
26 |
* @version Release: 1.2.0RC1
|
|
|
27 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
28 |
*/
|
|
|
29 |
class PEAR_Sniffs_Functions_FunctionCallSignatureSniff implements PHP_CodeSniffer_Sniff
|
|
|
30 |
{
|
|
|
31 |
|
|
|
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_STRING);
|
|
|
41 |
|
|
|
42 |
}//end register()
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Processes this test, when one of its tokens is encountered.
|
|
|
47 |
*
|
|
|
48 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
49 |
* @param int $stackPtr The position of the current token
|
|
|
50 |
* in the stack passed in $tokens.
|
|
|
51 |
*
|
|
|
52 |
* @return void
|
|
|
53 |
*/
|
|
|
54 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
55 |
{
|
|
|
56 |
$tokens = $phpcsFile->getTokens();
|
|
|
57 |
|
|
|
58 |
// Find the next non-empty token.
|
|
|
59 |
$openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
|
|
|
60 |
|
|
|
61 |
if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
|
|
|
62 |
// Not a function call.
|
|
|
63 |
return;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
if (isset($tokens[$openBracket]['parenthesis_closer']) === false) {
|
|
|
67 |
// Not a function call.
|
|
|
68 |
return;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
// Find the previous non-empty token.
|
|
|
72 |
$previous = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
|
|
|
73 |
if ($tokens[$previous]['code'] === T_FUNCTION) {
|
|
|
74 |
// It's a function definition, not a function call.
|
|
|
75 |
return;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
if ($tokens[$previous]['code'] === T_NEW) {
|
|
|
79 |
// We are creating an object, not calling a function.
|
|
|
80 |
return;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
|
|
|
84 |
|
|
|
85 |
if (($stackPtr + 1) !== $openBracket) {
|
|
|
86 |
// Checking this: $value = my_function[*](...).
|
|
|
87 |
$error = 'Space before opening parenthesis of function call prohibited';
|
|
|
88 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
$next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), null, true);
|
|
|
92 |
if ($tokens[$next]['code'] === T_SEMICOLON) {
|
|
|
93 |
if (in_array($tokens[($closeBracket + 1)]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
|
|
|
94 |
$error = 'Space after closing parenthesis of function call prohibited';
|
|
|
95 |
$phpcsFile->addError($error, $closeBracket);
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
// Check if this is a single line or multi-line function call.
|
|
|
100 |
if ($tokens[$openBracket]['line'] === $tokens[$closeBracket]['line']) {
|
|
|
101 |
$this->processSingleLineCall($phpcsFile, $stackPtr, $openBracket, $tokens);
|
|
|
102 |
} else {
|
|
|
103 |
$this->processMultiLineCall($phpcsFile, $stackPtr, $openBracket, $tokens);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
}//end process()
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Processes single-line calls.
|
|
|
111 |
*
|
|
|
112 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
113 |
* @param int $stackPtr The position of the current token
|
|
|
114 |
* in the stack passed in $tokens.
|
|
|
115 |
* @param int $openBracket The position of the openning bracket
|
|
|
116 |
* in the stack passed in $tokens.
|
|
|
117 |
* @param array $tokens The stack of tokens that make up
|
|
|
118 |
* the file.
|
|
|
119 |
*
|
|
|
120 |
* @return void
|
|
|
121 |
*/
|
|
|
122 |
public function processSingleLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens)
|
|
|
123 |
{
|
|
|
124 |
if ($tokens[($openBracket + 1)]['code'] === T_WHITESPACE) {
|
|
|
125 |
// Checking this: $value = my_function([*]...).
|
|
|
126 |
$error = 'Space after opening parenthesis of function call prohibited';
|
|
|
127 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
$closer = $tokens[$openBracket]['parenthesis_closer'];
|
|
|
131 |
|
|
|
132 |
if ($tokens[($closer - 1)]['code'] === T_WHITESPACE) {
|
|
|
133 |
// Checking this: $value = my_function(...[*]).
|
|
|
134 |
$between = $phpcsFile->findNext(T_WHITESPACE, ($openBracket + 1), null, true);
|
|
|
135 |
|
|
|
136 |
// Only throw an error if there is some content between the parenthesis.
|
|
|
137 |
// i.e., Checking for this: $value = my_function().
|
|
|
138 |
// If there is no content, then we would have thrown an error in the
|
|
|
139 |
// previous IF statement because it would look like this:
|
|
|
140 |
// $value = my_function( ).
|
|
|
141 |
if ($between !== $closer) {
|
|
|
142 |
$error = 'Space before closing parenthesis of function call prohibited';
|
|
|
143 |
$phpcsFile->addError($error, $closer);
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
}//end processSingleLineCall()
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Processes multi-line calls.
|
|
|
152 |
*
|
|
|
153 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
154 |
* @param int $stackPtr The position of the current token
|
|
|
155 |
* in the stack passed in $tokens.
|
|
|
156 |
* @param int $openBracket The position of the openning bracket
|
|
|
157 |
* in the stack passed in $tokens.
|
|
|
158 |
* @param array $tokens The stack of tokens that make up
|
|
|
159 |
* the file.
|
|
|
160 |
*
|
|
|
161 |
* @return void
|
|
|
162 |
*/
|
|
|
163 |
public function processMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens)
|
|
|
164 |
{
|
|
|
165 |
// We need to work out how far indented the function
|
|
|
166 |
// call itself is, so we can work out how far to
|
|
|
167 |
// indent the arguments.
|
|
|
168 |
$functionIndent = 0;
|
|
|
169 |
for ($i = ($stackPtr - 1); $i >= 0; $i--) {
|
|
|
170 |
if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) {
|
|
|
171 |
$i++;
|
|
|
172 |
break;
|
|
|
173 |
}
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
if ($tokens[$i]['code'] === T_WHITESPACE) {
|
|
|
177 |
$functionIndent = strlen($tokens[$i]['content']);
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
// Each line between the parenthesis should be indented 4 spaces.
|
|
|
181 |
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
|
|
|
182 |
$lastLine = $tokens[$openBracket]['line'];
|
|
|
183 |
for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
|
|
|
184 |
// Skip nested function calls.
|
|
|
185 |
if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) {
|
|
|
186 |
$i = $tokens[$i]['parenthesis_closer'];
|
|
|
187 |
$lastLine = $tokens[$i]['line'];
|
|
|
188 |
continue;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
if ($tokens[$i]['line'] !== $lastLine) {
|
|
|
192 |
$lastLine = $tokens[$i]['line'];
|
|
|
193 |
|
|
|
194 |
// We changed lines, so this should be a whitespace indent token.
|
|
|
195 |
if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$heredocTokens) === true) {
|
|
|
196 |
// Ignore heredoc indentation.
|
|
|
197 |
continue;
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$stringTokens) === true) {
|
|
|
201 |
if ($tokens[$i]['code'] === $tokens[($i - 1)]['code']) {
|
|
|
202 |
// Ignore multi-line string indentation.
|
|
|
203 |
continue;
|
|
|
204 |
}
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
if ($tokens[$i]['line'] === $tokens[$closeBracket]['line']) {
|
|
|
208 |
// Closing brace needs to be indented to the same level
|
|
|
209 |
// as the function call.
|
|
|
210 |
$expectedIndent = $functionIndent;
|
|
|
211 |
} else {
|
|
|
212 |
$expectedIndent = ($functionIndent + 4);
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
if ($tokens[$i]['code'] !== T_WHITESPACE) {
|
|
|
216 |
$foundIndent = 0;
|
|
|
217 |
} else {
|
|
|
218 |
$foundIndent = strlen($tokens[$i]['content']);
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
if ($expectedIndent !== $foundIndent) {
|
|
|
222 |
$error = "Multi-line function call not indented correctly; expected $expectedIndent spaces but found $foundIndent";
|
|
|
223 |
$phpcsFile->addError($error, $i);
|
|
|
224 |
}
|
|
|
225 |
}//end if
|
|
|
226 |
}//end for
|
|
|
227 |
|
|
|
228 |
if ($tokens[($openBracket + 1)]['content'] !== $phpcsFile->eolChar) {
|
|
|
229 |
$error = 'Opening parenthesis of a multi-line function call must be the last content on the line';
|
|
|
230 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBracket - 1), null, true);
|
|
|
234 |
if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) {
|
|
|
235 |
$error = 'Closing parenthesis of a multi-line function call must be on a line by itself';
|
|
|
236 |
$phpcsFile->addError($error, $closeBracket);
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
}//end processMultiLineCall()
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
}//end class
|
|
|
243 |
?>
|