Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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 288251 2009-09-10 23:50:52Z squiz $
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.1
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
        $varToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
61
        if ($varToken === false || $tokens[$varToken]['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 = ($varToken - 1); $i >= 0; $i--) {
82
            if ($tokens[$i]['line'] !== $tokens[$varToken]['line']) {
83
                $i++;
84
                break;
85
            }
86
        }
87
 
88
        $requiredIndent = 0;
89
        if ($i >= 0 && $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
        // If the first object operator is on a different line than
108
        // the variable, make sure we check its indentation too.
109
        if ($tokens[$stackPtr]['line'] > $tokens[$varToken]['line']) {
110
            $next = $stackPtr;
111
        }
112
 
113
        while ($next !== false) {
114
            // Make sure it is in the same scope, otherwise dont check indent.
115
            $brackets = null;
116
            if (isset($tokens[$next]['nested_parenthesis']) === true) {
117
                $brackets = $tokens[$next]['nested_parenthesis'];
118
            }
119
 
120
            $conditions = null;
121
            if (isset($tokens[$next]['conditions']) === true) {
122
                $conditions = $tokens[$next]['conditions'];
123
            }
124
 
125
            if ($origBrackets === $brackets && $origConditions === $conditions) {
126
                // Make sure it starts a line, otherwise dont check indent.
127
                $indent = $tokens[($next - 1)];
128
                if ($indent['code'] === T_WHITESPACE) {
129
                    if ($indent['line'] === $tokens[$next]['line']) {
130
                        $foundIndent = strlen($indent['content']);
131
                    } else {
132
                        $foundIndent = 0;
133
                    }
134
 
135
                    if ($foundIndent !== $requiredIndent) {
136
                        $error = "Object operator not indented correctly; expected $requiredIndent spaces but found $foundIndent";
137
                        $phpcsFile->addError($error, $next);
138
                    }
139
                }
140
 
141
                // It cant be the last thing on the line either.
142
                $content = $phpcsFile->findNext(T_WHITESPACE, ($next + 1), null, true);
143
                if ($tokens[$content]['line'] !== $tokens[$next]['line']) {
144
                    $error = 'Object operator must be at the start of the line, not the end';
145
                    $phpcsFile->addError($error, $next);
146
                }
147
            }//end if
148
 
149
            $next = $phpcsFile->findNext(
150
                T_OBJECT_OPERATOR,
151
                ($next + 1),
152
                null,
153
                false,
154
                null,
155
                true
156
            );
157
        }//end while
158
 
159
    }//end process()
160
 
161
 
162
}//end class
163
 
164
?>