3
* PEAR_Sniffs_Functions_FunctionDeclarationSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12
* @link http://pear.php.net/package/PHP_CodeSniffer
16
* PEAR_Sniffs_Functions_FunctionDeclarationSniff.
18
* Ensure single and multi-line function declarations are defined correctly.
21
* @package PHP_CodeSniffer
22
* @author Greg Sherwood <gsherwood@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.5
26
* @link http://pear.php.net/package/PHP_CodeSniffer
28
class PEAR_Sniffs_Functions_FunctionDeclarationSniff implements PHP_CodeSniffer_Sniff
32
* The number of spaces code should be indented.
40
* Returns an array of tokens this test wants to listen for.
44
public function register()
55
* Processes this test, when one of its tokens is encountered.
57
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
58
* @param int $stackPtr The position of the current token
59
* in the stack passed in $tokens.
63
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
65
$tokens = $phpcsFile->getTokens();
68
if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) {
69
$spaces = strlen($tokens[($stackPtr + 1)]['content']);
73
$error = 'Expected 1 space after FUNCTION keyword; %s found';
74
$data = array($spaces);
75
$phpcsFile->addError($error, $stackPtr, 'SpaceAfterFunction', $data);
78
// Must be one space before and after USE keyword for closures.
79
$openBracket = $tokens[$stackPtr]['parenthesis_opener'];
80
$closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
81
if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
82
$use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']);
84
if ($tokens[($use + 1)]['code'] !== T_WHITESPACE) {
86
} else if ($tokens[($use + 1)]['content'] === "\t") {
89
$length = strlen($tokens[($use + 1)]['content']);
93
$error = 'Expected 1 space after USE keyword; found %s';
94
$data = array($length);
95
$phpcsFile->addError($error, $use, 'SpaceAfterUse', $data);
98
if ($tokens[($use - 1)]['code'] !== T_WHITESPACE) {
100
} else if ($tokens[($use - 1)]['content'] === "\t") {
103
$length = strlen($tokens[($use - 1)]['content']);
107
$error = 'Expected 1 space before USE keyword; found %s';
108
$data = array($length);
109
$phpcsFile->addError($error, $use, 'SpaceBeforeUse', $data);
114
// Check if this is a single line or multi-line declaration.
116
if ($tokens[$openBracket]['line'] === $tokens[$closeBracket]['line']) {
117
// Closures may use the USE keyword and so be multi-line in this way.
118
if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
119
if ($use !== false) {
120
// If the opening and closing parenthesis of the use statement
121
// are also on the same line, this is a single line declaration.
122
$open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1));
123
$close = $tokens[$open]['parenthesis_closer'];
124
if ($tokens[$open]['line'] !== $tokens[$close]['line']) {
133
if ($singleLine === true) {
134
$this->processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens);
136
$this->processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens);
143
* Processes single-line declarations.
145
* Just uses the Generic BSD-Allman brace sniff.
147
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
148
* @param int $stackPtr The position of the current token
149
* in the stack passed in $tokens.
150
* @param array $tokens The stack of tokens that make up
155
public function processSingleLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens)
157
if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
158
if (class_exists('Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff', true) === false) {
159
throw new PHP_CodeSniffer_Exception('Class Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff not found');
162
$sniff = new Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff();
164
if (class_exists('Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff', true) === false) {
165
throw new PHP_CodeSniffer_Exception('Class Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff not found');
168
$sniff = new Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff();
171
$sniff->process($phpcsFile, $stackPtr);
173
}//end processSingleLineDeclaration()
177
* Processes mutli-line declarations.
179
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
180
* @param int $stackPtr The position of the current token
181
* in the stack passed in $tokens.
182
* @param array $tokens The stack of tokens that make up
187
public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens)
189
// We need to work out how far indented the function
190
// declaration itself is, so we can work out how far to
191
// indent parameters.
193
for ($i = ($stackPtr - 1); $i >= 0; $i--) {
194
if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) {
200
if ($tokens[$i]['code'] === T_WHITESPACE) {
201
$functionIndent = strlen($tokens[$i]['content']);
204
// The closing parenthesis must be on a new line, even
205
// when checking abstract function definitions.
206
$closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
207
$prev = $phpcsFile->findPrevious(
214
if ($tokens[$closeBracket]['line'] !== $tokens[$tokens[$closeBracket]['parenthesis_opener']]['line']) {
215
if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) {
216
$error = 'The closing parenthesis of a multi-line function declaration must be on a new line';
217
$phpcsFile->addError($error, $closeBracket, 'CloseBracketLine');
221
// If this is a closure and is using a USE statement, the closing
222
// parenthesis we need to look at from now on is the closing parenthesis
223
// of the USE statement.
224
if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
225
$use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']);
226
if ($use !== false) {
227
$open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1));
228
$closeBracket = $tokens[$open]['parenthesis_closer'];
230
$prev = $phpcsFile->findPrevious(
237
if ($tokens[$closeBracket]['line'] !== $tokens[$tokens[$closeBracket]['parenthesis_opener']]['line']) {
238
if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) {
239
$error = 'The closing parenthesis of a multi-line use declaration must be on a new line';
240
$phpcsFile->addError($error, $closeBracket, 'CloseBracketLine');
246
// Each line between the parenthesis should be indented 4 spaces.
247
$openBracket = $tokens[$stackPtr]['parenthesis_opener'];
248
$lastLine = $tokens[$openBracket]['line'];
249
for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
250
if ($tokens[$i]['line'] !== $lastLine) {
251
if ($i === $tokens[$stackPtr]['parenthesis_closer']
252
|| ($tokens[$i]['code'] === T_WHITESPACE
253
&& (($i + 1) === $closeBracket
254
|| ($i + 1) === $tokens[$stackPtr]['parenthesis_closer']))
256
// Closing braces need to be indented to the same level
258
$expectedIndent = $functionIndent;
260
$expectedIndent = ($functionIndent + $this->indent);
263
// We changed lines, so this should be a whitespace indent token.
264
if ($tokens[$i]['code'] !== T_WHITESPACE) {
267
$foundIndent = strlen($tokens[$i]['content']);
270
if ($expectedIndent !== $foundIndent) {
271
$error = 'Multi-line function declaration not indented correctly; expected %s spaces but found %s';
276
$phpcsFile->addError($error, $i, 'Indent', $data);
279
$lastLine = $tokens[$i]['line'];
282
if ($tokens[$i]['code'] === T_ARRAY || $tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) {
283
// Skip arrays as they have their own indentation rules.
284
if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) {
285
$i = $tokens[$i]['bracket_closer'];
287
$i = $tokens[$i]['parenthesis_closer'];
290
$lastLine = $tokens[$i]['line'];
295
if (isset($tokens[$stackPtr]['scope_opener']) === true) {
296
// The openning brace needs to be one space away
297
// from the closing parenthesis.
298
$next = $tokens[($closeBracket + 1)];
299
if ($next['code'] !== T_WHITESPACE) {
301
} else if ($next['content'] === $phpcsFile->eolChar) {
304
$length = strlen($next['content']);
308
$data = array($length);
309
$code = 'SpaceBeforeOpenBrace';
311
$error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration; found ';
312
if ($length === -1) {
314
$code = 'NewlineBeforeOpenBrace';
316
$error .= '%s spaces';
319
$phpcsFile->addError($error, ($closeBracket + 1), $code, $data);
323
// And just in case they do something funny before the brace...
324
$next = $phpcsFile->findNext(
331
if ($next !== false && $tokens[$next]['code'] !== T_OPEN_CURLY_BRACKET) {
332
$error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration';
333
$phpcsFile->addError($error, $next, 'NoSpaceBeforeOpenBrace');
337
}//end processMultiLineDeclaration()