5 |
aurelien |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* PEAR_Sniffs_ControlStructures_MultiLineConditionSniff.
|
|
|
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: MultiLineConditionSniff.php,v 1.2 2008/12/07 22:25:02 squiz Exp $
|
|
|
13 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* PEAR_Sniffs_ControlStructures_MultiLineConditionSniff.
|
|
|
18 |
*
|
|
|
19 |
* Ensure multi-line IF conditions are defined correctly.
|
|
|
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_ControlStructures_MultiLineConditionSniff 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_IF);
|
|
|
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 |
// We need to work out how far indented the if statement
|
|
|
59 |
// itself is, so we can work out how far to indent conditions.
|
|
|
60 |
$statementIndent = 0;
|
|
|
61 |
for ($i = ($stackPtr - 1); $i >= 0; $i--) {
|
|
|
62 |
if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) {
|
|
|
63 |
$i++;
|
|
|
64 |
break;
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
if ($tokens[$i]['code'] === T_WHITESPACE) {
|
|
|
69 |
$statementIndent = strlen($tokens[$i]['content']);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
// Each line between the parenthesis should be indented 4 spaces
|
|
|
73 |
// and start with an operator.
|
|
|
74 |
$openBracket = $tokens[$stackPtr]['parenthesis_opener'];
|
|
|
75 |
$closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
|
|
|
76 |
$lastLine = $tokens[$openBracket]['line'];
|
|
|
77 |
for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
|
|
|
78 |
if ($tokens[$i]['line'] !== $lastLine) {
|
|
|
79 |
if ($tokens[$i]['line'] === $tokens[$closeBracket]['line']) {
|
|
|
80 |
$next = $phpcsFile->findNext(T_WHITESPACE, $i, null, true);
|
|
|
81 |
if ($next !== $closeBracket) {
|
|
|
82 |
// CLosing bracket is on the same line as a condition.
|
|
|
83 |
$error = 'Closing parenthesis of a multi-line IF statement must be on a new line';
|
|
|
84 |
$phpcsFile->addError($error, $i);
|
|
|
85 |
$expectedIndent = ($statementIndent + 4);
|
|
|
86 |
} else {
|
|
|
87 |
// Closing brace needs to be indented to the same level
|
|
|
88 |
// as the function.
|
|
|
89 |
$expectedIndent = $statementIndent;
|
|
|
90 |
}
|
|
|
91 |
} else {
|
|
|
92 |
$expectedIndent = ($statementIndent + 4);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
// We changed lines, so this should be a whitespace indent token.
|
|
|
96 |
if ($tokens[$i]['code'] !== T_WHITESPACE) {
|
|
|
97 |
$foundIndent = 0;
|
|
|
98 |
} else {
|
|
|
99 |
$foundIndent = strlen($tokens[$i]['content']);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
if ($expectedIndent !== $foundIndent) {
|
|
|
103 |
$error = "Multi-line IF statement not indented correctly; expected $expectedIndent spaces but found $foundIndent";
|
|
|
104 |
$phpcsFile->addError($error, $i);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
if ($tokens[$i]['line'] !== $tokens[$closeBracket]['line']) {
|
|
|
108 |
$next = $phpcsFile->findNext(T_WHITESPACE, $i, null, true);
|
|
|
109 |
if (in_array($tokens[$next]['code'], PHP_CodeSniffer_Tokens::$booleanOperators) === false) {
|
|
|
110 |
$error = 'Each line in a multi-line IF statement must begin with a boolean operator';
|
|
|
111 |
$phpcsFile->addError($error, $i);
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
$lastLine = $tokens[$i]['line'];
|
|
|
116 |
}//end if
|
|
|
117 |
}//end for
|
|
|
118 |
|
|
|
119 |
// The openning brace needs to be one space away
|
|
|
120 |
// from the closing parenthesis.
|
|
|
121 |
if ($tokens[($closeBracket + 1)]['code'] !== T_WHITESPACE) {
|
|
|
122 |
$length = 0;
|
|
|
123 |
} else {
|
|
|
124 |
$length = strlen($tokens[($closeBracket + 1)]['content']);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
if ($length !== 1) {
|
|
|
128 |
$error = "There must be a single space between the closing parenthesis and the openning brace of a multi-line IF statement; found $length spaces";
|
|
|
129 |
$phpcsFile->addError($error, ($closeBracket + 1));
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
// And just in case they do something funny before the brace...
|
|
|
133 |
$next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), null, true);
|
|
|
134 |
if ($next !== false && $tokens[$next]['code'] !== T_OPEN_CURLY_BRACKET) {
|
|
|
135 |
$error = 'There must be a single space between the closing parenthesis and the openning brace of a multi-line IF statement';
|
|
|
136 |
$phpcsFile->addError($error, $next);
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
}//end process()
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
}//end class
|
|
|
143 |
|
|
|
144 |
?>
|