5 |
aurelien |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* PEAR_Sniffs_WhiteSpace_ObjectOperatorIndentSniff.
|
|
|
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: ObjectOperatorIndentSniff.php,v 1.2 2009/02/20 00:26:47 squiz Exp $
|
|
|
13 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* PEAR_Sniffs_WhiteSpace_ObjectOperatorIndentSniff.
|
|
|
18 |
*
|
|
|
19 |
* Checks that object operators are indented 4 spaces if they are the first
|
|
|
20 |
* thing on a line.
|
|
|
21 |
*
|
|
|
22 |
* @category PHP
|
|
|
23 |
* @package PHP_CodeSniffer
|
|
|
24 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
25 |
* @copyright 2006 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.2.0RC1
|
|
|
28 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
29 |
*/
|
|
|
30 |
class PEAR_Sniffs_WhiteSpace_ObjectOperatorIndentSniff implements PHP_CodeSniffer_Sniff
|
|
|
31 |
{
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Returns an array of tokens this test wants to listen for.
|
|
|
36 |
*
|
|
|
37 |
* @return array
|
|
|
38 |
*/
|
|
|
39 |
public function register()
|
|
|
40 |
{
|
|
|
41 |
return array(T_OBJECT_OPERATOR);
|
|
|
42 |
|
|
|
43 |
}//end register()
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Processes this test, when one of its tokens is encountered.
|
|
|
48 |
*
|
|
|
49 |
* @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
|
|
|
50 |
* @param int $stackPtr The position of the current token
|
|
|
51 |
* in the stack passed in $tokens.
|
|
|
52 |
*
|
|
|
53 |
* @return void
|
|
|
54 |
*/
|
|
|
55 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
56 |
{
|
|
|
57 |
$tokens = $phpcsFile->getTokens();
|
|
|
58 |
|
|
|
59 |
// Make sure this is the first object operator in a chain of them.
|
|
|
60 |
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
|
|
|
61 |
if ($prev === false || $tokens[$prev]['code'] !== T_VARIABLE) {
|
|
|
62 |
return;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
// Make sure this is a chained call.
|
|
|
66 |
$next = $phpcsFile->findNext(
|
|
|
67 |
T_OBJECT_OPERATOR,
|
|
|
68 |
($stackPtr + 1),
|
|
|
69 |
null,
|
|
|
70 |
false,
|
|
|
71 |
null,
|
|
|
72 |
true
|
|
|
73 |
);
|
|
|
74 |
|
|
|
75 |
if ($next === false) {
|
|
|
76 |
// Not a chained call.
|
|
|
77 |
return;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
// Determine correct indent.
|
|
|
81 |
for ($i = ($stackPtr - 1); $i >= 0; $i--) {
|
|
|
82 |
if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) {
|
|
|
83 |
$i++;
|
|
|
84 |
break;
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
$requiredIndent = 0;
|
|
|
89 |
if ($tokens[$i]['code'] === T_WHITESPACE) {
|
|
|
90 |
$requiredIndent = strlen($tokens[$i]['content']);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
$requiredIndent += 4;
|
|
|
94 |
|
|
|
95 |
// Determine the scope of the original object operator.
|
|
|
96 |
$origBrackets = null;
|
|
|
97 |
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
|
|
|
98 |
$origBrackets = $tokens[$stackPtr]['nested_parenthesis'];
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
$origConditions = null;
|
|
|
102 |
if (isset($tokens[$stackPtr]['conditions']) === true) {
|
|
|
103 |
$origConditions = $tokens[$stackPtr]['conditions'];
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
// Check indentation of each object operator in the chain.
|
|
|
107 |
while ($next !== false) {
|
|
|
108 |
// Make sure it is in the same scope, otherwise dont check indent.
|
|
|
109 |
$brackets = null;
|
|
|
110 |
if (isset($tokens[$next]['nested_parenthesis']) === true) {
|
|
|
111 |
$brackets = $tokens[$next]['nested_parenthesis'];
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
$conditions = null;
|
|
|
115 |
if (isset($tokens[$next]['conditions']) === true) {
|
|
|
116 |
$conditions = $tokens[$next]['conditions'];
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
if ($origBrackets === $brackets && $origConditions === $conditions) {
|
|
|
120 |
// Make sure it starts a line, otherwise dont check indent.
|
|
|
121 |
$indent = $tokens[($next - 1)];
|
|
|
122 |
if ($indent['code'] === T_WHITESPACE) {
|
|
|
123 |
if ($indent['line'] === $tokens[$next]['line']) {
|
|
|
124 |
$foundIndent = strlen($indent['content']);
|
|
|
125 |
} else {
|
|
|
126 |
$foundIndent = 0;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
if ($foundIndent !== $requiredIndent) {
|
|
|
130 |
$error = "Object operator not indented correctly; expected $requiredIndent spaces but found $foundIndent";
|
|
|
131 |
$phpcsFile->addError($error, $next);
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
// It cant be the last thing on the line either.
|
|
|
136 |
$content = $phpcsFile->findNext(T_WHITESPACE, ($next + 1), null, true);
|
|
|
137 |
if ($tokens[$content]['line'] !== $tokens[$next]['line']) {
|
|
|
138 |
$error = 'Object operator must be at the start of the line, not the end';
|
|
|
139 |
$phpcsFile->addError($error, $next);
|
|
|
140 |
}
|
|
|
141 |
}//end if
|
|
|
142 |
|
|
|
143 |
$next = $phpcsFile->findNext(
|
|
|
144 |
T_OBJECT_OPERATOR,
|
|
|
145 |
($next + 1),
|
|
|
146 |
null,
|
|
|
147 |
false,
|
|
|
148 |
null,
|
|
|
149 |
true
|
|
|
150 |
);
|
|
|
151 |
}//end while
|
|
|
152 |
|
|
|
153 |
}//end process()
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
}//end class
|
|
|
157 |
|
|
|
158 |
?>
|