1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCompatibility_Sniffs_PHP_RemovedGlobalVariablesSniff. |
4
|
|
|
* |
5
|
|
|
* @category PHP |
6
|
|
|
* @package PHPCompatibility |
7
|
|
|
* @author Wim Godden <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* PHPCompatibility_Sniffs_PHP_RemovedGlobalVariablesSniff. |
12
|
|
|
* |
13
|
|
|
* Discourages the use of removed global variables. Suggests alternative extensions if available |
14
|
|
|
* |
15
|
|
|
* @category PHP |
16
|
|
|
* @package PHPCompatibility |
17
|
|
|
* @author Wim Godden <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class PHPCompatibility_Sniffs_PHP_RemovedGlobalVariablesSniff extends PHPCompatibility_AbstractRemovedFeatureSniff |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* A list of removed global variables with their alternative, if any. |
24
|
|
|
* |
25
|
|
|
* The array lists : version number with false (deprecated) and true (removed). |
26
|
|
|
* If's sufficient to list the first version where the variable was deprecated/removed. |
27
|
|
|
* |
28
|
|
|
* @var array(string|null) |
29
|
|
|
*/ |
30
|
|
|
protected $removedGlobalVariables = array( |
31
|
|
|
'HTTP_POST_VARS' => array( |
32
|
|
|
'5.3' => false, |
33
|
|
|
'5.4' => true, |
34
|
|
|
'alternative' => '$_POST', |
35
|
|
|
), |
36
|
|
|
'HTTP_GET_VARS' => array( |
37
|
|
|
'5.3' => false, |
38
|
|
|
'5.4' => true, |
39
|
|
|
'alternative' => '$_GET', |
40
|
|
|
), |
41
|
|
|
'HTTP_ENV_VARS' => array( |
42
|
|
|
'5.3' => false, |
43
|
|
|
'5.4' => true, |
44
|
|
|
'alternative' => '$_ENV', |
45
|
|
|
), |
46
|
|
|
'HTTP_SERVER_VARS' => array( |
47
|
|
|
'5.3' => false, |
48
|
|
|
'5.4' => true, |
49
|
|
|
'alternative' => '$_SERVER', |
50
|
|
|
), |
51
|
|
|
'HTTP_COOKIE_VARS' => array( |
52
|
|
|
'5.3' => false, |
53
|
|
|
'5.4' => true, |
54
|
|
|
'alternative' => '$_COOKIE', |
55
|
|
|
), |
56
|
|
|
'HTTP_SESSION_VARS' => array( |
57
|
|
|
'5.3' => false, |
58
|
|
|
'5.4' => true, |
59
|
|
|
'alternative' => '$_SESSION', |
60
|
|
|
), |
61
|
|
|
'HTTP_POST_FILES' => array( |
62
|
|
|
'5.3' => false, |
63
|
|
|
'5.4' => true, |
64
|
|
|
'alternative' => '$_FILES', |
65
|
|
|
), |
66
|
|
|
|
67
|
|
|
'HTTP_RAW_POST_DATA' => array( |
68
|
|
|
'5.6' => false, |
69
|
|
|
'7.0' => true, |
70
|
|
|
'alternative' => 'php://input', |
71
|
|
|
), |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns an array of tokens this test wants to listen for. |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function register() |
80
|
|
|
{ |
81
|
|
|
return array(T_VARIABLE); |
82
|
|
|
|
83
|
|
|
}//end register() |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Processes this test, when one of its tokens is encountered. |
87
|
|
|
* |
88
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
89
|
|
|
* @param int $stackPtr The position of the current token in the |
90
|
|
|
* stack passed in $tokens. |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
95
|
|
|
{ |
96
|
|
|
if ($this->supportsAbove('5.3') === false) { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$tokens = $phpcsFile->getTokens(); |
101
|
|
|
$varName = substr($tokens[$stackPtr]['content'], 1); |
102
|
|
|
|
103
|
|
|
if (isset($this->removedGlobalVariables[$varName]) === false) { |
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($this->inClassScope($phpcsFile, $stackPtr, false) === true) { |
108
|
|
|
/* |
109
|
|
|
* Check for class property definitions. |
110
|
|
|
*/ |
111
|
|
|
$properties = array(); |
112
|
|
|
try { |
113
|
|
|
$properties = $phpcsFile->getMemberProperties($stackPtr); |
114
|
|
|
} catch ( PHP_CodeSniffer_Exception $e) { |
115
|
|
|
// If it's not an expected exception, throw it. |
116
|
|
|
if ($e->getMessage() !== '$stackPtr is not a class member var') { |
117
|
|
|
throw $e; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (isset($properties['scope'])) { |
122
|
|
|
// Ok, so this was a class property declaration, not our concern. |
123
|
|
|
return; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/* |
127
|
|
|
* Check for static usage of class properties shadowing the removed global variables. |
128
|
|
|
*/ |
129
|
|
|
$prevToken = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true); |
130
|
|
|
if ($prevToken !== false && $tokens[$prevToken]['code'] === T_DOUBLE_COLON) { |
131
|
|
|
return; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// Still here, so throw an error/warning. |
136
|
|
|
$itemInfo = array( |
137
|
|
|
'name' => $varName, |
138
|
|
|
); |
139
|
|
|
$this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
140
|
|
|
|
141
|
|
|
}//end process() |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the relevant sub-array for a specific item from a multi-dimensional array. |
146
|
|
|
* |
147
|
|
|
* @param array $itemInfo Base information about the item. |
148
|
|
|
* |
149
|
|
|
* @return array Version and other information about the item. |
150
|
|
|
*/ |
151
|
|
|
public function getItemArray(array $itemInfo) |
152
|
|
|
{ |
153
|
|
|
return $this->removedGlobalVariables[$itemInfo['name']]; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the error message template for this sniff. |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
|
|
protected function getErrorMsgTemplate() |
163
|
|
|
{ |
164
|
|
|
return "Global variable '\$%s' is "; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
}//end class |
169
|
|
|
|
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.