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