| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Squiz_Sniffs_Functions_MultiLineFunctionDeclarationSniff.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PHP_CodeSniffer
|
|
|
9 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
10 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
11 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
12 |
* @version CVS: $Id: MultiLineFunctionDeclarationSniff.php 269973 2008-11-28 01:05:37Z squiz $
|
|
|
13 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
if (class_exists('PEAR_Sniffs_Functions_FunctionDeclarationSniff', true) === false) {
|
|
|
17 |
$error = 'Class PEAR_Sniffs_Functions_FunctionDeclarationSniff not found';
|
|
|
18 |
throw new PHP_CodeSniffer_Exception($error);
|
|
|
19 |
}
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Squiz_Sniffs_Functions_MultiLineFunctionDeclarationSniff.
|
|
|
23 |
*
|
|
|
24 |
* Ensure single and multi-line function declarations are defined correctly.
|
|
|
25 |
*
|
|
|
26 |
* @category PHP
|
|
|
27 |
* @package PHP_CodeSniffer
|
|
|
28 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
29 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
30 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
31 |
* @version Release: 1.2.1
|
|
|
32 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
33 |
*/
|
|
|
34 |
class Squiz_Sniffs_Functions_MultiLineFunctionDeclarationSniff extends PEAR_Sniffs_Functions_FunctionDeclarationSniff
|
|
|
35 |
{
|
|
|
36 |
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Processes mutli-line declarations.
|
|
|
40 |
*
|
|
|
41 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
42 |
* @param int $stackPtr The position of the current token
|
|
|
43 |
* in the stack passed in $tokens.
|
|
|
44 |
* @param array $tokens The stack of tokens that make up
|
|
|
45 |
* the file.
|
|
|
46 |
*
|
|
|
47 |
* @return void
|
|
|
48 |
*/
|
|
|
49 |
public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens)
|
|
|
50 |
{
|
|
|
51 |
// We do everything the parent sniff does, and a bit more.
|
|
|
52 |
parent::processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens);
|
|
|
53 |
|
|
|
54 |
$openBracket = $tokens[$stackPtr]['parenthesis_opener'];
|
|
|
55 |
$closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
|
|
|
56 |
|
|
|
57 |
// The open bracket should be the last thing on the line.
|
|
|
58 |
$next = $phpcsFile->findNext(T_WHITESPACE, ($openBracket + 1), null, true);
|
|
|
59 |
if ($tokens[$next]['line'] !== ($tokens[$openBracket]['line'] + 1)) {
|
|
|
60 |
$error = 'The first parameter of a multi-line function declaration must be on the line after the opening bracket';
|
|
|
61 |
$phpcsFile->addError($error, $next);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
// Each line between the brackets should contain a single parameter.
|
|
|
65 |
$lastCommaLine = null;
|
|
|
66 |
for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
|
|
|
67 |
// Skip brackets, like arrays, as they can contain commas.
|
|
|
68 |
if (isset($tokens[$i]['parenthesis_opener']) === true) {
|
|
|
69 |
$i = $tokens[$i]['parenthesis_closer'];
|
|
|
70 |
continue;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
if ($tokens[$i]['code'] === T_COMMA) {
|
|
|
74 |
if ($lastCommaLine !== null && $lastCommaLine === $tokens[$i]['line']) {
|
|
|
75 |
$error = 'Multi-line function declarations must define one parameter per line';
|
|
|
76 |
$phpcsFile->addError($error, $i);
|
|
|
77 |
} else {
|
|
|
78 |
// Comma must be the last thing on the line.
|
|
|
79 |
$next = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), null, true);
|
|
|
80 |
if ($tokens[$next]['line'] !== ($tokens[$i]['line'] + 1)) {
|
|
|
81 |
$error = 'Commas in multi-line function declarations must be the last content on a line';
|
|
|
82 |
$phpcsFile->addError($error, $next);
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
$lastCommaLine = $tokens[$i]['line'];
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
}//end processMultiLineDeclaration()
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
}//end class
|
|
|
94 |
|
|
|
95 |
?>
|