|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHPCompatibility_Sniffs_PHP_NewClassesSniff. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category PHP |
|
8
|
|
|
* @package PHPCompatibility |
|
9
|
|
|
* @author Wim Godden <[email protected]> |
|
10
|
|
|
* @copyright 2013 Cu.be Solutions bvba |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* PHPCompatibility_Sniffs_PHP_NewClassesSniff. |
|
15
|
|
|
* |
|
16
|
|
|
* @category PHP |
|
17
|
|
|
* @package PHPCompatibility |
|
18
|
|
|
* @author Wim Godden <[email protected]> |
|
19
|
|
|
* @version 1.0.0 |
|
20
|
|
|
* @copyright 2013 Cu.be Solutions bvba |
|
21
|
|
|
*/ |
|
22
|
|
|
class PHPCompatibility_Sniffs_PHP_NewClassesSniff extends PHPCompatibility_Sniff |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* A list of new classes, not present in older versions. |
|
27
|
|
|
* |
|
28
|
|
|
* The array lists : version number with false (not present) or true (present). |
|
29
|
|
|
* If's sufficient to list the first version where the class appears. |
|
30
|
|
|
* |
|
31
|
|
|
* @var array(string => array(string => int|string|null)) |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $newClasses = array( |
|
34
|
|
|
'DateTime' => array( |
|
35
|
|
|
'5.1' => false, |
|
36
|
|
|
'5.2' => true |
|
37
|
|
|
), |
|
38
|
|
|
'DateTimeZone' => array( |
|
39
|
|
|
'5.1' => false, |
|
40
|
|
|
'5.2' => true |
|
41
|
|
|
), |
|
42
|
|
|
'RegexIterator' => array( |
|
43
|
|
|
'5.1' => false, |
|
44
|
|
|
'5.2' => true |
|
45
|
|
|
), |
|
46
|
|
|
'RecursiveRegexIterator' => array( |
|
47
|
|
|
'5.1' => false, |
|
48
|
|
|
'5.2' => true |
|
49
|
|
|
), |
|
50
|
|
|
|
|
51
|
|
|
'DateInterval' => array( |
|
52
|
|
|
'5.2' => false, |
|
53
|
|
|
'5.3' => true |
|
54
|
|
|
), |
|
55
|
|
|
'DatePeriod' => array( |
|
56
|
|
|
'5.2' => false, |
|
57
|
|
|
'5.3' => true |
|
58
|
|
|
), |
|
59
|
|
|
'Phar' => array( |
|
60
|
|
|
'5.2' => false, |
|
61
|
|
|
'5.3' => true |
|
62
|
|
|
), |
|
63
|
|
|
'PharData' => array( |
|
64
|
|
|
'5.2' => false, |
|
65
|
|
|
'5.3' => true |
|
66
|
|
|
), |
|
67
|
|
|
'PharException' => array( |
|
68
|
|
|
'5.2' => false, |
|
69
|
|
|
'5.3' => true |
|
70
|
|
|
), |
|
71
|
|
|
'PharFileInfo' => array( |
|
72
|
|
|
'5.2' => false, |
|
73
|
|
|
'5.3' => true |
|
74
|
|
|
), |
|
75
|
|
|
'FilesystemIterator' => array( |
|
76
|
|
|
'5.2' => false, |
|
77
|
|
|
'5.3' => true |
|
78
|
|
|
), |
|
79
|
|
|
'GlobIterator' => array( |
|
80
|
|
|
'5.2' => false, |
|
81
|
|
|
'5.3' => true |
|
82
|
|
|
), |
|
83
|
|
|
'MultipleIterator' => array( |
|
84
|
|
|
'5.2' => false, |
|
85
|
|
|
'5.3' => true |
|
86
|
|
|
), |
|
87
|
|
|
'RecursiveTreeIterator' => array( |
|
88
|
|
|
'5.2' => false, |
|
89
|
|
|
'5.3' => true |
|
90
|
|
|
), |
|
91
|
|
|
'SplDoublyLinkedList' => array( |
|
92
|
|
|
'5.2' => false, |
|
93
|
|
|
'5.3' => true |
|
94
|
|
|
), |
|
95
|
|
|
'SplFixedArray' => array( |
|
96
|
|
|
'5.2' => false, |
|
97
|
|
|
'5.3' => true |
|
98
|
|
|
), |
|
99
|
|
|
'SplHeap' => array( |
|
100
|
|
|
'5.2' => false, |
|
101
|
|
|
'5.3' => true |
|
102
|
|
|
), |
|
103
|
|
|
'SplMaxHeap' => array( |
|
104
|
|
|
'5.2' => false, |
|
105
|
|
|
'5.3' => true |
|
106
|
|
|
), |
|
107
|
|
|
'SplMinHeap' => array( |
|
108
|
|
|
'5.2' => false, |
|
109
|
|
|
'5.3' => true |
|
110
|
|
|
), |
|
111
|
|
|
'SplPriorityQueue' => array( |
|
112
|
|
|
'5.2' => false, |
|
113
|
|
|
'5.3' => true |
|
114
|
|
|
), |
|
115
|
|
|
'SplQueue' => array( |
|
116
|
|
|
'5.2' => false, |
|
117
|
|
|
'5.3' => true |
|
118
|
|
|
), |
|
119
|
|
|
'SplStack' => array( |
|
120
|
|
|
'5.2' => false, |
|
121
|
|
|
'5.3' => true |
|
122
|
|
|
), |
|
123
|
|
|
|
|
124
|
|
|
'CallbackFilterIterator' => array( |
|
125
|
|
|
'5.3' => false, |
|
126
|
|
|
'5.4' => true |
|
127
|
|
|
), |
|
128
|
|
|
'RecursiveCallbackFilterIterator' => array( |
|
129
|
|
|
'5.3' => false, |
|
130
|
|
|
'5.4' => true |
|
131
|
|
|
), |
|
132
|
|
|
'ReflectionZendExtension' => array( |
|
133
|
|
|
'5.3' => false, |
|
134
|
|
|
'5.4' => true |
|
135
|
|
|
), |
|
136
|
|
|
'JsonSerializable' => array( |
|
137
|
|
|
'5.3' => false, |
|
138
|
|
|
'5.4' => true |
|
139
|
|
|
), |
|
140
|
|
|
'SessionHandler' => array( |
|
141
|
|
|
'5.3' => false, |
|
142
|
|
|
'5.4' => true |
|
143
|
|
|
), |
|
144
|
|
|
'SNMP' => array( |
|
145
|
|
|
'5.3' => false, |
|
146
|
|
|
'5.4' => true |
|
147
|
|
|
), |
|
148
|
|
|
'Transliterator' => array( |
|
149
|
|
|
'5.3' => false, |
|
150
|
|
|
'5.4' => true |
|
151
|
|
|
), |
|
152
|
|
|
'Spoofchecker' => array( |
|
153
|
|
|
'5.3' => false, |
|
154
|
|
|
'5.4' => true |
|
155
|
|
|
), |
|
156
|
|
|
|
|
157
|
|
|
'CURLFile' => array( |
|
158
|
|
|
'5.4' => false, |
|
159
|
|
|
'5.5' => true |
|
160
|
|
|
), |
|
161
|
|
|
'DateTimeImmutable' => array( |
|
162
|
|
|
'5.4' => false, |
|
163
|
|
|
'5.5' => true |
|
164
|
|
|
), |
|
165
|
|
|
'IntlCalendar' => array( |
|
166
|
|
|
'5.4' => false, |
|
167
|
|
|
'5.5' => true |
|
168
|
|
|
), |
|
169
|
|
|
'IntlGregorianCalendar' => array( |
|
170
|
|
|
'5.4' => false, |
|
171
|
|
|
'5.5' => true |
|
172
|
|
|
), |
|
173
|
|
|
'IntlTimeZone' => array( |
|
174
|
|
|
'5.4' => false, |
|
175
|
|
|
'5.5' => true |
|
176
|
|
|
), |
|
177
|
|
|
'IntlBreakIterator' => array( |
|
178
|
|
|
'5.4' => false, |
|
179
|
|
|
'5.5' => true |
|
180
|
|
|
), |
|
181
|
|
|
'IntlRuleBasedBreakIterator' => array( |
|
182
|
|
|
'5.4' => false, |
|
183
|
|
|
'5.5' => true |
|
184
|
|
|
), |
|
185
|
|
|
'IntlCodePointBreakIterator' => array( |
|
186
|
|
|
'5.4' => false, |
|
187
|
|
|
'5.5' => true |
|
188
|
|
|
), |
|
189
|
|
|
|
|
190
|
|
|
); |
|
191
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* If true, an error will be thrown; otherwise a warning. |
|
195
|
|
|
* |
|
196
|
|
|
* @var bool |
|
197
|
|
|
*/ |
|
198
|
|
|
protected $error = false; |
|
199
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Returns an array of tokens this test wants to listen for. |
|
203
|
|
|
* |
|
204
|
|
|
* @return array |
|
205
|
|
|
*/ |
|
206
|
|
|
public function register() |
|
207
|
|
|
{ |
|
208
|
|
|
return array(T_NEW); |
|
209
|
|
|
|
|
210
|
|
|
}//end register() |
|
211
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Processes this test, when one of its tokens is encountered. |
|
215
|
|
|
* |
|
216
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
217
|
|
|
* @param int $stackPtr The position of the current token in |
|
218
|
|
|
* the stack passed in $tokens. |
|
219
|
|
|
* |
|
220
|
|
|
* @return void |
|
221
|
|
|
*/ |
|
222
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
223
|
|
|
{ |
|
224
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
225
|
|
|
if ( |
|
226
|
|
|
$tokens[$stackPtr + 2]['type'] == 'T_STRING' |
|
227
|
|
|
&& |
|
228
|
|
|
( |
|
229
|
|
|
$tokens[$stackPtr + 3]['type'] == 'T_OPEN_PARENTHESIS' |
|
230
|
|
|
|| |
|
231
|
|
|
( |
|
232
|
|
|
$tokens[$stackPtr + 3]['type'] == 'T_WHITESPACE' |
|
233
|
|
|
&& |
|
234
|
|
|
$tokens[$stackPtr + 4]['type'] == 'T_OPEN_PARENTHESIS' |
|
235
|
|
|
) |
|
236
|
|
|
|| |
|
237
|
|
|
$tokens[$stackPtr + 3]['type'] == 'T_SEMICOLON' |
|
238
|
|
|
) |
|
239
|
|
|
) { |
|
240
|
|
|
$className = $tokens[$stackPtr + 2]['content']; |
|
241
|
|
|
if (array_key_exists($className, $this->newClasses) === false) { |
|
242
|
|
|
return; |
|
243
|
|
|
} |
|
244
|
|
|
$this->addError($phpcsFile, $stackPtr, $className); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
|
|
248
|
|
|
|
|
249
|
|
|
}//end process() |
|
250
|
|
|
|
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Generates the error or wanrning for this sniff. |
|
254
|
|
|
* |
|
255
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
256
|
|
|
* @param int $stackPtr The position of the function |
|
257
|
|
|
* in the token array. |
|
258
|
|
|
* @param string $function The name of the function. |
|
|
|
|
|
|
259
|
|
|
* @param string $pattern The pattern used for the match. |
|
260
|
|
|
* |
|
261
|
|
|
* @return void |
|
262
|
|
|
*/ |
|
263
|
|
View Code Duplication |
protected function addError($phpcsFile, $stackPtr, $className, $pattern=null) |
|
|
|
|
|
|
264
|
|
|
{ |
|
265
|
|
|
if ($pattern === null) { |
|
266
|
|
|
$pattern = $className; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
$error = ''; |
|
270
|
|
|
|
|
271
|
|
|
$this->error = false; |
|
272
|
|
|
foreach ($this->newClasses[$pattern] as $version => $present) { |
|
273
|
|
|
if ($this->supportsBelow($version)) { |
|
274
|
|
|
if ($present === false) { |
|
275
|
|
|
$this->error = true; |
|
276
|
|
|
$error .= 'not present in PHP version ' . $version . ' or earlier'; |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
if (strlen($error) > 0) { |
|
281
|
|
|
$error = 'The built-in class ' . $className . ' is ' . $error; |
|
282
|
|
|
|
|
283
|
|
|
if ($this->error === true) { |
|
284
|
|
|
$phpcsFile->addError($error, $stackPtr); |
|
285
|
|
|
} else { |
|
286
|
|
|
$phpcsFile->addWarning($error, $stackPtr); |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
}//end addError() |
|
291
|
|
|
|
|
292
|
|
|
}//end class |
|
293
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.