1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCompatibility_Sniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 5.6 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHPCompatibility |
9
|
|
|
* @author Wim Godden <[email protected]> |
10
|
|
|
* @copyright 2014 Cu.be Solutions bvba |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* PHPCompatibility_Sniff. |
15
|
|
|
* |
16
|
|
|
* @category PHP |
17
|
|
|
* @package PHPCompatibility |
18
|
|
|
* @author Wim Godden <[email protected]> |
19
|
|
|
* @version 1.1.0 |
20
|
|
|
* @copyright 2014 Cu.be Solutions bvba |
21
|
|
|
*/ |
22
|
|
|
abstract class PHPCompatibility_Sniff implements PHP_CodeSniffer_Sniff |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/* The testVersion configuration variable may be in any of the following formats: |
26
|
|
|
* 1) Omitted/empty, in which case no version is specified. This effectively |
27
|
|
|
* disables all the checks provided by this standard. |
28
|
|
|
* 2) A single PHP version number, e.g. "5.4" in which case the standard checks that |
29
|
|
|
* the code will run on that version of PHP (no deprecated features or newer |
30
|
|
|
* features being used). |
31
|
|
|
* 3) A range, e.g. "5.0-5.5", in which case the standard checks the code will run |
32
|
|
|
* on all PHP versions in that range, and that it doesn't use any features that |
33
|
|
|
* were deprecated by the final version in the list, or which were not available |
34
|
|
|
* for the first version in the list. |
35
|
|
|
* PHP version numbers should always be in Major.Minor format. Both "5", "5.3.2" |
36
|
|
|
* would be treated as invalid, and ignored. |
37
|
|
|
* This standard doesn't support checking against PHP4, so the minimum version that |
38
|
|
|
* is recognised is "5.0". |
39
|
|
|
*/ |
40
|
|
|
|
41
|
|
|
private function getTestVersion() |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* var $arrTestVersions will hold an array containing min/max version of PHP |
45
|
|
|
* that we are checking against (see above). If only a single version |
46
|
|
|
* number is specified, then this is used as both the min and max. |
47
|
|
|
*/ |
48
|
|
|
static $arrTestVersions = array(); |
49
|
|
|
|
50
|
|
|
$testVersion = trim(PHP_CodeSniffer::getConfigData('testVersion')); |
51
|
|
|
|
52
|
|
|
if (!isset($arrTestVersions[$testVersion]) && !empty($testVersion)) { |
53
|
|
|
|
54
|
|
|
$arrTestVersions[$testVersion] = array(null, null); |
55
|
|
|
if (preg_match('/^\d+\.\d+$/', $testVersion)) { |
56
|
|
|
$arrTestVersions[$testVersion] = array($testVersion, $testVersion); |
57
|
|
|
} |
58
|
|
|
elseif (preg_match('/^(\d+\.\d+)\s*-\s*(\d+\.\d+)$/', $testVersion, |
59
|
|
|
$matches)) |
60
|
|
|
{ |
61
|
|
|
if (version_compare($matches[1], $matches[2], '>')) { |
62
|
|
|
trigger_error("Invalid range in testVersion setting: '" |
63
|
|
|
. $testVersion . "'", E_USER_WARNING); |
64
|
|
|
} |
65
|
|
|
else { |
66
|
|
|
$arrTestVersions[$testVersion] = array($matches[1], $matches[2]); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
elseif (!$testVersion == '') { |
70
|
|
|
trigger_error("Invalid testVersion setting: '" . $testVersion |
71
|
|
|
. "'", E_USER_WARNING); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (isset($arrTestVersions[$testVersion])) { |
76
|
|
|
return $arrTestVersions[$testVersion]; |
77
|
|
|
} |
78
|
|
|
else { |
79
|
|
|
return array(null, null); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function supportsAbove($phpVersion) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$testVersion = $this->getTestVersion(); |
86
|
|
|
$testVersion = $testVersion[1]; |
87
|
|
|
|
88
|
|
|
if (is_null($testVersion) |
89
|
|
|
|| version_compare($testVersion, $phpVersion) >= 0 |
90
|
|
|
) { |
91
|
|
|
return true; |
92
|
|
|
} else { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
}//end supportsAbove() |
96
|
|
|
|
97
|
|
View Code Duplication |
public function supportsBelow($phpVersion) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$testVersion = $this->getTestVersion(); |
100
|
|
|
$testVersion = $testVersion[0]; |
101
|
|
|
|
102
|
|
|
if (!is_null($testVersion) |
103
|
|
|
&& version_compare($testVersion, $phpVersion) <= 0 |
104
|
|
|
) { |
105
|
|
|
return true; |
106
|
|
|
} else { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
}//end supportsBelow() |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns the name(s) of the interface(s) that the specified class implements. |
113
|
|
|
* |
114
|
|
|
* Returns FALSE on error or if there are no implemented interface names. |
115
|
|
|
* |
116
|
|
|
* {@internal Duplicate of same method as introduced in PHPCS 2.7. |
117
|
|
|
* Once the minimum supported PHPCS version for this sniff library goes beyond |
118
|
|
|
* that, this method can be removed and call to it replaced with |
119
|
|
|
* `$phpcsFile->findImplementedInterfaceNames($stackPtr)` calls.}} |
120
|
|
|
* |
121
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
122
|
|
|
* @param int $stackPtr The position of the class token. |
123
|
|
|
* |
124
|
|
|
* @return array|false |
125
|
|
|
*/ |
126
|
|
|
public function findImplementedInterfaceNames($phpcsFile, $stackPtr) |
127
|
|
|
{ |
128
|
|
|
$tokens = $phpcsFile->getTokens(); |
129
|
|
|
|
130
|
|
|
// Check for the existence of the token. |
131
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($tokens[$stackPtr]['code'] !== T_CLASS) { |
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$classOpenerIndex = $tokens[$stackPtr]['scope_opener']; |
144
|
|
|
$implementsIndex = $phpcsFile->findNext(T_IMPLEMENTS, $stackPtr, $classOpenerIndex); |
145
|
|
|
if ($implementsIndex === false) { |
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$find = array( |
150
|
|
|
T_NS_SEPARATOR, |
151
|
|
|
T_STRING, |
152
|
|
|
T_WHITESPACE, |
153
|
|
|
T_COMMA, |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
$end = $phpcsFile->findNext($find, ($implementsIndex + 1), ($classOpenerIndex + 1), true); |
157
|
|
|
$name = $phpcsFile->getTokensAsString(($implementsIndex + 1), ($end - $implementsIndex - 1)); |
158
|
|
|
$name = trim($name); |
159
|
|
|
|
160
|
|
|
if ($name === '') { |
161
|
|
|
return false; |
162
|
|
|
} else { |
163
|
|
|
$names = explode(',', $name); |
164
|
|
|
$names = array_map('trim', $names); |
165
|
|
|
return $names; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
}//end findImplementedInterfaceNames() |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Checks if a function call has parameters. |
173
|
|
|
* |
174
|
|
|
* Expects to be passed the T_STRING stack pointer for the function call. |
175
|
|
|
* If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
176
|
|
|
* |
177
|
|
|
* @link https://github.com/wimg/PHPCompatibility/issues/120 |
178
|
|
|
* @link https://github.com/wimg/PHPCompatibility/issues/152 |
179
|
|
|
* |
180
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
181
|
|
|
* @param int $stackPtr The position of the function call token. |
182
|
|
|
* |
183
|
|
|
* @return bool |
184
|
|
|
*/ |
185
|
|
|
public function doesFunctionCallHaveParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
186
|
|
|
{ |
187
|
|
|
$tokens = $phpcsFile->getTokens(); |
188
|
|
|
|
189
|
|
|
// Check for the existence of the token. |
190
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
191
|
|
|
return false; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if ($tokens[$stackPtr]['code'] !== T_STRING) { |
195
|
|
|
return false; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
// Next non-empty token should be the open parenthesis. |
199
|
|
|
$openParenthesis = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
200
|
|
|
if ($openParenthesis === false || $tokens[$openParenthesis]['code'] !== T_OPEN_PARENTHESIS) { |
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
if (isset($tokens[$openParenthesis]['parenthesis_closer']) === false) { |
205
|
|
|
return false; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$closeParenthesis = $tokens[$openParenthesis]['parenthesis_closer']; |
209
|
|
|
$nextNonEmpty = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $openParenthesis + 1, $closeParenthesis + 1, true); |
210
|
|
|
|
211
|
|
|
if ($nextNonEmpty === $closeParenthesis) { |
212
|
|
|
// No parameters. |
213
|
|
|
return false; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return true; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Count the number of parameters a function call has been passed. |
222
|
|
|
* |
223
|
|
|
* Expects to be passed the T_STRING stack pointer for the function call. |
224
|
|
|
* If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
225
|
|
|
* |
226
|
|
|
* @link https://github.com/wimg/PHPCompatibility/issues/111 |
227
|
|
|
* @link https://github.com/wimg/PHPCompatibility/issues/114 |
228
|
|
|
* @link https://github.com/wimg/PHPCompatibility/issues/151 |
229
|
|
|
* |
230
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
231
|
|
|
* @param int $stackPtr The position of the function call token. |
232
|
|
|
* |
233
|
|
|
* @return int |
234
|
|
|
*/ |
235
|
|
|
public function getFunctionCallParameterCount(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
236
|
|
|
{ |
237
|
|
|
if ($this->doesFunctionCallHaveParameters($phpcsFile, $stackPtr) === false) { |
238
|
|
|
return 0; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return count($this->getFunctionCallParameters($phpcsFile, $stackPtr)); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Get information on all parameters passed to a function call. |
247
|
|
|
* |
248
|
|
|
* Expects to be passed the T_STRING stack pointer for the function call. |
249
|
|
|
* If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
250
|
|
|
* |
251
|
|
|
* Will return an multi-dimentional array with the start token pointer, end token |
252
|
|
|
* pointer and raw parameter value for all parameters. Index will be 1-based. |
253
|
|
|
* If no parameters are found, will return an empty array. |
254
|
|
|
* |
255
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
256
|
|
|
* @param int $stackPtr The position of the function call token. |
257
|
|
|
* |
258
|
|
|
* @return array |
259
|
|
|
*/ |
260
|
|
|
public function getFunctionCallParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
261
|
|
|
{ |
262
|
|
|
if ($this->doesFunctionCallHaveParameters($phpcsFile, $stackPtr) === false) { |
263
|
|
|
return array(); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
// Ok, we know we have a T_STRING with parameters and valid open & close parenthesis. |
267
|
|
|
$tokens = $phpcsFile->getTokens(); |
268
|
|
|
|
269
|
|
|
$openParenthesis = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
270
|
|
|
$closeParenthesis = $tokens[$openParenthesis]['parenthesis_closer']; |
271
|
|
|
|
272
|
|
|
// Which nesting level is the one we are interested in ? |
273
|
|
|
$nestedParenthesisCount = 1; |
274
|
|
|
if (isset($tokens[$openParenthesis]['nested_parenthesis'])) { |
275
|
|
|
$nestedParenthesisCount = count($tokens[$openParenthesis]['nested_parenthesis']) + 1; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
$parameters = array(); |
279
|
|
|
$nextComma = $openParenthesis; |
280
|
|
|
$paramStart = $openParenthesis + 1; |
281
|
|
|
$cnt = 1; |
282
|
|
|
while ($nextComma = $phpcsFile->findNext(array(T_COMMA, T_CLOSE_PARENTHESIS), $nextComma + 1, $closeParenthesis + 1)) { |
283
|
|
|
// Ignore comma's at a lower nesting level. |
284
|
|
|
if ( |
285
|
|
|
$tokens[$nextComma]['type'] == 'T_COMMA' |
286
|
|
|
&& |
287
|
|
|
isset($tokens[$nextComma]['nested_parenthesis']) |
288
|
|
|
&& |
289
|
|
|
count($tokens[$nextComma]['nested_parenthesis']) != $nestedParenthesisCount |
290
|
|
|
) { |
291
|
|
|
continue; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
// Ignore closing parenthesis if not 'ours'. |
295
|
|
|
if ($tokens[$nextComma]['type'] == 'T_CLOSE_PARENTHESIS' && $nextComma != $closeParenthesis) { |
296
|
|
|
continue; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
// Ok, we've reached the end of the parameter. |
300
|
|
|
$parameters[$cnt]['start'] = $paramStart; |
301
|
|
|
$parameters[$cnt]['end'] = $nextComma - 1; |
302
|
|
|
$parameters[$cnt]['raw'] = trim($phpcsFile->getTokensAsString($paramStart, ($nextComma - $paramStart))); |
303
|
|
|
|
304
|
|
|
// Prepare for the next parameter. |
305
|
|
|
$paramStart = $nextComma + 1; |
306
|
|
|
$cnt++; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return $parameters; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Get information on a specific parameter passed to a function call. |
315
|
|
|
* |
316
|
|
|
* Expects to be passed the T_STRING stack pointer for the function call. |
317
|
|
|
* If passed a T_STRING which is *not* a function call, the behaviour is unreliable. |
318
|
|
|
* |
319
|
|
|
* Will return a array with the start token pointer, end token pointer and the raw value |
320
|
|
|
* of the parameter at a specific offset. |
321
|
|
|
* If the specified parameter is not found, will return false. |
322
|
|
|
* |
323
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
324
|
|
|
* @param int $stackPtr The position of the function call token. |
325
|
|
|
* @param int $paramOffset The 1-based index position of the parameter to retrieve. |
326
|
|
|
* |
327
|
|
|
* @return array|false |
328
|
|
|
*/ |
329
|
|
|
public function getFunctionCallParameter(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $paramOffset) |
330
|
|
|
{ |
331
|
|
|
$parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr); |
332
|
|
|
|
333
|
|
|
if (isset($parameters[$paramOffset]) === false) { |
334
|
|
|
return false; |
335
|
|
|
} |
336
|
|
|
else { |
337
|
|
|
return $parameters[$paramOffset]; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Verify whether a token is within a class scope. |
344
|
|
|
* |
345
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
346
|
|
|
* @param int $stackPtr The position of the token. |
347
|
|
|
* @param bool $strict Whether to strictly check for the T_CLASS |
348
|
|
|
* scope or also accept interfaces and traits |
349
|
|
|
* as scope. |
350
|
|
|
* |
351
|
|
|
* @return bool True if within class scope, false otherwise. |
352
|
|
|
*/ |
353
|
|
|
public function inClassScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $strict = true) |
354
|
|
|
{ |
355
|
|
|
$tokens = $phpcsFile->getTokens(); |
356
|
|
|
|
357
|
|
|
// Check for the existence of the token. |
358
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
359
|
|
|
return false; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
// No conditions = no scope. |
363
|
|
|
if (empty($tokens[$stackPtr]['conditions'])) { |
364
|
|
|
return false; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
$validScope = array(T_CLASS); |
368
|
|
|
if ($strict === false) { |
369
|
|
|
$validScope[] = T_INTERFACE; |
370
|
|
|
$validScope[] = T_TRAIT; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
// Check for class scope. |
374
|
|
|
foreach ($tokens[$stackPtr]['conditions'] as $pointer => $type) { |
375
|
|
|
if (in_array($type, $validScope, true)) { |
376
|
|
|
return true; |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
return false; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Returns the fully qualified class name for a new class instantiation. |
386
|
|
|
* |
387
|
|
|
* Returns an empty string if the class name could not be reliably inferred. |
388
|
|
|
* |
389
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
390
|
|
|
* @param int $stackPtr The position of a T_NEW token. |
391
|
|
|
* |
392
|
|
|
* @return string |
393
|
|
|
*/ |
394
|
|
|
public function getFQClassNameFromNewToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
395
|
|
|
{ |
396
|
|
|
$tokens = $phpcsFile->getTokens(); |
397
|
|
|
|
398
|
|
|
// Check for the existence of the token. |
399
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
400
|
|
|
return ''; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
if ($tokens[$stackPtr]['code'] !== T_NEW) { |
404
|
|
|
return ''; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
$find = array( |
408
|
|
|
T_NS_SEPARATOR, |
409
|
|
|
T_STRING, |
410
|
|
|
T_NAMESPACE, |
411
|
|
|
T_WHITESPACE, |
412
|
|
|
); |
413
|
|
|
|
414
|
|
|
$start = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
415
|
|
|
// Bow out if the next token is a variable as we don't know where it was defined. |
416
|
|
|
if ($tokens[$start]['code'] === T_VARIABLE) { |
417
|
|
|
return ''; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
$end = $phpcsFile->findNext($find, ($start + 1), null, true, null, true); |
421
|
|
|
$className = $phpcsFile->getTokensAsString($start, ($end - $start)); |
422
|
|
|
$className = trim($className); |
423
|
|
|
|
424
|
|
|
return $this->getFQName($phpcsFile, $stackPtr, $className); |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Returns the fully qualified name of the class that the specified class extends. |
430
|
|
|
* |
431
|
|
|
* Returns an empty string if the class does not extend another class or if |
432
|
|
|
* the class name could not be reliably inferred. |
433
|
|
|
* |
434
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
435
|
|
|
* @param int $stackPtr The position of a T_CLASS token. |
436
|
|
|
* |
437
|
|
|
* @return string |
438
|
|
|
*/ |
439
|
|
|
public function getFQExtendedClassName(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
440
|
|
|
{ |
441
|
|
|
$tokens = $phpcsFile->getTokens(); |
442
|
|
|
|
443
|
|
|
// Check for the existence of the token. |
444
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
445
|
|
|
return ''; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
if ($tokens[$stackPtr]['code'] !== T_CLASS) { |
449
|
|
|
return ''; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
$extends = $phpcsFile->findExtendedClassName($stackPtr); |
453
|
|
|
if (empty($extends) || is_string($extends) === false) { |
454
|
|
|
return ''; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
return $this->getFQName($phpcsFile, $stackPtr, $extends); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* Returns the class name for the static usage of a class. |
463
|
|
|
* This can be a call to a method, the use of a property or constant. |
464
|
|
|
* |
465
|
|
|
* Returns an empty string if the class name could not be reliably inferred. |
466
|
|
|
* |
467
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
468
|
|
|
* @param int $stackPtr The position of a T_NEW token. |
469
|
|
|
* |
470
|
|
|
* @return string |
471
|
|
|
*/ |
472
|
|
|
public function getFQClassNameFromDoubleColonToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
473
|
|
|
{ |
474
|
|
|
$tokens = $phpcsFile->getTokens(); |
475
|
|
|
|
476
|
|
|
// Check for the existence of the token. |
477
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
478
|
|
|
return ''; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
if ($tokens[$stackPtr]['code'] !== T_DOUBLE_COLON) { |
482
|
|
|
return ''; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
// Nothing to do if previous token is a variable as we don't know where it was defined. |
486
|
|
|
if ($tokens[$stackPtr - 1]['code'] === T_VARIABLE) { |
487
|
|
|
return ''; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
// Nothing to do if 'parent' or 'static' as we don't know how far the class tree extends. |
491
|
|
|
if (in_array($tokens[$stackPtr - 1]['code'], array(T_PARENT, T_STATIC), true)) { |
492
|
|
|
return ''; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
// Get the classname from the class declaration if self is used. |
496
|
|
|
if ($tokens[$stackPtr - 1]['code'] === T_SELF) { |
497
|
|
|
$classDeclarationPtr = $phpcsFile->findPrevious(T_CLASS, $stackPtr - 1); |
498
|
|
|
if ($classDeclarationPtr === false) { |
499
|
|
|
return ''; |
500
|
|
|
} |
501
|
|
|
$className = $phpcsFile->getDeclarationName($classDeclarationPtr); |
502
|
|
|
return $this->getFQName($phpcsFile, $classDeclarationPtr, $className); |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
$find = array( |
506
|
|
|
T_NS_SEPARATOR, |
507
|
|
|
T_STRING, |
508
|
|
|
T_NAMESPACE, |
509
|
|
|
T_WHITESPACE, |
510
|
|
|
); |
511
|
|
|
|
512
|
|
|
$start = ($phpcsFile->findPrevious($find, $stackPtr - 1, null, true, null, true) + 1); |
513
|
|
|
$className = $phpcsFile->getTokensAsString($start, ($stackPtr - $start)); |
514
|
|
|
$className = trim($className); |
515
|
|
|
|
516
|
|
|
return $this->getFQName($phpcsFile, $stackPtr, $className); |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* Get the Fully Qualified name for a class/function/constant etc. |
522
|
|
|
* |
523
|
|
|
* Checks if a class/function/constant name is already fully qualified and |
524
|
|
|
* if not, enrich it with the relevant namespace information. |
525
|
|
|
* |
526
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
527
|
|
|
* @param int $stackPtr The position of the token. |
528
|
|
|
* @param string $name The class / function / constant name. |
529
|
|
|
* |
530
|
|
|
* @return string |
531
|
|
|
*/ |
532
|
|
|
public function getFQName(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $name) |
533
|
|
|
{ |
534
|
|
|
if (strpos($name, '\\' ) === 0) { |
535
|
|
|
// Already fully qualified. |
536
|
|
|
return $name; |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
// Remove the namespace keyword if used. |
540
|
|
|
if (strpos($name, 'namespace\\') === 0) { |
541
|
|
|
$name = substr($name, 10); |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
$namespace = $this->determineNamespace($phpcsFile, $stackPtr); |
545
|
|
|
|
546
|
|
|
if ($namespace === '') { |
547
|
|
|
return '\\' . $name; |
548
|
|
|
} |
549
|
|
|
else { |
550
|
|
|
return '\\' . $namespace . '\\' . $name; |
551
|
|
|
} |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
|
555
|
|
|
/** |
556
|
|
|
* Is the class/function/constant name namespaced or global ? |
557
|
|
|
* |
558
|
|
|
* @param string $FQName Fully Qualified name of a class, function etc. |
559
|
|
|
* I.e. should always start with a `\` ! |
560
|
|
|
* |
561
|
|
|
* @return bool True if namespaced, false if global. |
562
|
|
|
*/ |
563
|
|
|
public function isNamespaced($FQName) { |
564
|
|
|
if (strpos($FQName, '\\') !== 0) { |
565
|
|
|
throw new PHP_CodeSniffer_Exception('$FQName must be a fully qualified name'); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
return (strpos(substr($FQName, 1), '\\') !== false); |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
|
572
|
|
|
/** |
573
|
|
|
* Determine the namespace name an arbitrary token lives in. |
574
|
|
|
* |
575
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
576
|
|
|
* @param int $stackPtr The token position for which to determine the namespace. |
577
|
|
|
* |
578
|
|
|
* @return string Namespace name or empty string if it couldn't be determined or no namespace applies. |
579
|
|
|
*/ |
580
|
|
|
public function determineNamespace(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
581
|
|
|
{ |
582
|
|
|
$tokens = $phpcsFile->getTokens(); |
583
|
|
|
|
584
|
|
|
// Check for the existence of the token. |
585
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
586
|
|
|
return ''; |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
// Check for scoped namespace {}. |
590
|
|
|
if (empty($tokens[$stackPtr]['conditions']) === false) { |
591
|
|
|
foreach ($tokens[$stackPtr]['conditions'] as $pointer => $type) { |
592
|
|
|
if ($type === T_NAMESPACE) { |
593
|
|
|
$namespace = $this->getDeclaredNamespaceName($phpcsFile, $pointer); |
594
|
|
|
if ($namespace !== false) { |
595
|
|
|
return $namespace; |
596
|
|
|
} |
597
|
|
|
} |
598
|
|
|
break; // We only need to check the highest level condition. |
599
|
|
|
} |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
/* |
603
|
|
|
* Not in a scoped namespace, so let's see if we can find a non-scoped namespace instead. |
604
|
|
|
* Keeping in mind that: |
605
|
|
|
* - there can be multiple non-scoped namespaces in a file (bad practice, but it happens). |
606
|
|
|
* - the namespace keyword can also be used as part of a function/method call and such. |
607
|
|
|
* - that a non-named namespace resolves to the global namespace. |
608
|
|
|
*/ |
609
|
|
|
$previousNSToken = $stackPtr; |
610
|
|
|
$namespace = false; |
611
|
|
|
do { |
612
|
|
|
$previousNSToken = $phpcsFile->findPrevious(T_NAMESPACE, $previousNSToken -1); |
613
|
|
|
|
614
|
|
|
// Stop if we encounter a scoped namespace declaration as we already know we're not in one. |
615
|
|
|
if (empty($tokens[$previousNSToken]['scope_condition']) === false && $tokens[$previousNSToken]['scope_condition'] = $previousNSToken) { |
616
|
|
|
break; |
617
|
|
|
} |
618
|
|
|
$namespace = $this->getDeclaredNamespaceName($phpcsFile, $previousNSToken); |
619
|
|
|
|
620
|
|
|
} while ($namespace === false && $previousNSToken !== false); |
621
|
|
|
|
622
|
|
|
// If we still haven't got a namespace, return an empty string. |
623
|
|
|
if ($namespace === false) { |
624
|
|
|
return ''; |
625
|
|
|
} |
626
|
|
|
else { |
627
|
|
|
return $namespace; |
628
|
|
|
} |
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
/** |
632
|
|
|
* Get the complete namespace name for a namespace declaration. |
633
|
|
|
* |
634
|
|
|
* For hierarchical namespaces, the name will be composed of several tokens, |
635
|
|
|
* i.e. MyProject\Sub\Level which will be returned together as one string. |
636
|
|
|
* |
637
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
638
|
|
|
* @param int|bool $stackPtr The position of a T_NAMESPACE token. |
639
|
|
|
* |
640
|
|
|
* @return string|false Namespace name or false if not a namespace declaration. |
641
|
|
|
* Namespace name can be an empty string for global namespace declaration. |
642
|
|
|
*/ |
643
|
|
|
public function getDeclaredNamespaceName(PHP_CodeSniffer_File $phpcsFile, $stackPtr ) |
644
|
|
|
{ |
645
|
|
|
$tokens = $phpcsFile->getTokens(); |
646
|
|
|
|
647
|
|
|
// Check for the existence of the token. |
648
|
|
|
if ($stackPtr === false || isset($tokens[$stackPtr]) === false) { |
649
|
|
|
return false; |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
if ($tokens[$stackPtr]['code'] !== T_NAMESPACE) { |
653
|
|
|
return false; |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
if ($tokens[$stackPtr + 1]['code'] === T_NS_SEPARATOR) { |
657
|
|
|
// Not a namespace declaration, but use of, i.e. namespace\someFunction(); |
658
|
|
|
return false; |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
$nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true); |
662
|
|
|
if ($tokens[$nextToken]['code'] === T_OPEN_CURLY_BRACKET) { |
663
|
|
|
// Declaration for global namespace when using multiple namespaces in a file. |
664
|
|
|
// I.e.: namespace {} |
665
|
|
|
return ''; |
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
// Ok, this should be a namespace declaration, so get all the parts together. |
669
|
|
|
$validTokens = array( |
670
|
|
|
T_STRING, |
671
|
|
|
T_NS_SEPARATOR, |
672
|
|
|
T_WHITESPACE, |
673
|
|
|
); |
674
|
|
|
|
675
|
|
|
$namespaceName = ''; |
676
|
|
|
while(in_array($tokens[$nextToken]['code'], $validTokens, true) === true) { |
677
|
|
|
$namespaceName .= trim($tokens[$nextToken]['content']); |
678
|
|
|
$nextToken++; |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
return $namespaceName; |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
|
685
|
|
|
/** |
686
|
|
|
* Returns the method parameters for the specified T_FUNCTION token. |
687
|
|
|
* |
688
|
|
|
* Each parameter is in the following format: |
689
|
|
|
* |
690
|
|
|
* <code> |
691
|
|
|
* 0 => array( |
692
|
|
|
* 'name' => '$var', // The variable name. |
693
|
|
|
* 'pass_by_reference' => false, // Passed by reference. |
694
|
|
|
* 'type_hint' => string, // Type hint for array or custom type |
695
|
|
|
* ) |
696
|
|
|
* </code> |
697
|
|
|
* |
698
|
|
|
* Parameters with default values have an additional array index of |
699
|
|
|
* 'default' with the value of the default as a string. |
700
|
|
|
* |
701
|
|
|
* {@internal Duplicate of same method as contained in the `PHP_CodeSniffer_File` |
702
|
|
|
* class, but with some improvements which were only introduced in PHPCS 2.7. |
703
|
|
|
* Once the minimum supported PHPCS version for this sniff library goes beyond |
704
|
|
|
* that, this method can be removed and calls to it replaced with |
705
|
|
|
* `$phpcsFile->getMethodParameters($stackPtr)` calls.}} |
706
|
|
|
* |
707
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile Instance of phpcsFile. |
708
|
|
|
* @param int $stackPtr The position in the stack of the T_FUNCTION token |
709
|
|
|
* to acquire the parameters for. |
710
|
|
|
* |
711
|
|
|
* @return array |
712
|
|
|
* @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of |
713
|
|
|
* type T_FUNCTION. |
714
|
|
|
*/ |
715
|
|
|
public function getMethodParameters(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
716
|
|
|
{ |
717
|
|
|
$tokens = $phpcsFile->getTokens(); |
718
|
|
|
|
719
|
|
|
// Check for the existence of the token. |
720
|
|
|
if (isset($tokens[$stackPtr]) === false) { |
721
|
|
|
return false; |
722
|
|
|
} |
723
|
|
|
|
724
|
|
|
if ($tokens[$stackPtr]['code'] !== T_FUNCTION) { |
725
|
|
|
throw new PHP_CodeSniffer_Exception('$stackPtr must be of type T_FUNCTION'); |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
$opener = $tokens[$stackPtr]['parenthesis_opener']; |
729
|
|
|
$closer = $tokens[$stackPtr]['parenthesis_closer']; |
730
|
|
|
|
731
|
|
|
$vars = array(); |
732
|
|
|
$currVar = null; |
733
|
|
|
$paramStart = ($opener + 1); |
734
|
|
|
$defaultStart = null; |
735
|
|
|
$paramCount = 0; |
736
|
|
|
$passByReference = false; |
737
|
|
|
$variableLength = false; |
738
|
|
|
$typeHint = ''; |
739
|
|
|
|
740
|
|
|
for ($i = $paramStart; $i <= $closer; $i++) { |
741
|
|
|
// Check to see if this token has a parenthesis or bracket opener. If it does |
742
|
|
|
// it's likely to be an array which might have arguments in it. This |
743
|
|
|
// could cause problems in our parsing below, so lets just skip to the |
744
|
|
|
// end of it. |
745
|
|
View Code Duplication |
if (isset($tokens[$i]['parenthesis_opener']) === true) { |
|
|
|
|
746
|
|
|
// Don't do this if it's the close parenthesis for the method. |
747
|
|
|
if ($i !== $tokens[$i]['parenthesis_closer']) { |
748
|
|
|
$i = ($tokens[$i]['parenthesis_closer'] + 1); |
749
|
|
|
} |
750
|
|
|
} |
751
|
|
|
|
752
|
|
View Code Duplication |
if (isset($tokens[$i]['bracket_opener']) === true) { |
|
|
|
|
753
|
|
|
// Don't do this if it's the close parenthesis for the method. |
754
|
|
|
if ($i !== $tokens[$i]['bracket_closer']) { |
755
|
|
|
$i = ($tokens[$i]['bracket_closer'] + 1); |
756
|
|
|
} |
757
|
|
|
} |
758
|
|
|
|
759
|
|
|
switch ($tokens[$i]['code']) { |
760
|
|
|
case T_BITWISE_AND: |
761
|
|
|
$passByReference = true; |
762
|
|
|
break; |
763
|
|
|
case T_VARIABLE: |
764
|
|
|
$currVar = $i; |
765
|
|
|
break; |
766
|
|
|
case T_ELLIPSIS: |
767
|
|
|
$variableLength = true; |
768
|
|
|
break; |
769
|
|
|
case T_ARRAY_HINT: |
770
|
|
|
case T_CALLABLE: |
771
|
|
|
$typeHint = $tokens[$i]['content']; |
772
|
|
|
break; |
773
|
|
|
case T_SELF: |
774
|
|
|
case T_PARENT: |
775
|
|
|
case T_STATIC: |
776
|
|
|
// Self is valid, the others invalid, but were probably intended as type hints. |
777
|
|
|
if (isset($defaultStart) === false) { |
778
|
|
|
$typeHint = $tokens[$i]['content']; |
779
|
|
|
} |
780
|
|
|
break; |
781
|
|
|
case T_STRING: |
782
|
|
|
// This is a string, so it may be a type hint, but it could |
783
|
|
|
// also be a constant used as a default value. |
784
|
|
|
$prevComma = false; |
785
|
|
View Code Duplication |
for ($t = $i; $t >= $opener; $t--) { |
|
|
|
|
786
|
|
|
if ($tokens[$t]['code'] === T_COMMA) { |
787
|
|
|
$prevComma = $t; |
788
|
|
|
break; |
789
|
|
|
} |
790
|
|
|
} |
791
|
|
|
|
792
|
|
|
if ($prevComma !== false) { |
793
|
|
|
$nextEquals = false; |
794
|
|
View Code Duplication |
for ($t = $prevComma; $t < $i; $t++) { |
|
|
|
|
795
|
|
|
if ($tokens[$t]['code'] === T_EQUAL) { |
796
|
|
|
$nextEquals = $t; |
797
|
|
|
break; |
798
|
|
|
} |
799
|
|
|
} |
800
|
|
|
|
801
|
|
|
if ($nextEquals !== false) { |
802
|
|
|
break; |
803
|
|
|
} |
804
|
|
|
} |
805
|
|
|
|
806
|
|
|
if ($defaultStart === null) { |
807
|
|
|
$typeHint .= $tokens[$i]['content']; |
808
|
|
|
} |
809
|
|
|
break; |
810
|
|
|
case T_NS_SEPARATOR: |
811
|
|
|
// Part of a type hint or default value. |
812
|
|
|
if ($defaultStart === null) { |
813
|
|
|
$typeHint .= $tokens[$i]['content']; |
814
|
|
|
} |
815
|
|
|
break; |
816
|
|
|
case T_CLOSE_PARENTHESIS: |
817
|
|
|
case T_COMMA: |
818
|
|
|
// If it's null, then there must be no parameters for this |
819
|
|
|
// method. |
820
|
|
|
if ($currVar === null) { |
821
|
|
|
continue; |
822
|
|
|
} |
823
|
|
|
|
824
|
|
|
$vars[$paramCount] = array(); |
825
|
|
|
$vars[$paramCount]['name'] = $tokens[$currVar]['content']; |
826
|
|
|
|
827
|
|
|
if ($defaultStart !== null) { |
828
|
|
|
$vars[$paramCount]['default'] |
829
|
|
|
= $phpcsFile->getTokensAsString( |
830
|
|
|
$defaultStart, |
831
|
|
|
($i - $defaultStart) |
832
|
|
|
); |
833
|
|
|
} |
834
|
|
|
|
835
|
|
|
$rawContent = trim($phpcsFile->getTokensAsString($paramStart, ($i - $paramStart))); |
836
|
|
|
|
837
|
|
|
$vars[$paramCount]['pass_by_reference'] = $passByReference; |
838
|
|
|
$vars[$paramCount]['variable_length'] = $variableLength; |
839
|
|
|
$vars[$paramCount]['type_hint'] = $typeHint; |
840
|
|
|
$vars[$paramCount]['raw'] = $rawContent; |
841
|
|
|
|
842
|
|
|
// Reset the vars, as we are about to process the next parameter. |
843
|
|
|
$defaultStart = null; |
844
|
|
|
$paramStart = ($i + 1); |
845
|
|
|
$passByReference = false; |
846
|
|
|
$variableLength = false; |
847
|
|
|
$typeHint = ''; |
848
|
|
|
|
849
|
|
|
$paramCount++; |
850
|
|
|
break; |
851
|
|
|
case T_EQUAL: |
852
|
|
|
$defaultStart = ($i + 1); |
853
|
|
|
break; |
854
|
|
|
}//end switch |
855
|
|
|
}//end for |
856
|
|
|
|
857
|
|
|
return $vars; |
858
|
|
|
|
859
|
|
|
}//end getMethodParameters() |
860
|
|
|
|
861
|
|
|
}//end class |
862
|
|
|
|
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.