5 |
aurelien |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* PEAR_Sniffs_Functions_FunctionDeclarationSniff.
|
|
|
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: MultiLineAssignmentSniff.php,v 1.2 2008/12/01 05:02:12 squiz Exp $
|
|
|
13 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* PEAR_Sniffs_Formatting_MultiLineAssignmentSniff.
|
|
|
18 |
*
|
|
|
19 |
* If an assignment goes over two lines, ensure the equal sign is indented.
|
|
|
20 |
*
|
|
|
21 |
* @category PHP
|
|
|
22 |
* @package PHP_CodeSniffer
|
|
|
23 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
24 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
25 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
26 |
* @version Release: 1.2.0RC1
|
|
|
27 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
28 |
*/
|
|
|
29 |
class PEAR_Sniffs_Formatting_MultiLineAssignmentSniff implements PHP_CodeSniffer_Sniff
|
|
|
30 |
{
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Returns an array of tokens this test wants to listen for.
|
|
|
35 |
*
|
|
|
36 |
* @return array
|
|
|
37 |
*/
|
|
|
38 |
public function register()
|
|
|
39 |
{
|
|
|
40 |
return array(T_EQUAL);
|
|
|
41 |
|
|
|
42 |
}//end register()
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Processes this test, when one of its tokens is encountered.
|
|
|
47 |
*
|
|
|
48 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
49 |
* @param int $stackPtr The position of the current token
|
|
|
50 |
* in the stack passed in $tokens.
|
|
|
51 |
*
|
|
|
52 |
* @return void
|
|
|
53 |
*/
|
|
|
54 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
55 |
{
|
|
|
56 |
$tokens = $phpcsFile->getTokens();
|
|
|
57 |
|
|
|
58 |
// Equal sign can't be the last thing on the line.
|
|
|
59 |
$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
|
|
|
60 |
if ($next === false) {
|
|
|
61 |
// Bad assignment.
|
|
|
62 |
return;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
if ($tokens[$next]['line'] !== $tokens[$stackPtr]['line']) {
|
|
|
66 |
$error = 'Multi-line assignments must have the equal sign on the second line';
|
|
|
67 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
68 |
return;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
// Make sure it is the first thing on the line, otherwise we ignore it.
|
|
|
72 |
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), false, true);
|
|
|
73 |
if ($prev === false) {
|
|
|
74 |
// Bad assignment.
|
|
|
75 |
return;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']) {
|
|
|
79 |
return;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
// Find the required indent based on the ident of the previous line.
|
|
|
83 |
$assignmentIndent = 0;
|
|
|
84 |
$prevLine = $tokens[$prev]['line'];
|
|
|
85 |
for ($i = ($prev - 1); $i >= 0; $i--) {
|
|
|
86 |
if ($tokens[$i]['line'] !== $prevLine) {
|
|
|
87 |
$i++;
|
|
|
88 |
break;
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
if ($tokens[$i]['code'] === T_WHITESPACE) {
|
|
|
93 |
$assignmentIndent = strlen($tokens[$i]['content']);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
// Find the actual indent.
|
|
|
97 |
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1));
|
|
|
98 |
|
|
|
99 |
$expectedIndent = ($assignmentIndent + 4);
|
|
|
100 |
$foundIndent = strlen($tokens[$prev]['content']);
|
|
|
101 |
if ($foundIndent !== $expectedIndent) {
|
|
|
102 |
$error = "Multi-line assignment not indented correctly; expected $expectedIndent spaces but found $foundIndent";
|
|
|
103 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
}//end process()
|
|
|
107 |
|
|
|
108 |
}//end class
|
|
|
109 |
|
|
|
110 |
?>
|