Subversion Repositories Applications.framework

Rev

Rev 5 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 aurelien 1
<?php
2
/**
3
 * PEAR_Sniffs_Functions_FunctionDeclarationSniff.
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
34 aurelien 12
 * @version   CVS: $Id: FunctionDeclarationSniff.php 34 2009-04-09 07:34:39Z aurelien $
5 aurelien 13
 * @link      http://pear.php.net/package/PHP_CodeSniffer
14
 */
15
 
16
/**
17
 * PEAR_Sniffs_Functions_FunctionDeclarationSniff.
18
 *
19
 * Ensure single and multi-line function declarations 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_Functions_FunctionDeclarationSniff 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_FUNCTION);
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
        // Check if this is a single line or multi-line declaration.
59
        $openBracket  = $tokens[$stackPtr]['parenthesis_opener'];
60
        $closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
61
        if ($tokens[$openBracket]['line'] === $tokens[$closeBracket]['line']) {
62
            $this->processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens);
63
        } else {
64
            $this->processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens);
65
        }
66
 
67
    }//end process()
68
 
69
 
70
    /**
71
     * Processes single-line declarations.
72
     *
73
     * Just uses the Generic BSD-Allman brace sniff.
74
     *
75
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
76
     * @param int                  $stackPtr  The position of the current token
77
     *                                        in the stack passed in $tokens.
78
     * @param array                $tokens    The stack of tokens that make up
79
     *                                        the file.
80
     *
81
     * @return void
82
     */
83
    public function processSingleLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens)
84
    {
85
        if (class_exists('Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff', true) === false) {
86
            throw new PHP_CodeSniffer_Exception('Class Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff not found');
87
        }
88
 
89
        $sniff = new Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff();
90
        $sniff->process($phpcsFile, $stackPtr);
91
 
92
    }//end processSingleLineDeclaration()
93
 
94
 
95
    /**
96
     * Processes mutli-line declarations.
97
     *
98
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
99
     * @param int                  $stackPtr  The position of the current token
100
     *                                        in the stack passed in $tokens.
101
     * @param array                $tokens    The stack of tokens that make up
102
     *                                        the file.
103
     *
104
     * @return void
105
     */
106
    public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens)
107
    {
108
        // We need to work out how far indented the function
109
        // declaration itself is, so we can work out how far to
110
        // indent parameters.
111
        $functionIndent = 0;
112
        for ($i = ($stackPtr - 1); $i >= 0; $i--) {
113
            if ($tokens[$i]['line'] !== $tokens[$stackPtr]['line']) {
114
                $i++;
115
                break;
116
            }
117
        }
118
 
119
        if ($tokens[$i]['code'] === T_WHITESPACE) {
120
            $functionIndent = strlen($tokens[$i]['content']);
121
        }
122
 
123
        // Each line between the parenthesis should be indented 4 spaces.
124
        $openBracket  = $tokens[$stackPtr]['parenthesis_opener'];
125
        $closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
126
        $lastLine     = $tokens[$openBracket]['line'];
127
        for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
128
            if ($tokens[$i]['line'] !== $lastLine) {
129
                if ($tokens[$i]['line'] === $tokens[$closeBracket]['line']) {
130
                    // Closing brace needs to be indented to the same level
131
                    // as the function.
132
                    $expectedIndent = $functionIndent;
133
                } else {
134
                    $expectedIndent = ($functionIndent + 4);
135
                }
136
 
137
                // We changed lines, so this should be a whitespace indent token.
138
                if ($tokens[$i]['code'] !== T_WHITESPACE) {
139
                    $foundIndent = 0;
140
                } else {
141
                    $foundIndent = strlen($tokens[$i]['content']);
142
                }
143
 
144
                if ($expectedIndent !== $foundIndent) {
145
                    $error = "Multi-line function declaration not indented correctly; expected $expectedIndent spaces but found $foundIndent";
146
                    $phpcsFile->addError($error, $i);
147
                }
148
 
149
                $lastLine = $tokens[$i]['line'];
150
            }
151
        }
152
 
153
        if (isset($tokens[$stackPtr]['scope_opener']) === true) {
154
            // The openning brace needs to be one space away
155
            // from the closing parenthesis.
156
            $next = $tokens[($closeBracket + 1)];
157
            if ($next['code'] !== T_WHITESPACE) {
158
                $length = 0;
159
            } else if ($next['content'] === $phpcsFile->eolChar) {
160
                $length = -1;
161
            } else {
162
                $length = strlen($next['content']);
163
            }
164
 
165
            if ($length !== 1) {
166
                $error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration; found ';
167
                if ($length === -1) {
168
                    $error .= 'newline';
169
                } else {
170
                    $error .= "$length spaces";
171
                }
172
 
173
                $phpcsFile->addError($error, ($closeBracket + 1));
174
                return;
175
            }
176
 
177
            // And just in case they do something funny before the brace...
178
            $next = $phpcsFile->findNext(
179
                T_WHITESPACE,
180
                ($closeBracket + 1),
181
                null,
182
                true
183
            );
184
 
185
            if ($next !== false && $tokens[$next]['code'] !== T_OPEN_CURLY_BRACKET) {
186
                $error = 'There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration';
187
                $phpcsFile->addError($error, $next);
188
            }
189
        }
190
 
191
    }//end processMultiLineDeclaration()
192
 
193
 
194
}//end class
195
 
196
?>