|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHPCompatibility_Sniffs_PHP_NewKeywordsSniff. |
|
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_NewKeywordsSniff extends PHPCompatibility_AbstractNewFeatureSniff |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* A list of new keywords, 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 last version which did not contain the keyword. |
|
30
|
|
|
* |
|
31
|
|
|
* Description will be used as part of the error message. |
|
32
|
|
|
* Condition is an array of valid scope conditions to check for. |
|
33
|
|
|
* If you need a condition of a different type, make sure to add the appropriate |
|
34
|
|
|
* logic for it as well as this will not resolve itself automatically. |
|
35
|
|
|
* |
|
36
|
|
|
* @var array(string => array(string => int|string|null)) |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $newKeywords = array( |
|
39
|
|
|
'T_HALT_COMPILER' => array( |
|
40
|
|
|
'5.0' => false, |
|
41
|
|
|
'5.1' => true, |
|
42
|
|
|
'description' => '"__halt_compiler" keyword' |
|
43
|
|
|
), |
|
44
|
|
|
'T_CONST' => array( |
|
45
|
|
|
'5.2' => false, |
|
46
|
|
|
'5.3' => true, |
|
47
|
|
|
'description' => '"const" keyword', |
|
48
|
|
|
'condition' => array(T_CLASS), // Keyword is only new when not in class context. |
|
49
|
|
|
), |
|
50
|
|
|
'T_CALLABLE' => array( |
|
51
|
|
|
'5.3' => false, |
|
52
|
|
|
'5.4' => true, |
|
53
|
|
|
'description' => '"callable" keyword', |
|
54
|
|
|
'content' => 'callable', |
|
55
|
|
|
), |
|
56
|
|
|
'T_DIR' => array( |
|
57
|
|
|
'5.2' => false, |
|
58
|
|
|
'5.3' => true, |
|
59
|
|
|
'description' => '__DIR__ magic constant', |
|
60
|
|
|
'content' => '__DIR__', |
|
61
|
|
|
), |
|
62
|
|
|
'T_GOTO' => array( |
|
63
|
|
|
'5.2' => false, |
|
64
|
|
|
'5.3' => true, |
|
65
|
|
|
'description' => '"goto" keyword', |
|
66
|
|
|
'content' => 'goto', |
|
67
|
|
|
), |
|
68
|
|
|
'T_INSTEADOF' => array( |
|
69
|
|
|
'5.3' => false, |
|
70
|
|
|
'5.4' => true, |
|
71
|
|
|
'description' => '"insteadof" keyword (for traits)', |
|
72
|
|
|
'content' => 'insteadof', |
|
73
|
|
|
), |
|
74
|
|
|
'T_NAMESPACE' => array( |
|
75
|
|
|
'5.2' => false, |
|
76
|
|
|
'5.3' => true, |
|
77
|
|
|
'description' => '"namespace" keyword', |
|
78
|
|
|
'content' => 'namespace', |
|
79
|
|
|
), |
|
80
|
|
|
'T_NS_C' => array( |
|
81
|
|
|
'5.2' => false, |
|
82
|
|
|
'5.3' => true, |
|
83
|
|
|
'description' => '__NAMESPACE__ magic constant', |
|
84
|
|
|
'content' => '__NAMESPACE__', |
|
85
|
|
|
), |
|
86
|
|
|
'T_USE' => array( |
|
87
|
|
|
'5.2' => false, |
|
88
|
|
|
'5.3' => true, |
|
89
|
|
|
'description' => '"use" keyword (for traits/namespaces/anonymous functions)' |
|
90
|
|
|
), |
|
91
|
|
|
'T_TRAIT' => array( |
|
92
|
|
|
'5.3' => false, |
|
93
|
|
|
'5.4' => true, |
|
94
|
|
|
'description' => '"trait" keyword', |
|
95
|
|
|
'content' => 'trait', |
|
96
|
|
|
), |
|
97
|
|
|
'T_TRAIT_C' => array( |
|
98
|
|
|
'5.3' => false, |
|
99
|
|
|
'5.4' => true, |
|
100
|
|
|
'description' => '__TRAIT__ magic constant', |
|
101
|
|
|
'content' => '__TRAIT__', |
|
102
|
|
|
), |
|
103
|
|
|
'T_YIELD' => array( |
|
104
|
|
|
'5.4' => false, |
|
105
|
|
|
'5.5' => true, |
|
106
|
|
|
'description' => '"yield" keyword (for generators)', |
|
107
|
|
|
'content' => 'yield', |
|
108
|
|
|
), |
|
109
|
|
|
'T_FINALLY' => array( |
|
110
|
|
|
'5.4' => false, |
|
111
|
|
|
'5.5' => true, |
|
112
|
|
|
'description' => '"finally" keyword (in exception handling)', |
|
113
|
|
|
'content' => 'finally', |
|
114
|
|
|
), |
|
115
|
|
|
'T_START_NOWDOC' => array( |
|
116
|
|
|
'5.2' => false, |
|
117
|
|
|
'5.3' => true, |
|
118
|
|
|
'description' => 'nowdoc functionality', |
|
119
|
|
|
), |
|
120
|
|
|
'T_END_NOWDOC' => array( |
|
121
|
|
|
'5.2' => false, |
|
122
|
|
|
'5.3' => true, |
|
123
|
|
|
'description' => 'nowdoc functionality', |
|
124
|
|
|
), |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Translation table for T_STRING tokens. |
|
129
|
|
|
* |
|
130
|
|
|
* Will be set up from the register() method. |
|
131
|
|
|
* |
|
132
|
|
|
* @var array(string => string) |
|
133
|
|
|
*/ |
|
134
|
|
|
protected $translateContentToToken = array(); |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Returns an array of tokens this test wants to listen for. |
|
139
|
|
|
* |
|
140
|
|
|
* @return array |
|
141
|
|
|
*/ |
|
142
|
|
|
public function register() |
|
143
|
|
|
{ |
|
144
|
|
|
$tokens = array(); |
|
145
|
|
|
$translate = array(); |
|
146
|
|
|
foreach ($this->newKeywords as $token => $versions) { |
|
147
|
|
|
if (defined($token)) { |
|
148
|
|
|
$tokens[] = constant($token); |
|
149
|
|
|
} |
|
150
|
|
|
if (isset($versions['content'])) { |
|
151
|
|
|
$translate[$versions['content']] = $token; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/* |
|
156
|
|
|
* Deal with tokens not recognized by the PHP version the sniffer is run |
|
157
|
|
|
* under and (not correctly) compensated for by PHPCS. |
|
158
|
|
|
*/ |
|
159
|
|
|
if (empty($translate) === false) { |
|
160
|
|
|
$this->translateContentToToken = $translate; |
|
161
|
|
|
$tokens[] = T_STRING; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return $tokens; |
|
165
|
|
|
|
|
166
|
|
|
}//end register() |
|
167
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Processes this test, when one of its tokens is encountered. |
|
171
|
|
|
* |
|
172
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
173
|
|
|
* @param int $stackPtr The position of the current token in |
|
174
|
|
|
* the stack passed in $tokens. |
|
175
|
|
|
* |
|
176
|
|
|
* @return void |
|
177
|
|
|
*/ |
|
178
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
179
|
|
|
{ |
|
180
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
181
|
|
|
$tokenType = $tokens[$stackPtr]['type']; |
|
182
|
|
|
|
|
183
|
|
|
// Translate T_STRING token if necessary. |
|
184
|
|
|
if ($tokens[$stackPtr]['type'] === 'T_STRING') { |
|
185
|
|
|
$content = $tokens[$stackPtr]['content']; |
|
186
|
|
|
if (isset($this->translateContentToToken[$content]) === false) { |
|
187
|
|
|
// Not one of the tokens we're looking for. |
|
188
|
|
|
return; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$tokenType = $this->translateContentToToken[$content]; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
if (isset($this->newKeywords[$tokenType]) === false) { |
|
195
|
|
|
return; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
$nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
|
199
|
|
|
$prevToken = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
200
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
// Skip attempts to use keywords as functions or class names - the former |
|
203
|
|
|
// will be reported by ForbiddenNamesAsInvokedFunctionsSniff, whilst the |
|
204
|
|
|
// latter will be (partially) reported by the ForbiddenNames sniff. |
|
205
|
|
|
// Either type will result in false-positives when targetting lower versions |
|
206
|
|
|
// of PHP where the name was not reserved, unless we explicitly check for |
|
207
|
|
|
// them. |
|
208
|
|
|
if ( |
|
209
|
|
|
($nextToken === false || $tokens[$nextToken]['type'] !== 'T_OPEN_PARENTHESIS') |
|
210
|
|
|
&& |
|
211
|
|
|
($prevToken === false || $tokens[$prevToken]['type'] !== 'T_CLASS' || $tokens[$prevToken]['type'] !== 'T_INTERFACE') |
|
212
|
|
|
) { |
|
213
|
|
|
// Skip based on token scope condition. |
|
214
|
|
|
if (isset($this->newKeywords[$tokenType]['condition'])) { |
|
215
|
|
|
$condition = $this->newKeywords[$tokenType]['condition']; |
|
216
|
|
|
if ($this->tokenHasScope($phpcsFile, $stackPtr, $condition) === true) { |
|
217
|
|
|
return; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
$itemInfo = array( |
|
222
|
|
|
'name' => $tokenType, |
|
223
|
|
|
); |
|
224
|
|
|
$this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
}//end process() |
|
228
|
|
|
|
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Get the relevant sub-array for a specific item from a multi-dimensional array. |
|
232
|
|
|
* |
|
233
|
|
|
* @param array $itemInfo Base information about the item. |
|
234
|
|
|
* |
|
235
|
|
|
* @return array Version and other information about the item. |
|
236
|
|
|
*/ |
|
237
|
|
|
public function getItemArray(array $itemInfo) |
|
238
|
|
|
{ |
|
239
|
|
|
return $this->newKeywords[$itemInfo['name']]; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Get an array of the non-PHP-version array keys used in a sub-array. |
|
245
|
|
|
* |
|
246
|
|
|
* @return array |
|
247
|
|
|
*/ |
|
248
|
|
|
protected function getNonVersionArrayKeys() |
|
249
|
|
|
{ |
|
250
|
|
|
return array( |
|
251
|
|
|
'description', |
|
252
|
|
|
'condition', |
|
253
|
|
|
'content', |
|
254
|
|
|
); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Retrieve the relevant detail (version) information for use in an error message. |
|
260
|
|
|
* |
|
261
|
|
|
* @param array $itemArray Version and other information about the item. |
|
262
|
|
|
* @param array $itemInfo Base information about the item. |
|
263
|
|
|
* |
|
264
|
|
|
* @return array |
|
265
|
|
|
*/ |
|
266
|
|
|
public function getErrorInfo(array $itemArray, array $itemInfo) |
|
267
|
|
|
{ |
|
268
|
|
|
$errorInfo = parent::getErrorInfo($itemArray, $itemInfo); |
|
269
|
|
|
$errorInfo['description'] = $itemArray['description']; |
|
270
|
|
|
|
|
271
|
|
|
return $errorInfo; |
|
272
|
|
|
|
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Allow for concrete child classes to filter the error data before it's passed to PHPCS. |
|
278
|
|
|
* |
|
279
|
|
|
* @param array $data The error data array which was created. |
|
280
|
|
|
* @param array $itemInfo Base information about the item this error message applied to. |
|
281
|
|
|
* @param array $errorInfo Detail information about an item this error message applied to. |
|
282
|
|
|
* |
|
283
|
|
|
* @return array |
|
284
|
|
|
*/ |
|
285
|
|
|
protected function filterErrorData(array $data, array $itemInfo, array $errorInfo) |
|
286
|
|
|
{ |
|
287
|
|
|
$data[0] = $errorInfo['description']; |
|
288
|
|
|
return $data; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
|
|
292
|
|
|
}//end class |
|
293
|
|
|
|
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.