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_Functions_FunctionCallSignatureSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 * @author    Marc McIntyre <mmcintyre@squiz.net>
11
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
12
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
13
 * @version   CVS: $Id: FunctionCallSignatureSniff.php 284575 2009-07-22 02:58:19Z squiz $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * PEAR_Sniffs_Functions_FunctionCallSignatureSniff.
19
 *
20
 * @category  PHP
21
 * @package   PHP_CodeSniffer
22
 * @author    Greg Sherwood <gsherwood@squiz.net>
23
 * @author    Marc McIntyre <mmcintyre@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.1
27
 * @link      http://pear.php.net/package/PHP_CodeSniffer
28
 */
29
class PEAR_Sniffs_Functions_FunctionCallSignatureSniff 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_STRING);
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
        // Find the next non-empty token.
59
        $openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
60
 
61
        if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
62
            // Not a function call.
63
            return;
64
        }
65
 
66
        if (isset($tokens[$openBracket]['parenthesis_closer']) === false) {
67
            // Not a function call.
68
            return;
69
        }
70
 
71
        // Find the previous non-empty token.
72
        $search   = PHP_CodeSniffer_Tokens::$emptyTokens;
73
        $search[] = T_BITWISE_AND;
74
        $previous = $phpcsFile->findPrevious($search, ($stackPtr - 1), null, true);
75
        if ($tokens[$previous]['code'] === T_FUNCTION) {
76
            // It's a function definition, not a function call.
77
            return;
78
        }
79
 
80
        if ($tokens[$previous]['code'] === T_NEW) {
81
            // We are creating an object, not calling a function.
82
            return;
83
        }
84
 
85
        $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
86
 
87
        if (($stackPtr + 1) !== $openBracket) {
88
            // Checking this: $value = my_function[*](...).
89
            $error = 'Space before opening parenthesis of function call prohibited';
90
            $phpcsFile->addError($error, $stackPtr);
91
        }
92
 
93
        $next = $phpcsFile->findNext(T_WHITESPACE, ($closeBracket + 1), null, true);
94
        if ($tokens[$next]['code'] === T_SEMICOLON) {
95
            if (in_array($tokens[($closeBracket + 1)]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
96
                $error = 'Space after closing parenthesis of function call prohibited';
97
                $phpcsFile->addError($error, $closeBracket);
98
            }
99
        }
100
 
101
        // Check if this is a single line or multi-line function call.
102
        if ($tokens[$openBracket]['line'] === $tokens[$closeBracket]['line']) {
103
            $this->processSingleLineCall($phpcsFile, $stackPtr, $openBracket, $tokens);
104
        } else {
105
            $this->processMultiLineCall($phpcsFile, $stackPtr, $openBracket, $tokens);
106
        }
107
 
108
    }//end process()
109
 
110
 
111
    /**
112
     * Processes single-line calls.
113
     *
114
     * @param PHP_CodeSniffer_File $phpcsFile   The file being scanned.
115
     * @param int                  $stackPtr    The position of the current token
116
     *                                          in the stack passed in $tokens.
117
     * @param int                  $openBracket The position of the openning bracket
118
     *                                          in the stack passed in $tokens.
119
     * @param array                $tokens      The stack of tokens that make up
120
     *                                          the file.
121
     *
122
     * @return void
123
     */
124
    public function processSingleLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens)
125
    {
126
        if ($tokens[($openBracket + 1)]['code'] === T_WHITESPACE) {
127
            // Checking this: $value = my_function([*]...).
128
            $error = 'Space after opening parenthesis of function call prohibited';
129
            $phpcsFile->addError($error, $stackPtr);
130
        }
131
 
132
        $closer = $tokens[$openBracket]['parenthesis_closer'];
133
 
134
        if ($tokens[($closer - 1)]['code'] === T_WHITESPACE) {
135
            // Checking this: $value = my_function(...[*]).
136
            $between = $phpcsFile->findNext(T_WHITESPACE, ($openBracket + 1), null, true);
137
 
138
            // Only throw an error if there is some content between the parenthesis.
139
            // i.e., Checking for this: $value = my_function().
140
            // If there is no content, then we would have thrown an error in the
141
            // previous IF statement because it would look like this:
142
            // $value = my_function( ).
143
            if ($between !== $closer) {
144
                $error = 'Space before closing parenthesis of function call prohibited';
145
                $phpcsFile->addError($error, $closer);
146
            }
147
        }
148
 
149
    }//end processSingleLineCall()
