~ubuntu-branches/ubuntu/saucy/php-codesniffer/saucy

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.1.0/CodeSniffer/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php

  • Committer: Bazaar Package Importer
  • Author(s): Jack Bates
  • Date: 2008-10-01 17:39:43 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20081001173943-2dy06n1e8zwyw1o8
Tags: 1.1.0-1
* New upstream release
* Acknowledge NMU, thanks Jan

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Squiz_Sniffs_Functions_FunctionDeclarationArgumentSpacingSniff.
 
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: FunctionDeclarationArgumentSpacingSniff.php,v 1.10 2007/10/19 07:05:35 squiz Exp $
 
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
15
 */
 
16
 
 
17
/**
 
18
 * Squiz_Sniffs_Functions_FunctionDeclarationArgumentSpacingSniff.
 
19
 *
 
20
 * Checks that arguments in function declarations are spaced correctly.
 
21
 *
 
22
 * @category  PHP
 
23
 * @package   PHP_CodeSniffer
 
24
 * @author    Greg Sherwood <gsherwood@squiz.net>
 
25
 * @author    Marc McIntyre <mmcintyre@squiz.net>
 
26
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
 
27
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 
28
 * @version   Release: 1.1.0
 
29
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
30
 */
 
31
class Squiz_Sniffs_Functions_FunctionDeclarationArgumentSpacingSniff implements PHP_CodeSniffer_Sniff
 
