3
* Generic_Sniffs_Functions_FunctionCallArgumentSpacingSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @author Marc McIntyre <mmcintyre@squiz.net>
11
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
12
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
13
* @link http://pear.php.net/package/PHP_CodeSniffer
17
* Generic_Sniffs_Functions_FunctionCallArgumentSpacingSniff.
19
* Checks that calls to methods and functions are spaced correctly.
22
* @package PHP_CodeSniffer
23
* @author Greg Sherwood <gsherwood@squiz.net>
24
* @author Marc McIntyre <mmcintyre@squiz.net>
25
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
26
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
27
* @version Release: 1.3.4
28
* @link http://pear.php.net/package/PHP_CodeSniffer
30
class Generic_Sniffs_Functions_FunctionCallArgumentSpacingSniff implements PHP_CodeSniffer_Sniff
35
* Returns an array of tokens this test wants to listen for.
39
public function register()
41
return array(T_STRING);
47
* Processes this test, when one of its tokens is encountered.
49
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
50
* @param int $stackPtr The position of the current token in the
51
* stack passed in $tokens.
55
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
57
$tokens = $phpcsFile->getTokens();
59
// Skip tokens that are the names of functions or classes
60
// within their definitions. For example:
61
// function myFunction...
62
// "myFunction" is T_STRING but we should skip because it is not a
63
// function or method *call*.
64
$functionName = $stackPtr;
65
$ignoreTokens = PHP_CodeSniffer_Tokens::$emptyTokens;
66
$ignoreTokens[] = T_BITWISE_AND;
67
$functionKeyword = $phpcsFile->findPrevious($ignoreTokens, ($stackPtr - 1), null, true);
68
if ($tokens[$functionKeyword]['code'] === T_FUNCTION || $tokens[$functionKeyword]['code'] === T_CLASS) {
72
// If the next non-whitespace token after the function or method call
73
// is not an opening parenthesis then it cant really be a *call*.
74
$openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($functionName + 1), null, true);
75
if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
79
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
81
$nextSeperator = $openBracket;
82
while (($nextSeperator = $phpcsFile->findNext(array(T_COMMA, T_VARIABLE, T_CLOSURE), ($nextSeperator + 1), $closeBracket)) !== false) {
83
if ($tokens[$nextSeperator]['code'] === T_CLOSURE) {
84
$nextSeperator = $tokens[$nextSeperator]['scope_closer'];
88
// Make sure the comma or variable belongs directly to this function call,
89
// and is not inside a nested function call or array.
90
$brackets = $tokens[$nextSeperator]['nested_parenthesis'];
91
$lastBracket = array_pop($brackets);
92
if ($lastBracket !== $closeBracket) {
96
if ($tokens[$nextSeperator]['code'] === T_COMMA) {
97
if ($tokens[($nextSeperator - 1)]['code'] === T_WHITESPACE) {
98
$error = 'Space found before comma in function call';
99
$phpcsFile->addError($error, $stackPtr, 'SpaceBeforeComma');
102
if ($tokens[($nextSeperator + 1)]['code'] !== T_WHITESPACE) {
103
$error = 'No space found after comma in function call';
104
$phpcsFile->addError($error, $stackPtr, 'NoSpaceAfterComma');
106
// If there is a newline in the space, then the must be formatting
107
// each argument on a newline, which is valid, so ignore it.
108
if (strpos($tokens[($nextSeperator + 1)]['content'], $phpcsFile->eolChar) === false) {
109
$space = strlen($tokens[($nextSeperator + 1)]['content']);
111
$error = 'Expected 1 space after comma in function call; %s found';
112
$data = array($space);
113
$phpcsFile->addError($error, $stackPtr, 'TooMuchSpaceAfterComma', $data);
118
// Token is a variable.
119
$nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($nextSeperator + 1), $closeBracket, true);
120
if ($nextToken !== false) {
121
if ($tokens[$nextToken]['code'] === T_EQUAL) {
122
if (($tokens[($nextToken - 1)]['code']) !== T_WHITESPACE) {
123
$error = 'Expected 1 space before = sign of default value';
124
$phpcsFile->addError($error, $stackPtr, 'NoSpaceBeforeEquals');
127
if ($tokens[($nextToken + 1)]['code'] !== T_WHITESPACE) {
128
$error = 'Expected 1 space after = sign of default value';
129
$phpcsFile->addError($error, $stackPtr, 'NoSpaceAfterEquals');