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_FunctionCallArgumentSpacingSniff.
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
34 aurelien 13
 * @version   CVS: $Id: FunctionCallArgumentSpacingSniff.php 34 2009-04-09 07:34:39Z aurelien $
5 aurelien 14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * PEAR_Sniffs_Functions_FunctionCallArgumentSpacingSniff.
19
 *
20
 * Checks that calls to methods and functions 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.2.0RC1
29
 * @link      http://pear.php.net/package/PHP_CodeSniffer
30
 */
31
class PEAR_Sniffs_Functions_FunctionCallArgumentSpacingSniff 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_STRING);
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
        // Skip tokens that are the names of functions or classes
61
        // within their definitions. For example:
62
        // function myFunction...
63
        // "myFunction" is T_STRING but we should skip because it is not a
64
        // function or method *call*.
65
        $functionName    = $stackPtr;
66
        $functionKeyword = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
67
        if ($tokens[$functionKeyword]['code'] === T_FUNCTION || $tokens[$functionKeyword]['code'] === T_CLASS) {
68
            return;
69
        }
70
 
71
        // If the next non-whitespace token after the function or method call
72
        // is not an opening parenthesis then it cant really be a *call*.
73
        $openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($functionName + 1), null, true);
74
        if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
75
            return;
76
        }
77
 
78
        $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
79
 
80
        $nextSeperator = $openBracket;
81
        while (($nextSeperator = $phpcsFile->findNext(array(T_COMMA, T_VARIABLE), ($nextSeperator + 1), $closeBracket)) !== false) {
82
            // Make sure the comma or variable belongs directly to this function call,
83
            // and is not inside a nested function call or array.
84
            $brackets    = $tokens[$nextSeperator]['nested_parenthesis'];
85
            $lastBracket = array_pop($brackets);
86
            if ($lastBracket !== $closeBracket) {
87
                continue;
88
            }
89
 
90
            if ($tokens[$nextSeperator]['code'] === T_COMMA) {
91
                if ($tokens[($nextSeperator - 1)]['code'] === T_WHITESPACE) {
92
                    $error = 'Space found before comma in function call';
93
                    $phpcsFile->addError($error, $stackPtr);
94
                }
95
 
96
                if ($tokens[($nextSeperator + 1)]['code'] !== T_WHITESPACE) {
97
                    $error = 'No space found after comma in function call';
98
                    $phpcsFile->addError($error, $stackPtr);
99
                } else {
100
                    // If there is a newline in the space, then the must be formatting
101
                    // each argument on a newline, which is valid, so ignore it.
102
                    if (strpos($tokens[($nextSeperator + 1)]['content'], $phpcsFile->eolChar) === false) {
103
                        $space = strlen($tokens[($nextSeperator + 1)]['content']);
104
                        if ($space > 1) {
105
                            $error  = 'Expected 1 space after comma in function call; ';
106
                            $error .= $space.' found';
107
                            $phpcsFile->addError($error, $stackPtr);
108
                        }
109
                    }
110
                }
111
            } else {
112
                // Token is a variable.
113
                $nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($nextSeperator + 1), $closeBracket, true);
114
                if ($nextToken !== false) {
115
                    if ($tokens[$nextToken]['code'] === T_EQUAL) {
116
                        if (($tokens[($nextToken - 1)]['code']) !== T_WHITESPACE) {
117
                            $error = 'Expected 1 space before = sign of default value';
118
                            $phpcsFile->addError($error, $stackPtr);
119
                        }
120
 
121
                        if ($tokens[($nextToken + 1)]['code'] !== T_WHITESPACE) {
122
                            $error = 'Expected 1 space after = sign of default value';
123
                            $phpcsFile->addError($error, $stackPtr);
124
                        }
125
                    }
126
                }
127
            }//end if
128
        }//end while
129
 
130
    }//end process()
131
 
132
 
133
}//end class
134
 
135
?>