~ubuntu-branches/ubuntu/vivid/php-codesniffer/vivid

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.3.4/CodeSniffer/Standards/PEAR/Sniffs/ControlStructures/MultiLineConditionSniff.php

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2013-07-12 15:16:25 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20130712151625-4autdc0twzbueha9
Tags: 1.5.0~rc2-1
* New upstream release.
* Refreshed patch.
* Standards-Version is now 3.9.4.
* Canonical VCS URLs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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-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
13
 
 */
14
 
 
15
 
/**
16
 
 * PEAR_Sniffs_ControlStructures_MultiLineConditionSniff.
17
 
 *
18
 
 * Ensure multi-line IF conditions are defined correctly.
19
 
 *
20
 
 * @category  PHP
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
27
 
 */
28
 
class PEAR_Sniffs_ControlStructures_MultiLineConditionSniff implements PHP_CodeSniffer_Sniff
29
 
{
30
 
 
31
 
 
32
 
    /**
33
 
     * Returns an array of tokens this test wants to listen for.
34
 
     *
35
 
     * @return array
36
 
     */
37
 
    public function register()
38
 
    {
39
 
        return array(T_IF);
40
 
 
41
 
    }//end register()
42
 
 
43
 
 
44
 
    /**
45
 
     * Processes this test, when one of its tokens is encountered.
46
 
     *
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.
50
 
     *
51
 
     * @return void
52
 
     */
53
 
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
54
 
    {
55
 
        $tokens = $phpcsFile->getTokens();
56
 
 
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.
59
 
        $statementIndent = 0;
60
 
        for ($i = ($stackPtr - 1); $i >= 0; $i--) {
61
 
            if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) {
62
 
                $i++;
63
 
                break;
64
 
            }
65
 
        }
66
 
 
67
 
        if ($i >= 0 && $tokens[$i]['code'] === T_WHITESPACE) {
68
 
            $statementIndent = strlen($tokens[$i]['content']);
69
 
        }
70
 
 
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);
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 %s spaces but found %s';
104
 
                    $data  = array(
105
 
                              $expectedIndent,
106
 
                              $foundIndent,
107
 
                             );
108
 
                    $phpcsFile->addError($error, $i, 'Alignment', $data);
109
 
                }
110
 
 
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');
116
 
                    }
117
 
                }
118
 
 
119
 
                $lastLine = $tokens[$i]['line'];
120
 
            }//end if
121
 
 
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'];
129
 
                    continue;
130
 
                }
131
 
            }
132
 
        }//end for
133
 
 
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) {
137
 
            return;
138
 
        }
139
 
 
140
 
        // The opening brace needs to be one space away from the closing parenthesis.
141
 
        if ($tokens[($closeBracket + 1)]['code'] !== T_WHITESPACE) {
142
 
            $length = 0;
143
 
        } else if ($tokens[($closeBracket + 1)]['content'] === $phpcsFile->eolChar) {
144
 
            $length = -1;
145
 
        } else {
146
 
            $length = strlen($tokens[($closeBracket + 1)]['content']);
147
 
        }
148
 
 
149
 
        if ($length !== 1) {
150
 
            $data = array($length);
151
 
            $code = 'SpaceBeforeOpenBrace';
152
 
 
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) {
155
 
                $error .= 'newline';
156
 
                $code   = 'NewlineBeforeOpenBrace';
157
 
            } else {
158
 
                $error .= '%s spaces';
159
 
            }
160
 
 
161
 
            $phpcsFile->addError($error, ($closeBracket + 1), $code, $data);
162
 
        }
163
 
 
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');
169
 
        }
170
 
 
171
 
    }//end process()
172
 
 
173
 
 
174
 
}//end class
175
 
 
176
 
?>