3
* Squiz_Sniffs_Functions_MultiLineFunctionDeclarationSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @copyright 2006-2012 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
15
if (class_exists('PEAR_Sniffs_Functions_FunctionDeclarationSniff', true) === false) {
16
$error = 'Class PEAR_Sniffs_Functions_FunctionDeclarationSniff not found';
17
throw new PHP_CodeSniffer_Exception($error);
21
* Squiz_Sniffs_Functions_MultiLineFunctionDeclarationSniff.
23
* Ensure single and multi-line function declarations are defined correctly.
26
* @package PHP_CodeSniffer
27
* @author Greg Sherwood <gsherwood@squiz.net>
28
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
29
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
30
* @version Release: 1.5.0RC2
31
* @link http://pear.php.net/package/PHP_CodeSniffer
33
class Squiz_Sniffs_Functions_MultiLineFunctionDeclarationSniff extends PEAR_Sniffs_Functions_FunctionDeclarationSniff
38
* Processes multi-line declarations.
40
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
41
* @param int $stackPtr The position of the current token
42
* in the stack passed in $tokens.
43
* @param array $tokens The stack of tokens that make up
48
public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens)
50
// We do everything the parent sniff does, and a bit more.
51
parent::processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens);
53
$openBracket = $tokens[$stackPtr]['parenthesis_opener'];
54
$this->processBracket($phpcsFile, $openBracket, $tokens, 'function');
56
if ($tokens[$stackPtr]['code'] !== T_CLOSURE) {
60
$use = $phpcsFile->findNext(T_USE, ($tokens[$stackPtr]['parenthesis_closer'] + 1), $tokens[$stackPtr]['scope_opener']);
65
$openBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1), null);
66
$this->processBracket($phpcsFile, $openBracket, $tokens, 'use');
68
// Also check spacing.
69
if ($tokens[($use - 1)]['code'] === T_WHITESPACE) {
70
$gap = strlen($tokens[($use - 1)]['content']);
75
}//end processMultiLineDeclaration()
79
* Processes the contents of a single set of brackets.
81
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
82
* @param int $openBracket The position of the open bracket
83
* in the stack passed in $tokens.
84
* @param array $tokens The stack of tokens that make up
86
* @param string $type The type of the token the brackets
87
* belong to (function or use).
91
public function processBracket(PHP_CodeSniffer_File $phpcsFile, $openBracket, $tokens, $type='function')
94
if ($type === 'use') {
98
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
100
// The open bracket should be the last thing on the line.
101
if ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']) {
102
$next = $phpcsFile->findNext(T_WHITESPACE, ($openBracket + 1), null, true);
103
if ($tokens[$next]['line'] !== ($tokens[$openBracket]['line'] + 1)) {
104
$error = 'The first parameter of a multi-line '.$type.' declaration must be on the line after the opening bracket';
105
$phpcsFile->addError($error, $next, $errorPrefix.'FirstParamSpacing');
109
// Each line between the brackets should contain a single parameter.
110
$lastCommaLine = null;
111
for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
112
// Skip brackets, like arrays, as they can contain commas.
113
if (isset($tokens[$i]['parenthesis_opener']) === true) {
114
$i = $tokens[$i]['parenthesis_closer'];
118
if ($tokens[$i]['code'] === T_COMMA) {
119
if ($lastCommaLine !== null && $lastCommaLine === $tokens[$i]['line']) {
120
$error = 'Multi-line '.$type.' declarations must define one parameter per line';
121
$phpcsFile->addError($error, $i, $errorPrefix.'OneParamPerLine');
123
// Comma must be the last thing on the line.
124
$next = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), null, true);
125
if ($tokens[$next]['line'] !== ($tokens[$i]['line'] + 1)) {
126
$error = 'Commas in multi-line '.$type.' declarations must be the last content on a line';
127
$phpcsFile->addError($error, $next, $errorPrefix.'ContentAfterComma');
131
$lastCommaLine = $tokens[$i]['line'];
135
}//end processBracket()