150
 
151
 
152
    /**
153
     * Processes multi-line calls.
154
     *
155
     * @param PHP_CodeSniffer_File $phpcsFile   The file being scanned.
156
     * @param int                  $stackPtr    The position of the current token
157
     *                                          in the stack passed in $tokens.
158
     * @param int                  $openBracket The position of the openning bracket
159
     *                                          in the stack passed in $tokens.
160
     * @param array                $tokens      The stack of tokens that make up
161
     *                                          the file.
162
     *
163
     * @return void
164
     */
165
    public function processMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $openBracket, $tokens)
166
    {
167
        // We need to work out how far indented the function
168
        // call itself is, so we can work out how far to
169
        // indent the arguments.
170
        $functionIndent = 0;
171
        for ($i = ($stackPtr - 1); $i >= 0; $i--) {
172
            if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) {
173
                $i++;
174
                break;
175
            }
176
        }
177
 
178
        if ($tokens[$i]['code'] === T_WHITESPACE) {
179
            $functionIndent = strlen($tokens[$i]['content']);
180
        }
181
 
182
        // Each line between the parenthesis should be indented 4 spaces.
183
        $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
184
        $lastLine     = $tokens[$openBracket]['line'];
185
        for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
186
            // Skip nested function calls.
187
            if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) {
188
                $i        = $tokens[$i]['parenthesis_closer'];
189
                $lastLine = $tokens[$i]['line'];
190
                continue;
191
            }
192
 
193
            if ($tokens[$i]['line'] !== $lastLine) {
194
                $lastLine = $tokens[$i]['line'];
195
 
196
                // We changed lines, so this should be a whitespace indent token.
197
                if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$heredocTokens) === true) {
198
                    // Ignore heredoc indentation.
199
                    continue;
200
                }
201
 
202
                if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$stringTokens) === true) {
203
                    if ($tokens[$i]['code'] === $tokens[($i - 1)]['code']) {
204
                        // Ignore multi-line string indentation.
205
                        continue;
206
                    }
207
                }
208
 
209
                if ($tokens[$i]['line'] === $tokens[$closeBracket]['line']) {
210
                    // Closing brace needs to be indented to the same level
211
                    // as the function call.
212
                    $expectedIndent = $functionIndent;
213
                } else {
214
                    $expectedIndent = ($functionIndent + 4);
215
                }
216
 
217
                if ($tokens[$i]['code'] !== T_WHITESPACE) {
218
                    $foundIndent = 0;
219
                } else {
220
                    $foundIndent = strlen($tokens[$i]['content']);
221
                }
222
 
223
                if ($expectedIndent !== $foundIndent) {
224
                    $error = "Multi-line function call not indented correctly; expected $expectedIndent spaces but found $foundIndent";
225
                    $phpcsFile->addError($error, $i);
226
                }
227
            }//end if
228
        }//end for
229
 
230
        if ($tokens[($openBracket + 1)]['content'] !== $phpcsFile->eolChar) {
231
            $error = 'Opening parenthesis of a multi-line function call must be the last content on the line';
232
            $phpcsFile->addError($error, $stackPtr);
233
        }
234
 
235
        $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBracket - 1), null, true);
236
        if ($tokens[$prev]['line'] === $tokens[$closeBracket]['line']) {
237
            $error = 'Closing parenthesis of a multi-line function call must be on a line by itself';
238
            $phpcsFile->addError($error, $closeBracket);
239
        }
240
 
241
    }//end processMultiLineCall()
242
 
243
 
244
}//end class
245
?>