3
* PEAR_Sniffs_ControlStructures_MultiLineConditionSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
11
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
12
* @link http://pear.php.net/package/PHP_CodeSniffer
16
* PEAR_Sniffs_ControlStructures_MultiLineConditionSniff.
18
* Ensure multi-line IF conditions are defined correctly.
21
* @package PHP_CodeSniffer
22
* @author Greg Sherwood <gsherwood@squiz.net>
23
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
24
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
25
* @version Release: 1.3.4
26
* @link http://pear.php.net/package/PHP_CodeSniffer
28
class PEAR_Sniffs_ControlStructures_MultiLineConditionSniff implements PHP_CodeSniffer_Sniff
33
* Returns an array of tokens this test wants to listen for.
37
public function register()
45
* Processes this test, when one of its tokens is encountered.
47
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
48
* @param int $stackPtr The position of the current token
49
* in the stack passed in $tokens.
53
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
55
$tokens = $phpcsFile->getTokens();
57
// We need to work out how far indented the if statement
58
// itself is, so we can work out how far to indent conditions.
60
for ($i = ($stackPtr - 1); $i >= 0; $i--) {
61
if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) {
67
if ($i >= 0 && $tokens[$i]['code'] === T_WHITESPACE) {
68
$statementIndent = strlen($tokens[$i]['content']);
71
// Each line between the parenthesis should be indented 4 spaces
72
// and start with an operator, unless the line is inside a
73
// function call, in which case it is ignored.
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, 'CloseBracketNewLine');
85
$expectedIndent = ($statementIndent + 4);
87
// Closing brace needs to be indented to the same level
89
$expectedIndent = $statementIndent;
92
$expectedIndent = ($statementIndent + 4);
95
// We changed lines, so this should be a whitespace indent token.
96
if ($tokens[$i]['code'] !== T_WHITESPACE) {
99
$foundIndent = strlen($tokens[$i]['content']);
102
if ($expectedIndent !== $foundIndent) {
103
$error = 'Multi-line IF statement not indented correctly; expected %s spaces but found %s';
108
$phpcsFile->addError($error, $i, 'Alignment', $data);
111
if ($tokens[$i]['line'] !== $tokens[$closeBracket]['line']) {
112
$next = $phpcsFile->findNext(T_WHITESPACE, $i, null, true);
113
if (in_array($tokens[$next]['code'], PHP_CodeSniffer_Tokens::$booleanOperators) === false) {
114
$error = 'Each line in a multi-line IF statement must begin with a boolean operator';
115
$phpcsFile->addError($error, $i, 'StartWithBoolean');
119
$lastLine = $tokens[$i]['line'];
122
if ($tokens[$i]['code'] === T_STRING) {
123
$next = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), null, true);
124
if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) {
125
// This is a function call, so skip to the end as they
126
// have their own indentation rules.
127
$i = $tokens[$next]['parenthesis_closer'];
128
$lastLine = $tokens[$i]['line'];
134
// From here on, we are checking the spacing of the opening and closing
135
// braces. If this IF statement does not use braces, we end here.
136
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
140
// The opening brace needs to be one space away from the closing parenthesis.
141
if ($tokens[($closeBracket + 1)]['code'] !== T_WHITESPACE) {
143
} else if ($tokens[($closeBracket + 1)]['content'] === $phpcsFile->eolChar) {
146
$length = strlen($tokens[($closeBracket + 1)]['content']);
150
$data = array($length);
151
$code = 'SpaceBeforeOpenBrace';
153
$error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line IF statement; found ';
154
if ($length === -1) {
156
$code = 'NewlineBeforeOpenBrace';
158
$error .= '%s spaces';
161
$phpcsFile->addError($error, ($closeBracket + 1), $code, $data);
164
// And just in case they do something funny before the brace...
165
$next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), null, true);
166
if ($next !== false && $tokens[$next]['code'] !== T_OPEN_CURLY_BRACKET) {
167
$error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line IF statement';
168
$phpcsFile->addError($error, $next, 'NoSpaceBeforeOpenBrace');