1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* \PHPCompatibility\Sniffs\PHP\RemovedConstantsSniff. |
4
|
|
|
* |
5
|
|
|
* @category PHP |
6
|
|
|
* @package PHPCompatibility |
7
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace PHPCompatibility\Sniffs\PHP; |
11
|
|
|
|
12
|
|
|
use PHPCompatibility\AbstractRemovedFeatureSniff; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* \PHPCompatibility\Sniffs\PHP\RemovedConstantsSniff. |
16
|
|
|
* |
17
|
|
|
* @category PHP |
18
|
|
|
* @package PHPCompatibility |
19
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class RemovedConstantsSniff extends AbstractRemovedFeatureSniff |
22
|
|
|
{ |
23
|
|
|
/* |
24
|
|
|
@todo Verify this list in detail by checking the constants pages of all extensions! |
25
|
|
|
List currently based on: http://php.net/manual/en/doc.changelog.php |
26
|
|
|
|
27
|
|
|
Note: sniff currently uses tabs -> needs to be converted back to spaces. |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* A list of removed PHP Constants. |
32
|
|
|
* |
33
|
|
|
* The array lists : version number with false (deprecated) or true (removed). |
34
|
|
|
* If's sufficient to list the first version where the constant was deprecated/removed. |
35
|
|
|
* |
36
|
|
|
* Note: PHP Constants are case-sensitive! |
37
|
|
|
* |
38
|
|
|
* @var array(string => array(string => bool|string|null)) |
39
|
|
|
*/ |
40
|
|
|
protected $removedConstants = array( |
41
|
|
|
'PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT' => array( |
42
|
|
|
'7.0' => true, |
43
|
|
|
), |
44
|
|
|
|
45
|
|
|
'INTL_IDNA_VARIANT_2003' => array( |
46
|
|
|
'7.2' => false, |
47
|
|
|
// 8.0. => true, |
|
|
|
|
48
|
|
|
), |
49
|
|
|
|
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns an array of tokens this test wants to listen for. |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public function register() |
59
|
|
|
{ |
60
|
|
|
return array(T_STRING); |
61
|
|
|
|
62
|
|
|
}//end register() |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Processes this test, when one of its tokens is encountered. |
67
|
|
|
* |
68
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
69
|
|
|
* @param int $stackPtr The position of the current token in |
70
|
|
|
* the stack passed in $tokens. |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
75
|
|
|
{ |
76
|
|
|
$tokens = $phpcsFile->getTokens(); |
77
|
|
|
$constantName = $tokens[$stackPtr]['content']; |
78
|
|
|
|
79
|
|
|
if (isset($this->removedConstants[$constantName]) === false) { |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($this->isUseOfGlobalConstant($phpcsFile, $stackPtr) === false) { |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$itemInfo = array( |
88
|
|
|
'name' => $constantName, |
89
|
|
|
); |
90
|
|
|
$this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
91
|
|
|
|
92
|
|
|
}//end process() |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the relevant sub-array for a specific item from a multi-dimensional array. |
97
|
|
|
* |
98
|
|
|
* @param array $itemInfo Base information about the item. |
99
|
|
|
* |
100
|
|
|
* @return array Version and other information about the item. |
101
|
|
|
*/ |
102
|
|
|
public function getItemArray(array $itemInfo) |
103
|
|
|
{ |
104
|
|
|
return $this->removedConstants[$itemInfo['name']]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the error message template for this sniff. |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
protected function getErrorMsgTemplate() |
114
|
|
|
{ |
115
|
|
|
return 'The constant "%s" is '; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
}//end class |
119
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.