Skip to content

Commit 5e133cc

Browse files
Nathaniel McHughproofek
Nathaniel McHugh
authored andcommitted
add new sniffs to detect for functions with parameters that have been removed and will no longer work
1 parent 2b5301e commit 5e133cc

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
/**
3+
* PHP54Compatibility_Sniffs_PHP_RemovedFunctionParametersSniff.
4+
*
5+
* This is based on Wim Godden's PHP53Compatibility code sniffs.
6+
* See [blog](http://techblog.wimgodden.be/tag/codesniffer) and
7+
* [github](https://github.com/wimg/PHP53Compat_CodeSniffer).
8+
*
9+
* PHP version 5.4
10+
*
11+
* @category PHP
12+
* @package PHP54Compatibility
13+
* @author Nat McHugh [email protected]
14+
* @copyright 2012 Nathaniel McHugh
15+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
16+
* @link https://github.com/proofek/PHP54Compatibility
17+
*/
18+
19+
20+
class PHP54Compatibility_Sniffs_PHP_RemovedFunctionParametersSniff implements PHP_CodeSniffer_Sniff
21+
{
22+
23+
/**
24+
* A list of removed functions with an assocuiated regular expression of parameters not allowed.
25+
*
26+
*
27+
* @var array(string => string)
28+
*/
29+
protected $forbiddenFunctionsParameters = array(
30+
'putenv' => '/^TZ=/',
31+
'hash_init' => '/salsa([1-2])0/',
32+
'hash_file' => '/salsa[1-2]0/',
33+
);
34+
35+
/**
36+
* A cache of removed function names, for faster lookups.
37+
*
38+
* @var array(string)
39+
*/
40+
protected $forbiddenFunctionNames = array();
41+
42+
43+
/**
44+
* If true, an error will be thrown; otherwise a warning.
45+
*
46+
* @var bool
47+
*/
48+
public $error = true;
49+
50+
51+
/**
52+
* Returns an array of tokens this test wants to listen for.
53+
*
54+
* @return array
55+
*/
56+
public function register()
57+
{
58+
$this->forbiddenFunctionNames = array_keys($this->forbiddenFunctionsParameters);
59+
return array(T_STRING);
60+
61+
}//end register()
62+
63+
64+
/**
65+
* Processes this test, when one of its tokens is encountered.
66+
*
67+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
68+
* @param int $stackPtr The position of the current token in
69+
* the stack passed in $tokens.
70+
*
71+
* @return void
72+
*/
73+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
74+
{
75+
$tokens = $phpcsFile->getTokens();
76+
77+
$ignore = array(
78+
T_DOUBLE_COLON,
79+
T_OBJECT_OPERATOR,
80+
T_FUNCTION,
81+
T_CONST,
82+
);
83+
84+
$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
85+
if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
86+
// Not a call to a PHP function.
87+
return;
88+
}
89+
90+
$function = strtolower($tokens[$stackPtr]['content']);
91+
$pattern = null;
92+
// if we don't have the name function bail early
93+
if (in_array($function, $this->forbiddenFunctionNames) === false) {
94+
return;
95+
}
96+
// we have a named function find the params regexes associated with it
97+
$associatedRegEx = $this->forbiddenFunctionsParameters[$function];
98+
$ptr = $stackPtr;
99+
$paramValues = array();
100+
do {
101+
++$ptr;
102+
// will get a whole bunch of other tonkens inc whitespace and open_parenthsis
103+
$paramValues[] = trim($tokens[$ptr]['content'], '"\'');
104+
} while (T_CLOSE_PARENTHESIS !== $tokens[$ptr]['code']);
105+
foreach ($paramValues as $paramValue) {
106+
if (preg_match($associatedRegEx, $paramValue)) {
107+
$this->addError($phpcsFile, $stackPtr, $function, $associatedRegEx);
108+
}
109+
}
110+
}//end process()
111+
112+
113+
/**
114+
* Generates the error or wanrning for this sniff.
115+
*
116+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
117+
* @param int $stackPtr The position of the forbidden function
118+
* in the token array.
119+
* @param string $function The name of the forbidden function.
120+
* @param string $pattern The pattern used for the match.
121+
*
122+
* @return void
123+
*/
124+
protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
125+
{
126+
$data = array($function, $pattern);
127+
$type = 'Removed';
128+
$error = '[PHP 5.4] The use of function %s() has been removed for parameters matching %s';
129+
130+
if ($pattern === null) {
131+
$pattern = $function;
132+
}
133+
134+
if ($this->error === true) {
135+
$phpcsFile->addError($error, $stackPtr, $type, $data);
136+
} else {
137+
$phpcsFile->addWarning($error, $stackPtr, $type, $data);
138+
}
139+
140+
}//end addError()
141+
142+
143+
}//end class
144+
145+
?>

tests/RemovedFunctionParameters.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
putenv('UNIQID=$uniqid');
3+
putenv( "TZ=Europe/London");
4+
hash_init ( 'salsa10');
5+
hash_init ( 'salsa20');
6+
hash_init ( 'md5');
7+
hash_file("salsa10", __FILE__);
8+
hash_file("md5", __FILE__);

0 commit comments

Comments
 (0)