1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCompatibility_Sniffs_PHP_NewHashAlgorithmsSniff. |
4
|
|
|
* |
5
|
|
|
* @category PHP |
6
|
|
|
* @package PHPCompatibility |
7
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* PHPCompatibility_Sniffs_PHP_NewHashAlgorithmsSniff. |
12
|
|
|
* |
13
|
|
|
* @category PHP |
14
|
|
|
* @package PHPCompatibility |
15
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class PHPCompatibility_Sniffs_PHP_NewHashAlgorithmsSniff extends PHPCompatibility_AbstractNewFeatureSniff |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* A list of new hash algorithms, not present in older versions. |
21
|
|
|
* |
22
|
|
|
* The array lists : version number with false (not present) or true (present). |
23
|
|
|
* If's sufficient to list the first version where the hash algorithm appears. |
24
|
|
|
* |
25
|
|
|
* @var array(string => array(string => bool)) |
26
|
|
|
*/ |
27
|
|
|
protected $newAlgorithms = array( |
28
|
|
|
'md2' => array( |
29
|
|
|
'5.2' => false, |
30
|
|
|
'5.3' => true, |
31
|
|
|
), |
32
|
|
|
'ripemd256' => array( |
33
|
|
|
'5.2' => false, |
34
|
|
|
'5.3' => true, |
35
|
|
|
), |
36
|
|
|
'ripemd320' => array( |
37
|
|
|
'5.2' => false, |
38
|
|
|
'5.3' => true, |
39
|
|
|
), |
40
|
|
|
'salsa10' => array( |
41
|
|
|
'5.2' => false, |
42
|
|
|
'5.3' => true, |
43
|
|
|
), |
44
|
|
|
'salsa20' => array( |
45
|
|
|
'5.2' => false, |
46
|
|
|
'5.3' => true, |
47
|
|
|
), |
48
|
|
|
'snefru256' => array( |
49
|
|
|
'5.2' => false, |
50
|
|
|
'5.3' => true, |
51
|
|
|
), |
52
|
|
|
'sha224' => array( |
53
|
|
|
'5.2' => false, |
54
|
|
|
'5.3' => true, |
55
|
|
|
), |
56
|
|
|
'joaat' => array( |
57
|
|
|
'5.3' => false, |
58
|
|
|
'5.4' => true, |
59
|
|
|
), |
60
|
|
|
'fnv132' => array( |
61
|
|
|
'5.3' => false, |
62
|
|
|
'5.4' => true, |
63
|
|
|
), |
64
|
|
|
'fnv164' => array( |
65
|
|
|
'5.3' => false, |
66
|
|
|
'5.4' => true, |
67
|
|
|
), |
68
|
|
|
'gost-crypto' => array( |
69
|
|
|
'5.5' => false, |
70
|
|
|
'5.6' => true, |
71
|
|
|
), |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns an array of tokens this test wants to listen for. |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function register() |
81
|
|
|
{ |
82
|
|
|
return array(T_STRING); |
83
|
|
|
|
84
|
|
|
}//end register() |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Processes this test, when one of its tokens is encountered. |
89
|
|
|
* |
90
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
91
|
|
|
* @param int $stackPtr The position of the current token in the |
92
|
|
|
* stack passed in $tokens. |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
97
|
|
|
{ |
98
|
|
|
$algo = $this->getHashAlgorithmParameter($phpcsFile, $stackPtr); |
99
|
|
|
if (empty($algo) || is_string($algo) === false) { |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Bow out if not one of the algorithms we're targetting. |
104
|
|
|
if (isset($this->newAlgorithms[$algo]) === false) { |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Check if the algorithm used is new. |
109
|
|
|
$itemInfo = array( |
110
|
|
|
'name' => $algo, |
111
|
|
|
); |
112
|
|
|
$this->handleFeature($phpcsFile, $stackPtr, $itemInfo); |
113
|
|
|
|
114
|
|
|
}//end process() |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the relevant sub-array for a specific item from a multi-dimensional array. |
119
|
|
|
* |
120
|
|
|
* @param array $itemInfo Base information about the item. |
121
|
|
|
* |
122
|
|
|
* @return array Version and other information about the item. |
123
|
|
|
*/ |
124
|
|
|
public function getItemArray(array $itemInfo) |
125
|
|
|
{ |
126
|
|
|
return $this->newAlgorithms[$itemInfo['name']]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the error message template for this sniff. |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
protected function getErrorMsgTemplate() |
136
|
|
|
{ |
137
|
|
|
return 'The %s hash algorithm is not present in PHP version %s or earlier'; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
}//end class |
142
|
|
|
|
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.