32
{
 
33
 
 
34
 
 
35
    /**
 
36
     * Returns an array of tokens this test wants to listen for.
 
37
     *
 
38
     * @return array
 
39
     */
 
40
    public function register()
 
41
    {
 
42
        return array(T_FUNCTION);
 
43
 
 
44
    }//end register()
 
45
 
 
46
 
 
47
    /**
 
48
     * Processes this test, when one of its tokens is encountered.
 
49
     *
 
50
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
 
51
     * @param int                  $stackPtr  The position of the current token in the
 
52
     *                                        stack passed in $tokens.
 
53
     *
 
54
     * @return void
 
55
     */
 
56
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 
57
    {
 
58
        $tokens = $phpcsFile->getTokens();
 
59
 
 
60
        $functionName = $phpcsFile->findNext(array(T_STRING), $stackPtr);
 
61
        $openBracket  = $tokens[$stackPtr]['parenthesis_opener'];
 
62
        $closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
 
63
 
 
64
        $nextParam = $openBracket;
 
65
        $params    = array();
 
66
        while (($nextParam = $phpcsFile->findNext(T_VARIABLE, ($nextParam + 1), $closeBracket)) !== false) {
 
67
 
 
68
            $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($nextParam + 1), ($closeBracket + 1), true);
 
69
            if ($nextToken === false) {
 
70
                break;
 
71
            }
 
72
 
 
73
            $nextCode = $tokens[$nextToken]['code'];
 
74
 
 
75
            if ($nextCode === T_EQUAL) {
 
76
                // Check parameter default spacing.
 
77
                if (($nextToken - $nextParam) > 1) {
 
78
                    $gap   = strlen($tokens[($nextParam + 1)]['content']);
 
79
                    $arg   = $tokens[$nextParam]['content'];
 
80
                    $error = "Expected 0 spaces between argument \"$arg\" and equals sign; $gap found";
 
81
                    $phpcsFile->addError($error, $nextToken);
 
82
                }
 
83
 
 
84
                if ($tokens[($nextToken + 1)]['code'] === T_WHITESPACE) {
 
85
                    $gap   = strlen($tokens[($nextToken + 1)]['content']);
 
86
                    $arg   = $tokens[$nextParam]['content'];
 
87
                    $error = "Expected 0 spaces between default value and equals sign for argument \"$arg\"; $gap found";
 
88
                    $phpcsFile->addError($error, $nextToken);
 
89
                }
 
90
            }
 
91
 
 
92
            // Find and check the comma (if there is one).
 
93
            $nextComma = $phpcsFile->findNext(T_COMMA, ($nextParam + 1), $closeBracket);
 
94
            if ($nextComma !== false) {
 
95
                // Comma found.
 
96
                if ($tokens[($nextComma - 1)]['code'] === T_WHITESPACE) {
 
97
                    $space = strlen($tokens[($nextComma - 1)]['content']);
 
98
                    $arg   = $tokens[$nextParam]['content'];
 
99
                    $error = "Expected 0 spaces between argument \"$arg\" and comma; $space found";
 
100
                    $phpcsFile->addError($error, $nextToken);
 
101
                }
 
102
            }
 
103
 
 
104
            // Take references into account when expecting the
 
105
            // location of whitespace.
 
106
            if ($phpcsFile->isReference(($nextParam - 1)) === true) {
 
107
                $whitespace = $tokens[($nextParam - 2)];
 
108
            } else {
 
109
                $whitespace = $tokens[($nextParam - 1)];
 
110
            }
 
111
 
 
112
            if (empty($params) === false) {
 
113
                // This is not the first argument in the function declaration.
 
114
                $arg = $tokens[$nextParam]['content'];
 
115
 
 
116
                if ($whitespace['code'] === T_WHITESPACE) {
 
117
                    $gap = strlen($whitespace['content']);
 
118
 
 
119
                    // Before we throw an error, make sure there is no type hint.
 
120
                    $comma     = $phpcsFile->findPrevious(T_COMMA, ($nextParam - 1));
 
121
                    $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($comma + 1), null, true);
 
122
                    if ($phpcsFile->isReference($nextToken) === true) {
 
123
                        $nextToken++;
 
124
                    }
 
125
 
 
126
                    if ($nextToken !== $nextParam) {
 
127
                        // There was a type hint, so check the spacing between
 
128
                        // the hint and the variable as well.
 
129
                        $hint = $tokens[$nextToken]['content'];
 
130
 
 
131
                        if ($gap !== 1) {
 
132
                            $error = "Expected 1 space between type hint and argument \"$arg\"; $gap found";
 
133
                            $phpcsFile->addError($error, $nextToken);
 
134
                        }
 
135
 
 
136
                        if ($tokens[($comma + 1)]['code'] !== T_WHITESPACE) {
 
137
                            $error = "Expected 1 space between comma and type hint \"$hint\"; 0 found";
 
138
                            $phpcsFile->addError($error, $nextToken);
 
139
                        } else {
 
140
                            $gap = strlen($tokens[($comma + 1)]['content']);
 
141
                            if ($gap !== 1) {
 
142
                                $error = "Expected 1 space between comma and type hint \"$hint\"; $gap found";
 
143
                                $phpcsFile->addError($error, $nextToken);
 
144
                            }
 
145
                        }
 
146
                    } else if ($gap !== 1) {
 
147
                        $error = "Expected 1 space between comma and argument \"$arg\"; $gap found";
 
148
                        $phpcsFile->addError($error, $nextToken);
 
149
                    }//end if
 
150
                } else {
 
151
                    $error = "Expected 1 space between comma and argument \"$arg\"; 0 found";
 
152
                    $phpcsFile->addError($error, $nextToken);
 
153
                }//end if
 
154
            } else {
 
155
                // First argument in function declaration.
 
156
                if ($whitespace['code'] === T_WHITESPACE) {
 
157
                    $gap = strlen($whitespace['content']);
 
158
                    $arg = $tokens[$nextParam]['content'];
 
159
 
 
160
                    // Before we throw an error, make sure there is no type hint.
 
161
                    $bracket   = $phpcsFile->findPrevious(T_OPEN_PARENTHESIS, ($nextParam - 1));
 
162
                    $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($bracket + 1), null, true);
 
163
                    if ($phpcsFile->isReference($nextToken) === true) {
 
164
                        $nextToken++;
 
165
                    }
 
166
 
 
167
                    if ($nextToken !== $nextParam) {
 
168
                        // There was a type hint, so check the spacing between
 
169
                        // the hint and the variable as well.
 
170
                        $hint = $tokens[$nextToken]['content'];
 
171
 
 
172
                        if ($gap !== 1) {
 
173
                            $error = "Expected 1 space between type hint and argument \"$arg\"; $gap found";
 
174
                            $phpcsFile->addError($error, $nextToken);
 
175
                        }
 
176
 
 
177
                        if ($tokens[($bracket + 1)]['code'] === T_WHITESPACE) {
 
178
                            $gap   = strlen($tokens[($bracket + 1)]['content']);
 
179
                            $error = "Expected 0 spaces between opening bracket and type hint \"$hint\"; $gap found";
 
180
                            $phpcsFile->addError($error, $nextToken);
 
181
                        }
 
182
                    } else {
 
183
                        $error = "Expected 0 spaces between opening bracket and argument \"$arg\"; $gap found";
 
184
                        $phpcsFile->addError($error, $nextToken);
 
185
                    }
 
186
                }//end if
 
187
            }//end if
 
188
 
 
189
            $params[] = $nextParam;
 
190
 
 
191
        }//end while
 
192
 
 
193
        if (empty($params) === true) {
 
194
            // There are no parameters for this function.
 
195
            if (($closeBracket - $openBracket) !== 1) {
 
196
                $space = strlen($tokens[($closeBracket - 1)]['content']);
 
197
                $error = "Expected 0 spaces between brackets of function declaration; $space found";
 
198
                $phpcsFile->addError($error, $stackPtr);
 
199
            }
 
200
        } else if ($tokens[($closeBracket - 1)]['code'] === T_WHITESPACE) {
 
201
            $lastParam = array_pop($params);
 
202
            $arg       = $tokens[$lastParam]['content'];
 
203
            $gap       = strlen($tokens[($closeBracket - 1)]['content']);
 
204
            $error     = "Expected 0 spaces between argument \"$arg\" and closing bracket; $gap found";
 
205
            $phpcsFile->addError($error, $closeBracket);
 
206
        }
 
207
 
 
208
    }//end process()
 
209
 
 
210
 
 
211
}//end class
 
212
 
 
213
?>