|
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
|
|
|
namespace PHPCompatibility\Sniffs\PHP; |
|
13
|
|
|
|
|
14
|
|
|
use PHPCompatibility\Sniff; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* \PHPCompatibility\Sniffs\PHP\ForbiddenGlobalVariableVariableSniff. |
|
18
|
|
|
* |
|
19
|
|
|
* Variable variables are forbidden with global |
|
20
|
|
|
* |
|
21
|
|
|
* PHP version 7.0 |
|
22
|
|
|
* |
|
23
|
|
|
* @category PHP |
|
24
|
|
|
* @package PHPCompatibility |
|
25
|
|
|
* @author Wim Godden <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class ForbiddenGlobalVariableVariableSniff extends Sniff |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Returns an array of tokens this test wants to listen for. |
|
32
|
|
|
* |
|
33
|
|
|
* @return array |
|
34
|
|
|
*/ |
|
35
|
|
|
public function register() |
|
36
|
|
|
{ |
|
37
|
|
|
return array(T_GLOBAL); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Processes this test, when one of its tokens is encountered. |
|
42
|
|
|
* |
|
43
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
44
|
|
|
* @param int $stackPtr The position of the current token in the |
|
45
|
|
|
* stack passed in $tokens. |
|
46
|
|
|
* |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
50
|
|
|
{ |
|
51
|
|
|
if ($this->supportsAbove('7.0') === false) { |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
56
|
|
|
$endOfStatement = $phpcsFile->findNext(array(T_SEMICOLON, T_CLOSE_TAG), ($stackPtr + 1)); |
|
57
|
|
|
if ($endOfStatement === false) { |
|
58
|
|
|
// No semi-colon - live coding. |
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
for ($ptr = ($stackPtr + 1); $ptr <= $endOfStatement; $ptr++) { |
|
63
|
|
|
$errorThrown = false; |
|
64
|
|
|
$nextComma = $phpcsFile->findNext(T_COMMA, $ptr, $endOfStatement, false, null, true); |
|
65
|
|
|
$varEnd = ($nextComma === false) ? $endOfStatement : $nextComma; |
|
66
|
|
|
$variable = $phpcsFile->findNext(T_VARIABLE, $ptr, $varEnd); |
|
67
|
|
|
$varString = trim($phpcsFile->getTokensAsString($ptr, ($varEnd - $ptr))); |
|
68
|
|
|
$data = array($varString); |
|
69
|
|
|
|
|
70
|
|
|
if ($variable !== false) { |
|
71
|
|
|
|
|
72
|
|
|
$prev = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($variable - 1), $ptr, true); |
|
73
|
|
|
|
|
74
|
|
|
if ($prev !== false && $tokens[$prev]['type'] === 'T_DOLLAR') { |
|
75
|
|
|
|
|
76
|
|
|
$next = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($variable + 1), $varEnd, true); |
|
77
|
|
|
|
|
78
|
|
|
if ($next !== false |
|
79
|
|
|
&& in_array($tokens[$next]['code'], array(T_OPEN_SQUARE_BRACKET, T_OBJECT_OPERATOR, T_DOUBLE_COLON), true) === true |
|
80
|
|
|
) { |
|
81
|
|
|
$phpcsFile->addError( |
|
82
|
|
|
'Global with variable variables is not allowed since PHP 7.0. Found %s', |
|
83
|
|
|
$variable, |
|
84
|
|
|
'Found', |
|
85
|
|
|
$data |
|
86
|
|
|
); |
|
87
|
|
|
$errorThrown = true; |
|
88
|
|
|
} else { |
|
89
|
|
|
$phpcsFile->addWarning( |
|
90
|
|
|
'Global with anything other than bare variables is discouraged since PHP 7.0. Found %s', |
|
91
|
|
|
$variable, |
|
92
|
|
|
'NonBareVariableFound', |
|
93
|
|
|
$data |
|
94
|
|
|
); |
|
95
|
|
|
$errorThrown = true; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ($errorThrown === false) { |
|
101
|
|
|
$dollar = $phpcsFile->findNext(T_DOLLAR, $ptr, $varEnd); |
|
102
|
|
|
if ($dollar !== false) { |
|
103
|
|
|
$next = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($dollar + 1), $varEnd, true); |
|
104
|
|
|
if ($tokens[$next]['code'] === T_OPEN_CURLY_BRACKET) { |
|
105
|
|
|
$phpcsFile->addWarning( |
|
106
|
|
|
'Global with anything other than bare variables is discouraged since PHP 7.0. Found %s', |
|
107
|
|
|
$dollar, |
|
108
|
|
|
'NonBareVariableFound', |
|
109
|
|
|
$data |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// Move the stack pointer forward to the next variable for multi-variable statements. |
|
116
|
|
|
if ($nextComma === false) { |
|
117
|
|
|
break; |
|
118
|
|
|
} |
|
119
|
|
|
$ptr = $nextComma; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|