1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCompatibility_Sniffs_PHP_RemovedFunctionParametersSniff. |
4
|
|
|
* |
5
|
|
|
* @category PHP |
6
|
|
|
* @package PHPCompatibility |
7
|
|
|
* @author Wim Godden <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* PHPCompatibility_Sniffs_PHP_RemovedFunctionParametersSniff. |
12
|
|
|
* |
13
|
|
|
* @category PHP |
14
|
|
|
* @package PHPCompatibility |
15
|
|
|
* @author Wim Godden <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class PHPCompatibility_Sniffs_PHP_RemovedFunctionParametersSniff extends PHPCompatibility_Sniff |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* If true, forbidden functions will be considered regular expressions. |
22
|
|
|
* |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
protected $patternMatch = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* A list of Removed function parameters, not present in older versions. |
29
|
|
|
* |
30
|
|
|
* The array lists : version number with false (not present) or true (present). |
31
|
|
|
* The index is the location of the parameter in the parameter list, starting at 0 ! |
32
|
|
|
* If's sufficient to list the first version where the function appears. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $removedFunctionParameters = array( |
37
|
|
|
'mktime' => array( |
38
|
|
|
6 => array( |
39
|
|
|
'name' => 'is_dst', |
40
|
|
|
'5.1' => true, |
41
|
|
|
'7.0' => false |
42
|
|
|
), |
43
|
|
|
), |
44
|
|
|
'gmmktime' => array( |
45
|
|
|
6 => array( |
46
|
|
|
'name' => 'is_dst', |
47
|
|
|
'7.0' => false |
48
|
|
|
), |
49
|
|
|
), |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
private $removedFunctionParametersNames; |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns an array of tokens this test wants to listen for. |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function register() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
// Everyone has had a chance to figure out what forbidden functions |
68
|
|
|
// they want to check for, so now we can cache out the list. |
69
|
|
|
$this->removedFunctionParametersNames = array_keys($this->removedFunctionParameters); |
70
|
|
|
|
71
|
|
|
if ($this->patternMatch === true) { |
72
|
|
|
foreach ($this->removedFunctionParametersNames as $i => $name) { |
73
|
|
|
$this->removedFunctionParametersNames[$i] = '/'.$name.'/i'; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return array(T_STRING); |
78
|
|
|
}//end register() |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Processes this test, when one of its tokens is encountered. |
82
|
|
|
* |
83
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
84
|
|
|
* @param int $stackPtr The position of the current token in |
85
|
|
|
* the stack passed in $tokens. |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$tokens = $phpcsFile->getTokens(); |
92
|
|
|
|
93
|
|
|
$ignore = array( |
94
|
|
|
T_DOUBLE_COLON, |
95
|
|
|
T_OBJECT_OPERATOR, |
96
|
|
|
T_FUNCTION, |
97
|
|
|
T_CONST, |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
101
|
|
|
if (in_array($tokens[$prevToken]['code'], $ignore) === true) { |
102
|
|
|
// Not a call to a PHP function. |
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$function = strtolower($tokens[$stackPtr]['content']); |
107
|
|
|
|
108
|
|
|
if (in_array($function, $this->removedFunctionParametersNames) === false) { |
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$parameterCount = $this->getFunctionCallParameterCount($phpcsFile, $stackPtr); |
113
|
|
|
if ($parameterCount === 0) { |
114
|
|
|
return; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// If the parameter count returned > 0, we know there will be valid open parenthesis. |
118
|
|
|
$openParenthesis = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
119
|
|
|
$parameterOffsetFound = $parameterCount - 1; |
120
|
|
|
|
121
|
|
|
foreach($this->removedFunctionParameters[$function] as $offset => $parameterDetails) { |
122
|
|
|
if ($offset <= $parameterOffsetFound) { |
123
|
|
|
$this->addError($phpcsFile, $openParenthesis, $function, $offset); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
}//end process() |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Generates the error or warning for this sniff. |
132
|
|
|
* |
133
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
134
|
|
|
* @param int $stackPtr The position of the function |
135
|
|
|
* in the token array. |
136
|
|
|
* @param string $function The name of the function. |
137
|
|
|
* @param int $parameterLocation The parameter position within the function call. |
138
|
|
|
* |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
|
View Code Duplication |
protected function addError($phpcsFile, $stackPtr, $function, $parameterLocation) |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$error = ''; |
144
|
|
|
|
145
|
|
|
$isError = false; |
146
|
|
|
foreach ($this->removedFunctionParameters[$function][$parameterLocation] as $version => $present) { |
147
|
|
|
if ($version != 'name' && $this->supportsAbove($version)) { |
148
|
|
|
if ($present === false) { |
149
|
|
|
$isError = true; |
150
|
|
|
$error .= 'in PHP version ' . $version . ' or later'; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (strlen($error) > 0) { |
156
|
|
|
$error = 'The function ' . $function . ' does not have a parameter ' . $this->removedFunctionParameters[$function][$parameterLocation]['name'] . ' ' . $error; |
157
|
|
|
|
158
|
|
|
if ($isError === true) { |
159
|
|
|
$phpcsFile->addError($error, $stackPtr); |
160
|
|
|
} else { |
161
|
|
|
$phpcsFile->addWarning($error, $stackPtr); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
}//end addError() |
166
|
|
|
|
167
|
|
|
}//end class |
168
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.