| 1 |
lars |
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
|
|
|
13 |
* @version CVS: $Id: ForbiddenFunctionsSniff.php 290853 2009-11-17 03:17:17Z squiz $
|
|
|
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.1
|
|
|
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 |
$ignore = array(
|
|
|
82 |
T_DOUBLE_COLON,
|
|
|
83 |
T_OBJECT_OPERATOR,
|
|
|
84 |
T_FUNCTION,
|
|
|
85 |
T_CONST,
|
|
|
86 |
);
|
|
|
87 |
|
|
|
88 |
$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
|
|
|
89 |
if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
|
|
|
90 |
// Not a call to a PHP function.
|
|
|
91 |
return;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
$function = strtolower($tokens[$stackPtr]['content']);
|
|
|
95 |
|
|
|
96 |
if (in_array($function, array_keys($this->forbiddenFunctions)) === false) {
|
|
|
97 |
return;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
$error = "The use of function $function() is ";
|
|
|
101 |
if ($this->error === true) {
|
|
|
102 |
$error .= 'forbidden';
|
|
|
103 |
} else {
|
|
|
104 |
$error .= 'discouraged';
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
if ($this->forbiddenFunctions[$function] !== null) {
|
|
|
108 |
$error .= '; use '.$this->forbiddenFunctions[$function].'() instead';
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
if ($this->error === true) {
|
|
|
112 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
113 |
} else {
|
|
|
114 |
$phpcsFile->addWarning($error, $stackPtr);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
}//end process()
|
|
|
118 |
|
|
|
119 |
|
|
|
120 |
}//end class
|
|
|
121 |
|
|
|
122 |
?>
|