1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCompatibility_Sniffs_PHP_ForbiddenGlobalVariableVariableSniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 7.0 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHPCompatibility |
9
|
|
|
* @author Wim Godden <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* PHPCompatibility_Sniffs_PHP_ForbiddenGlobalVariableVariableSniff. |
14
|
|
|
* |
15
|
|
|
* Variable variables are forbidden with global |
16
|
|
|
* |
17
|
|
|
* PHP version 7.0 |
18
|
|
|
* |
19
|
|
|
* @category PHP |
20
|
|
|
* @package PHPCompatibility |
21
|
|
|
* @author Wim Godden <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class PHPCompatibility_Sniffs_PHP_ForbiddenGlobalVariableVariableSniff extends PHPCompatibility_Sniff |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns an array of tokens this test wants to listen for. |
28
|
|
|
* |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
public function register() |
32
|
|
|
{ |
33
|
|
|
return array(T_GLOBAL); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Processes this test, when one of its tokens is encountered. |
38
|
|
|
* |
39
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
40
|
|
|
* @param int $stackPtr The position of the current token in the |
41
|
|
|
* stack passed in $tokens. |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
46
|
|
|
{ |
47
|
|
|
if ($this->supportsAbove('7.0') === false) { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$tokens = $phpcsFile->getTokens(); |
52
|
|
|
$endOfStatement = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1)); |
53
|
|
|
if ($endOfStatement === false) { |
54
|
|
|
// No semi-colon - live coding. |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
for ($ptr = ($stackPtr + 1); $ptr <= $endOfStatement; $ptr++) { |
59
|
|
|
$variable = $phpcsFile->findNext(T_VARIABLE, $ptr, $endOfStatement, false, null, true); |
60
|
|
|
if ($variable !== false && (isset($tokens[$variable - 1]) && $tokens[$variable - 1]['type'] === 'T_DOLLAR')) { |
61
|
|
|
$phpcsFile->addError( |
62
|
|
|
'Global with variable variables is not allowed since PHP 7.0', |
63
|
|
|
$stackPtr, |
64
|
|
|
'Found' |
65
|
|
|
); |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Move the stack pointer forward to the next variable for multi-variable statements. |
70
|
|
|
$nextComma = $phpcsFile->findNext(T_COMMA, $ptr, $endOfStatement, false, null, true); |
71
|
|
|
if ($nextComma === false) { |
72
|
|
|
break; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$ptr = $nextComma; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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.