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_ValidDefaultValueSniff.
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: ValidDefaultValueSniff.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_ValidDefaultValueSniff.
19
 *
20
 * A Sniff to ensure that parameters defined for a function that have a default
21
 * value come at the end of the function signature.
22
 *
23
 * @category  PHP
24
 * @package   PHP_CodeSniffer
25
 * @author    Greg Sherwood <gsherwood@squiz.net>
26
 * @author    Marc McIntyre <mmcintyre@squiz.net>
27
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
28
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
29
 * @version   Release: 1.2.0RC1
30
 * @link      http://pear.php.net/package/PHP_CodeSniffer
31
 */
32
class PEAR_Sniffs_Functions_ValidDefaultValueSniff implements PHP_CodeSniffer_Sniff
33
{
34
 
35
 
36
    /**
37
     * Returns an array of tokens this test wants to listen for.
38
     *
39
     * @return array
40
     */
41
    public function register()
42
    {
43
        return array(T_FUNCTION);
44
 
45
    }//end register()
46
 
47
 
48
    /**
49
     * Processes this test, when one of its tokens is encountered.
50
     *
51
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
52
     * @param int                  $stackPtr  The position of the current token in the
53
     *                                        stack passed in $tokens.
54
     *
55
     * @return void
56
     */
57
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
58
    {
59
        $tokens = $phpcsFile->getTokens();
60
 
61
        $argStart = $tokens[$stackPtr]['parenthesis_opener'];
62
        $argEnd   = $tokens[$stackPtr]['parenthesis_closer'];
63
 
64
        // Flag for when we have found a default in our arg list.
65
        // If there is a value without a default after this, it is an error.
66
        $defaultFound = false;
67
 
68
        $nextArg = $argStart;
69
        while (($nextArg = $phpcsFile->findNext(T_VARIABLE, ($nextArg + 1), $argEnd)) !== false) {
70
            $argHasDefault = self::_argHasDefault($phpcsFile, $nextArg);
71
            if (($argHasDefault === false) && ($defaultFound === true)) {
72
                $error  = 'Arguments with default values must be at the end';
73
                $error .= ' of the argument list';
74
                $phpcsFile->addError($error, $nextArg);
75
                return;
76
            }
77
 
78
            if ($argHasDefault === true) {
79
                $defaultFound = true;
80
            }
81
        }
82
 
83
    }//end process()
84
 
85
 
86
    /**
87
     * Returns true if the passed argument has a default value.
88
     *
89
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
90
     * @param int                  $argPtr    The position of the argument
91
     *                                        in the stack.
92
     *
93
     * @return bool
94
     */
95
    private static function _argHasDefault(PHP_CodeSniffer_File $phpcsFile, $argPtr)
96
    {
97
        $tokens    = $phpcsFile->getTokens();
98
        $nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($argPtr + 1), null, true);
99
        if ($tokens[$nextToken]['code'] !== T_EQUAL) {
100
            return false;
101
        }
102
 
103
        return true;
104
 
105
    }//end _argHasDefault()
106
 
107
 
108
}//end class
109
 
110
?>