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
 * Generic_Sniffs_PHP_ForbiddenFunctionsSniff.
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: ForbiddenFunctionsSniff.php 34 2009-04-09 07:34:39Z aurelien $
5 aurelien 14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * Generic_Sniffs_PHP_ForbiddenFunctionsSniff.
19
 *
20
 * Discourages the use of alias functions that are kept in PHP for compatibility
21
 * with older versions. Can be used to forbid the use of any function.
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 Generic_Sniffs_PHP_ForbiddenFunctionsSniff implements PHP_CodeSniffer_Sniff
33
{
34
 
35
    /**
36
     * A list of forbidden functions with their alternatives.
37
     *
38
     * The value is NULL if no alternative exists. IE, the
39
     * function should just not be used.
40
     *
41
     * @var array(string => string|null)
42
     */
43
    protected $forbiddenFunctions = array(
44
                                     'sizeof' => 'count',
45
                                     'delete' => 'unset',
46
                                    );
47
 
48
    /**
49
     * If true, an error will be thrown; otherwise a warning.
50
     *
51
     * @var bool
52
     */
53
    protected $error = true;
54
 
55
 
56
    /**
57
     * Returns an array of tokens this test wants to listen for.
58
     *
59
     * @return array
60
     */
61
    public function register()
62
    {
63
        return array(T_STRING);
64
 
65
    }//end register()
66
 
67
 
68
    /**
69
     * Processes this test, when one of its tokens is encountered.
70
     *
71
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
72
     * @param int                  $stackPtr  The position of the current token in the
73
     *                                        stack passed in $tokens.
74
     *
75
     * @return void
76
     */
77
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
78
    {
79
        $tokens = $phpcsFile->getTokens();
80
 
81
        $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
82
        if (in_array($tokens[$prevToken]['code'], array(T_DOUBLE_COLON, T_OBJECT_OPERATOR, T_FUNCTION)) === true) {
83
            // Not a call to a PHP function.
84
            return;
85
        }
86
 
87
        $function = strtolower($tokens[$stackPtr]['content']);
88
 
89
        if (in_array($function, array_keys($this->forbiddenFunctions)) === false) {
90
            return;
91
        }
92
 
93
        $error = "The use of function $function() is ";
94
        if ($this->error === true) {
95
            $error .= 'forbidden';
96
        } else {
97
            $error .= 'discouraged';
98
        }
99
 
100
        if ($this->forbiddenFunctions[$function] !== null) {
101
            $error .= '; use '.$this->forbiddenFunctions[$function].'() instead';
102
        }
103
 
104
        if ($this->error === true) {
105
            $phpcsFile->addError($error, $stackPtr);
106
        } else {
107
            $phpcsFile->addWarning($error, $stackPtr);
108
        }
109
 
110
    }//end process()
111
 
112
 
113
}//end class
114
 
115
?>