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_RAW_POST_DATA' => array( |
32
|
|
|
'5.6' => false, |
33
|
|
|
'7.0' => true, |
34
|
|
|
'alternative' => 'php://input' |
35
|
|
|
), |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns an array of tokens this test wants to listen for. |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public function register() |
44
|
|
|
{ |
45
|
|
|
return array(T_VARIABLE); |
46
|
|
|
|
47
|
|
|
}//end register() |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Processes this test, when one of its tokens is encountered. |
51
|
|
|
* |
52
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
53
|
|
|
* @param int $stackPtr The position of the current token in the |
54
|
|
|
* stack passed in $tokens. |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$tokens = $phpcsFile->getTokens(); |
61
|
|
|
$varName = substr($tokens[$stackPtr]['content'], 1); |
62
|
|
|
|
63
|
|
|
if (isset($this->removedGlobalVariables[$varName]) === false) { |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$itemInfo = array( |
68
|
|
|
'name' => $varName, |
69
|
|
|
); |
70
|
|
|
$this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
71
|
|
|
|
72
|
|
|
}//end process() |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the relevant sub-array for a specific item from a multi-dimensional array. |
77
|
|
|
* |
78
|
|
|
* @param array $itemInfo Base information about the item. |
79
|
|
|
* |
80
|
|
|
* @return array Version and other information about the item. |
81
|
|
|
*/ |
82
|
|
|
public function getItemArray(array $itemInfo) |
83
|
|
|
{ |
84
|
|
|
return $this->removedGlobalVariables[$itemInfo['name']]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the error message template for this sniff. |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
protected function getErrorMsgTemplate() |
94
|
|
|
{ |
95
|
|
|
return "Global variable '\$%s' is "; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
}//end class |
100
|
|
|
|
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.