~ubuntu-branches/ubuntu/utopic/php-codesniffer/utopic-proposed

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.3.4/CodeSniffer/Standards/Generic/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2012-05-31 16:37:24 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20120531163724-u6aiaubu8ks5dh5z
Tags: 1.3.4-0.1
* Non-maintainer upload.
* New upstream release (Closes: #599617, #634825).
* Swtiched debian/copyright to format 1.0 (rewrite was needed anyway, as the
upstream license changed).
* Switched package to pkg-php-tools and debhelper 8 sequencer.
* Now running unit tests at build time (so depends on phpunit (>= 3.6)).
* Section is now PHP.
* Added missing Build-Depends-Indep: php-pear.
* Added missing ${misc:Depends}.
* Added Vcs fields.
* Added homepage field.
* Reviewed short and long description.
* Added dependency on php-timer.
* Standards-Version: is now 3.9.3 (lots of changes, see above...).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Generic_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-2011 Squiz Pty Ltd (ABN 77 084 670 600)
 
12
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 
13
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
14
 */
 
15
 
 
16
/**
 
17
 * Generic_Sniffs_Functions_FunctionCallArgumentSpacingSniff.
 
18
 *
 
19
 * Checks that calls to methods and functions are spaced correctly.
 
20
 *
 
21
 * @category  PHP
 
22
 * @package   PHP_CodeSniffer
 
23
 * @author    Greg Sherwood <gsherwood@squiz.net>
 
24
 * @author    Marc McIntyre <mmcintyre@squiz.net>
 
25
 * @copyright 2006-2011 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.3.4
 
28
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
29
 */
 
30
class Generic_Sniffs_Functions_FunctionCallArgumentSpacingSniff 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_STRING);
 
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 The file being scanned.
 
50
     * @param int                  $stackPtr  The position of the current token in the
 
51
     *                                        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
        // Skip tokens that are the names of functions or classes
 
60
        // within their definitions. For example:
 
61
        // function myFunction...
 
62
        // "myFunction" is T_STRING but we should skip because it is not a
 
63
        // function or method *call*.
 
64
        $functionName    = $stackPtr;
 
65
        $ignoreTokens    = PHP_CodeSniffer_Tokens::$emptyTokens;
 
66
        $ignoreTokens[]  = T_BITWISE_AND;
 
67
        $functionKeyword = $phpcsFile->findPrevious($ignoreTokens, ($stackPtr - 1), null, true);
 
68
        if ($tokens[$functionKeyword]['code'] === T_FUNCTION || $tokens[$functionKeyword]['code'] === T_CLASS) {
 
69
            return;
 
70
        }
 
71
 
 
72
        // If the next non-whitespace token after the function or method call
 
73
        // is not an opening parenthesis then it cant really be a *call*.
 
74
        $openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($functionName + 1), null, true);
 
75
        if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) {
 
76
            return;
 
77
        }
 
78
 
 
79
        $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
 
80
 
 
81
        $nextSeperator = $openBracket;
 
82
        while (($nextSeperator = $phpcsFile->findNext(array(T_COMMA, T_VARIABLE, T_CLOSURE), ($nextSeperator + 1), $closeBracket)) !== false) {
 
83
            if ($tokens[$nextSeperator]['code'] === T_CLOSURE) {
 
84
                $nextSeperator = $tokens[$nextSeperator]['scope_closer'];
 
85
                continue;
 
86
            }
 
87
 
 
88
            // Make sure the comma or variable belongs directly to this function call,
 
89
            // and is not inside a nested function call or array.
 
90
            $brackets    = $tokens[$nextSeperator]['nested_parenthesis'];
 
91
            $lastBracket = array_pop($brackets);
 
92
            if ($lastBracket !== $closeBracket) {
 
93
                continue;
 
94
            }
 
95
 
 
96
            if ($tokens[$nextSeperator]['code'] === T_COMMA) {
 
97
                if ($tokens[($nextSeperator - 1)]['code'] === T_WHITESPACE) {
 
98
                    $error = 'Space found before comma in function call';
 
99
                    $phpcsFile->addError($error, $stackPtr, 'SpaceBeforeComma');
 
100
                }
 
101
 
 
102
                if ($tokens[($nextSeperator + 1)]['code'] !== T_WHITESPACE) {
 
103
                    $error = 'No space found after comma in function call';
 
104
                    $phpcsFile->addError($error, $stackPtr, 'NoSpaceAfterComma');
 
105
                } else {
 
106
                    // If there is a newline in the space, then the must be formatting
 
107
                    // each argument on a newline, which is valid, so ignore it.
 
108
                    if (strpos($tokens[($nextSeperator + 1)]['content'], $phpcsFile->eolChar) === false) {
 
109
                        $space = strlen($tokens[($nextSeperator + 1)]['content']);
 
110
                        if ($space > 1) {
 
111
                            $error = 'Expected 1 space after comma in function call; %s found';
 
112
                            $data  = array($space);
 
113
                            $phpcsFile->addError($error, $stackPtr, 'TooMuchSpaceAfterComma', $data);
 
114
                        }
 
115
                    }
 
116
                }
 
117
            } else {
 
118
                // Token is a variable.
 
119
                $nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($nextSeperator + 1), $closeBracket, true);
 
120
                if ($nextToken !== false) {
 
121
                    if ($tokens[$nextToken]['code'] === T_EQUAL) {
 
122
                        if (($tokens[($nextToken - 1)]['code']) !== T_WHITESPACE) {
 
123
                            $error = 'Expected 1 space before = sign of default value';
 
124
                            $phpcsFile->addError($error, $stackPtr, 'NoSpaceBeforeEquals');
 
125
                        }
 
126
 
 
127
                        if ($tokens[($nextToken + 1)]['code'] !== T_WHITESPACE) {
 
128
                            $error = 'Expected 1 space after = sign of default value';
 
129
                            $phpcsFile->addError($error, $stackPtr, 'NoSpaceAfterEquals');
 
130
                        }
 
131
                    }
 
132
                }
 
133
            }//end if
 
134
        }//end while
 
135
 
 
136
    }//end process()
 
137
 
 
138
 
 
139
}//end class
 
140
 
 
141
?>