3
* PEAR_Sniffs_Functions_FunctionCallSignatureSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @author Marc McIntyre <mmcintyre@squiz.net>
11
* @copyright 2006-2014 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
17
* PEAR_Sniffs_Functions_FunctionCallSignatureSniff.
20
* @package PHP_CodeSniffer
21
* @author Greg Sherwood <gsherwood@squiz.net>
22
* @author Marc McIntyre <mmcintyre@squiz.net>
23
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
24
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
25
* @version Release: 1.5.4
26
* @link http://pear.php.net/package/PHP_CodeSniffer
28
class PEAR_Sniffs_Functions_FunctionCallSignatureSniff implements PHP_CodeSniffer_Sniff
32
* The number of spaces code should be indented.
39
* If TRUE, multiple arguments can be defined per line in a multi-line call.
43
public $allowMultipleArguments = true;
46
* How many spaces should follow the opening bracket.
50
public $requiredSpacesAfterOpen = 0;
53
* How many spaces should precede the closing bracket.
57
public $requiredSpacesBeforeClose = 0;
61
* Returns an array of tokens this test wants to listen for.
65
public function register()
67
return array(T_STRING);
73
* Processes this test, when one of its tokens is encountered.
75
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
76
* @param int $stackPtr The position of the current token
77
* in the stack passed in $tokens.
81
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
83
$this->requiredSpacesAfterOpen = (int) $this->requiredSpacesAfterOpen;
84
$this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose;
85
$tokens = $phpcsFile->getTokens();
87
// Find the next non-empty token.
88
$openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
90
if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
91
// Not a function call.
95
if (isset($tokens[$openBracket]['parenthesis_closer']) === false) {
96
// Not a function call.
100
// Find the previous non-empty token.
101
$search = PHP_CodeSniffer_Tokens::$emptyTokens;
102
$search[] = T_BITWISE_AND;
103
$previous = $phpcsFile->findPrevious($search, ($stackPtr - 1), null, true);
104
if ($tokens[$previous]['code'] === T_FUNCTION) {
105
// It's a function definition, not a function call.
109
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
111
if (($stackPtr + 1) !== $openBracket) {
112
// Checking this: $value = my_function[*](...).
113
$error = 'Space before opening parenthesis of function call prohibited';
114
$phpcsFile->addError($error, $stackPtr, 'SpaceBeforeOpenBracket');
117
$next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), null, true);
118
if ($tokens[$next]['code'] === T_SEMICOLON) {
119
if (in_array($tokens[($closeBracket + 1)]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
120
$error = 'Space after closing parenthesis of function call prohibited';
121
$phpcsFile->addError($error, $closeBracket, 'SpaceAfterCloseBracket');
125
// Check if this is a single line or multi-line function call.
126
if ($this->isMultiLineCall($phpcsFile, $stackPtr, $openBracket, $tokens) === true) {
127
$this->processMultiLineCall($phpcsFile, $stackPtr, $openBracket, $tokens);
129
$this->processSingleLineCall($phpcsFile, $stackPtr, $openBracket, $tokens);
136
* Processes single-line calls.
138
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
139
* @param int $stackPtr The position of the current token
140
* in the stack passed in $tokens.
141
* @param int $openBracket The position of the opening bracket
142
* in the stack passed in $tokens.
143
* @param array $tokens The stack of tokens that make up
148
public function isMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens)
150
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
151
if ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']) {
157
}//end isMultiLineCall()
161
* Processes single-line calls.
163
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
164
* @param int $stackPtr The position of the current token
165
* in the stack passed in $tokens.
166
* @param int $openBracket The position of the opening bracket
167
* in the stack passed in $tokens.
168
* @param array $tokens The stack of tokens that make up
173
public function processSingleLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens)
176
$closer = $tokens[$openBracket]['parenthesis_closer'];
177
if ($openBracket === ($closer - 1)) {
181
if ($this->requiredSpacesAfterOpen === 0 && $tokens[($openBracket + 1)]['code'] === T_WHITESPACE) {
182
// Checking this: $value = my_function([*]...).
183
$error = 'Space after opening parenthesis of function call prohibited';
184
$phpcsFile->addError($error, $stackPtr, 'SpaceAfterOpenBracket');
185
} else if ($this->requiredSpacesAfterOpen > 0) {
187
if ($tokens[($openBracket + 1)]['code'] === T_WHITESPACE) {
188
$spaceAfterOpen = strlen($tokens[($openBracket + 1)]['content']);
191
if ($spaceAfterOpen !== $this->requiredSpacesAfterOpen) {
192
$error = 'Expected %s spaces after opening bracket; %s found';
194
$this->requiredSpacesAfterOpen,
197
$phpcsFile->addError($error, $stackPtr, 'SpaceAfterOpenBracket', $data);
201
// Checking this: $value = my_function(...[*]).
202
$spaceBeforeClose = 0;
203
if ($tokens[($closer - 1)]['code'] === T_WHITESPACE) {
204
$spaceBeforeClose = strlen($tokens[($closer - 1)]['content']);
207
if ($spaceBeforeClose !== $this->requiredSpacesBeforeClose) {
208
$error = 'Expected %s spaces before closing bracket; %s found';
210
$this->requiredSpacesBeforeClose,
213
$phpcsFile->addError($error, $stackPtr, 'SpaceBeforeCloseBracket', $data);
216
}//end processSingleLineCall()
220
* Processes multi-line calls.
222
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
223
* @param int $stackPtr The position of the current token
224
* in the stack passed in $tokens.
225
* @param int $openBracket The position of the openning bracket
226
* in the stack passed in $tokens.
227
* @param array $tokens The stack of tokens that make up
232
public function processMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens)
234
// We need to work out how far indented the function
235
// call itself is, so we can work out how far to
236
// indent the arguments.
238
for ($i = ($stackPtr - 1); $i >= 0; $i--) {
239
if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) {
245
if ($i > 0 && $tokens[$i]['code'] === T_WHITESPACE) {
246
$functionIndent = strlen($tokens[$i]['content']);
249
// Each line between the parenthesis should be indented n spaces.
250
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
251
$lastLine = $tokens[$openBracket]['line'];
252
for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
253
// Skip nested function calls.
254
if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) {
255
$i = $tokens[$i]['parenthesis_closer'];
256
$lastLine = $tokens[$i]['line'];
260
if ($tokens[$i]['line'] !== $lastLine) {
261
$lastLine = $tokens[$i]['line'];
263
// Ignore heredoc indentation.
264
if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$heredocTokens) === true) {
268
// Ignore multi-line string indentation.
269
if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$stringTokens) === true) {
270
if ($tokens[$i]['code'] === $tokens[($i - 1)]['code']) {
275
// We changed lines, so this should be a whitespace indent token, but first make
276
// sure it isn't a blank line because we don't need to check indent unless there
277
// is actually some code to indent.
278
if ($tokens[$i]['code'] === T_WHITESPACE) {
279
$nextCode = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), ($closeBracket + 1), true);
280
if ($tokens[$nextCode]['line'] !== $lastLine) {
281
$error = 'Empty lines are not allowed in multi-line function calls';
282
$phpcsFile->addError($error, $i, 'EmptyLine');
289
// Check if the next line contains an object operator, if so rely on
290
// the ObjectOperatorIndentSniff to test the indent.
291
if ($tokens[$nextCode]['type'] === 'T_OBJECT_OPERATOR') {
295
if ($nextCode === $closeBracket) {
296
// Closing brace needs to be indented to the same level
297
// as the function call.
298
$expectedIndent = $functionIndent;
300
$expectedIndent = ($functionIndent + $this->indent);
303
if ($tokens[$i]['code'] !== T_WHITESPACE) {
304
// Just check if it is a multi-line block comment. If so, we can
305
// calculate the indent from the whitespace before the content.
306
if ($tokens[$i]['code'] === T_COMMENT
307
&& $tokens[($i - 1)]['code'] === T_COMMENT
309
$trimmed = ltrim($tokens[$i]['content']);
310
$foundIndent = (strlen($tokens[$i]['content']) - strlen($trimmed));
315
$foundIndent = strlen($tokens[$i]['content']);
318
if ($expectedIndent !== $foundIndent) {
319
$error = 'Multi-line function call not indented correctly; expected %s spaces but found %s';
324
$phpcsFile->addError($error, $i, 'Indent', $data);
328
// Skip the rest of a closure.
329
if ($tokens[$i]['code'] === T_CLOSURE) {
330
$i = $tokens[$i]['scope_closer'];
331
$lastLine = $tokens[$i]['line'];
335
// Skip the rest of a short array.
336
if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) {
337
$i = $tokens[$i]['bracket_closer'];
338
$lastLine = $tokens[$i]['line'];
342
if ($this->allowMultipleArguments === false && $tokens[$i]['code'] === T_COMMA) {
343
// Comma has to be the last token on the line.
344
$next = $phpcsFile->findNext(array(T_WHITESPACE, T_COMMENT), ($i + 1), $closeBracket, true);
346
&& $tokens[$i]['line'] === $tokens[$next]['line']
348
$error = 'Only one argument is allowed per line in a multi-line function call';
349
$phpcsFile->addError($error, $next, 'MultipleArguments');
354
if ($tokens[($openBracket + 1)]['content'] !== $phpcsFile->eolChar) {
355
$error = 'Opening parenthesis of a multi-line function call must be the last content on the line';
356
$phpcsFile->addError($error, $stackPtr, 'ContentAfterOpenBracket');
359
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBracket - 1), null, true);
360
if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) {
361
$error = 'Closing parenthesis of a multi-line function call must be on a line by itself';
362
$phpcsFile->addError($error, $closeBracket, 'CloseBracketLine');
365
}//end processMultiLineCall()