1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Reason.php |
4
|
|
|
* |
5
|
|
|
* MIT LICENSE |
6
|
|
|
* |
7
|
|
|
* LICENSE: This source file is subject to the MIT license. |
8
|
|
|
* A copy of the licenses text was distributed alongside this |
9
|
|
|
* file (usually the repository or package root). The text can also |
10
|
|
|
* be obtained through one of the following sources: |
11
|
|
|
* * http://opensource.org/licenses/MIT |
12
|
|
|
* * https://github.com/suralc/pvra/blob/master/LICENSE |
13
|
|
|
* |
14
|
|
|
* @author suralc <[email protected]> |
15
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
16
|
|
|
*/ |
17
|
|
|
namespace Pvra\Result; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Reason |
21
|
|
|
* |
22
|
|
|
* @package Pvra\Result |
23
|
|
|
*/ |
24
|
|
|
abstract class Reason |
25
|
|
|
{ |
26
|
|
|
// These integers are arbitrary. They may appear to follow some system, but they do not. Always use the constants. |
27
|
|
|
const UNKNOWN = 0, |
28
|
|
|
LIB_CLASS_ADDITION = 1, |
29
|
|
|
LIB_CLASS_REMOVAL = 3, |
30
|
|
|
LIB_CLASS_DEPRECATION = 6, |
31
|
|
|
LIB_FUNCTION_ADDITION = 2, |
32
|
|
|
LIB_FUNCTION_REMOVAL = 4, |
33
|
|
|
LIB_FUNCTION_DEPRECATION = 5, |
34
|
|
|
LIB_CONSTANT_ADDITION = 7, |
35
|
|
|
LIB_CONSTANT_DEPRECATION = 8, |
36
|
|
|
LIB_CONSTANT_REMOVAL = 9, |
37
|
|
|
// 5.3 |
38
|
|
|
GOTO_KEYWORD = 70, |
39
|
|
|
JUMP_LABEL = 71, |
40
|
|
|
NAMESPACE_DECLERATION = 72, |
41
|
|
|
NAMESPACE_MAGIC_CONSTANT = 73, |
42
|
|
|
NAMESPACE_IMPORT = 74, |
43
|
|
|
NOWDOC_LITERAL = 75, |
44
|
|
|
CALLSTATIC_MAGIC_METHOD = 76, |
45
|
|
|
INVOKE_MAGIC_METHOD = 77, |
46
|
|
|
CONST_KEYWORD_OUTSIDE_CLASS = 78, |
47
|
|
|
CONST_KEYWORD_DOC_SYNTAX = 79, |
48
|
|
|
SHORT_TERNARY = 80, |
49
|
|
|
CLOSURE_DECLARATION = 81, |
50
|
|
|
DYNAMIC_ACCESS_TO_STATIC = 82, |
51
|
|
|
LATE_STATE_BINDING_USING_STATIC = 83, |
52
|
|
|
NAMESPACE_SEPARATOR = 84, |
53
|
|
|
DIR_MAGIC_CONSTANT = 85, |
54
|
|
|
NEW_ASSIGN_BY_REF_DEP = 86, |
55
|
|
|
// 5.4 |
56
|
|
|
TRAIT_DEFINITION = 10, |
57
|
|
|
TRAIT_USE = 11, |
58
|
|
|
TRAIT_MAGIC_CONST = 12, |
59
|
|
|
ARRAY_FUNCTION_DEREFERENCING = 13, |
60
|
|
|
THIS_IN_CLOSURE = 14, |
61
|
|
|
TYPEHINT_CALLABLE = 15, |
62
|
|
|
INSTANT_CLASS_MEMBER_ACCESS = 16, |
63
|
|
|
BINARY_NUMBER_DECLARATION = 17, |
64
|
|
|
SHORT_ARRAY_DECLARATION = 18, |
65
|
|
|
STATIC_CALL_BY_EXPRESSION = 19, |
66
|
|
|
SHORT_ECHO_TAG = 20, |
67
|
|
|
// 5.5 |
68
|
|
|
GENERATOR_DEFINITION = 30, |
69
|
|
|
TRY_CATCH_FINALLY = 31, |
70
|
|
|
LIST_IN_FOREACH = 32, |
71
|
|
|
EXPR_IN_EMPTY = 33, |
72
|
|
|
ARRAY_OR_STRING_DEREFERENCING = 34, |
73
|
|
|
CLASS_NAME_RESOLUTION = 35, |
74
|
|
|
// 5.6 |
75
|
|
|
VARIADIC_ARGUMENT = 50, |
76
|
|
|
ARGUMENT_UNPACKING = 51, |
77
|
|
|
CONSTANT_SCALAR_EXPRESSION = 52, |
78
|
|
|
POW_OPERATOR = 53, |
79
|
|
|
FUNCTION_IMPORT_USE = 54, |
80
|
|
|
CONSTANT_IMPORT_USE = 55, |
81
|
|
|
// 7.0 |
82
|
|
|
RESERVED_CLASS_NAME = 100, |
83
|
|
|
SOFT_RESERVED_NAME = 101, |
84
|
|
|
PHP4_CONSTRUCTOR = 103, |
85
|
|
|
COALESCE_OPERATOR = 104, |
86
|
|
|
SPACESHIP_OPERATOR = 105, |
87
|
|
|
RETURN_TYPE = 106, |
88
|
|
|
YIELD_FROM = 107, |
89
|
|
|
ANON_CLASS = 108, |
90
|
|
|
NEW_ASSIGN_BY_REF_REM = 109, |
91
|
|
|
STRICT_TYPE_DECLARE = 110; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @var array|null |
95
|
|
|
*/ |
96
|
|
|
private static $reasonToVersion; |
97
|
|
|
/** |
98
|
|
|
* @var array|null |
99
|
|
|
*/ |
100
|
|
|
private static $constantsCache; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get version information from a reason. |
104
|
|
|
* |
105
|
|
|
* This static method may be used to get version information of a reason constant defined in Reason. |
106
|
|
|
* If no matching version for a constant can be found an InvalidArgumentException will be thrown. |
107
|
|
|
* |
108
|
|
|
* This method may return a falseable string or bool(false). Use !== false to check for a valid returned version. |
109
|
|
|
* If false is returned refer to the corresponding method on the class that returned the constant. |
110
|
|
|
* |
111
|
|
|
* @param int $reason One of the constants defined in RequirementReason |
112
|
|
|
* @return string|false The required version or bool(false) |
113
|
118 |
|
*/ |
114
|
|
|
public static function getVersionFromReason($reason) |
115
|
118 |
|
{ |
116
|
8 |
|
if (self::$reasonToVersion === null) { |
117
|
4 |
|
self::$reasonToVersion = static::getReasonToVersionBaseValues(); |
118
|
|
|
} |
119
|
118 |
|
|
120
|
114 |
|
if (isset(self::$reasonToVersion[ $reason ])) { |
121
|
8 |
|
return self::$reasonToVersion[ $reason ]; |
122
|
8 |
|
} elseif ($reason > self::UNKNOWN && $reason < self::TRAIT_DEFINITION) { |
123
|
|
|
return false; |
124
|
2 |
|
} else { |
125
|
2 |
|
throw new \InvalidArgumentException(sprintf('There is no required version defined for this reason(id: "%s", name: "%s").', |
126
|
|
|
$reason, static::getReasonNameFromValue($reason))); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the constant name from constant value |
132
|
|
|
* |
133
|
|
|
* @param int $value |
134
|
|
|
* @return string The name of the constant or 'UNKNOWN' |
135
|
110 |
|
*/ |
136
|
|
|
public static function getReasonNameFromValue($value) |
137
|
110 |
|
{ |
138
|
110 |
|
$names = static::getReasonNames(); |
139
|
|
|
$names = array_flip($names); |
140
|
110 |
|
|
141
|
108 |
|
if (isset($names[ $value ])) { |
142
|
|
|
return $names[ $value ]; |
143
|
|
|
} |
144
|
4 |
|
|
145
|
|
|
return 'UNKNOWN'; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get a list of defined constants and their values. |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
112 |
|
*/ |
153
|
|
|
public static function getReasonNames() |
154
|
112 |
|
{ |
155
|
112 |
|
if (self::$constantsCache !== null) { |
156
|
|
|
return self::$constantsCache; |
157
|
|
|
} |
158
|
10 |
|
|
159
|
10 |
|
$constants = (new \ReflectionClass(get_called_class())) |
160
|
|
|
->getConstants(); |
161
|
10 |
|
|
162
|
10 |
|
foreach ($constants as $name => $value) { |
163
|
5 |
|
self::$constantsCache[ $name ] = $value; |
164
|
|
|
} |
165
|
10 |
|
|
166
|
|
|
return self::$constantsCache; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Clears the cached constant lists. |
171
|
|
|
* |
172
|
|
|
* May be useful to save memory or to force regeneration of the list in tests. |
173
|
8 |
|
*/ |
174
|
|
|
public static function clear() |
175
|
8 |
|
{ |
176
|
8 |
|
self::$reasonToVersion = null; |
177
|
8 |
|
self::$constantsCache = null; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Map reasons to versions |
182
|
|
|
* |
183
|
|
|
* This method returns an array having the constants defined by this class as keys and their required |
184
|
|
|
* php versions as values. |
185
|
|
|
* |
186
|
|
|
* @return array |
187
|
8 |
|
*/ |
188
|
|
|
protected static function getReasonToVersionBaseValues() |
189
|
|
|
{ |
190
|
8 |
|
return [ |
191
|
|
|
self::UNKNOWN => '7.0.0', |
192
|
8 |
|
// 5.4 |
193
|
8 |
|
self::GOTO_KEYWORD => '5.3.0', |
194
|
8 |
|
self::JUMP_LABEL => '5.3.0', |
195
|
8 |
|
self::NAMESPACE_DECLERATION => '5.3.0', |
196
|
8 |
|
self::NAMESPACE_MAGIC_CONSTANT => '5.3.0', |
197
|
8 |
|
self::NAMESPACE_IMPORT => '5.3.0', |
198
|
8 |
|
self::NAMESPACE_SEPARATOR => '5.3.0', |
199
|
8 |
|
self::NOWDOC_LITERAL => '5.3.0', |
200
|
8 |
|
self::CALLSTATIC_MAGIC_METHOD => '5.3.0', |
201
|
8 |
|
self::INVOKE_MAGIC_METHOD => '5.3.0', |
202
|
8 |
|
self::CONST_KEYWORD_OUTSIDE_CLASS => '5.3.0', |
203
|
8 |
|
self::CONST_KEYWORD_DOC_SYNTAX => '5.3.0', |
204
|
8 |
|
self::SHORT_TERNARY => '5.3.0', |
205
|
8 |
|
self::CLOSURE_DECLARATION => '5.3.0', |
206
|
8 |
|
self::DYNAMIC_ACCESS_TO_STATIC => '5.3.0', |
207
|
8 |
|
self::LATE_STATE_BINDING_USING_STATIC => '5.3.0', |
208
|
8 |
|
self::DIR_MAGIC_CONSTANT => '5.3.0', |
209
|
|
|
self::NEW_ASSIGN_BY_REF_DEP => '5.3.0', |
210
|
8 |
|
// 5.4 |
211
|
8 |
|
self::TRAIT_DEFINITION => '5.4.0', |
212
|
8 |
|
self::TRAIT_USE => '5.4.0', |
213
|
8 |
|
self::TRAIT_MAGIC_CONST => '5.4.0', |
214
|
8 |
|
self::ARRAY_FUNCTION_DEREFERENCING => '5.4.0', |
215
|
8 |
|
self::THIS_IN_CLOSURE => '5.4.0', |
216
|
8 |
|
self::TYPEHINT_CALLABLE => '5.4.0', |
217
|
8 |
|
self::INSTANT_CLASS_MEMBER_ACCESS => '5.4.0', |
218
|
8 |
|
self::BINARY_NUMBER_DECLARATION => '5.4.0', |
219
|
8 |
|
self::SHORT_ARRAY_DECLARATION => '5.4.0', |
220
|
8 |
|
self::STATIC_CALL_BY_EXPRESSION => '5.4.0', |
221
|
|
|
self::SHORT_ECHO_TAG => '5.4.0', |
222
|
8 |
|
// 5.5 |
223
|
8 |
|
self::GENERATOR_DEFINITION => '5.5.0', |
224
|
8 |
|
self::TRY_CATCH_FINALLY => '5.5.0', |
225
|
8 |
|
self::LIST_IN_FOREACH => '5.5.0', |
226
|
8 |
|
self::EXPR_IN_EMPTY => '5.5.0', |
227
|
8 |
|
self::ARRAY_OR_STRING_DEREFERENCING => '5.5.0', |
228
|
|
|
self::CLASS_NAME_RESOLUTION => '5.5.0', |
229
|
8 |
|
// 5.6 |
230
|
8 |
|
self::VARIADIC_ARGUMENT => '5.6.0', |
231
|
8 |
|
self::ARGUMENT_UNPACKING => '5.6.0', |
232
|
8 |
|
self::CONSTANT_SCALAR_EXPRESSION => '5.6.0', |
233
|
8 |
|
self::POW_OPERATOR => '5.6.0', |
234
|
8 |
|
self::FUNCTION_IMPORT_USE => '5.6.0', |
235
|
|
|
self::CONSTANT_IMPORT_USE => '5.6.0', |
236
|
8 |
|
// 7.0 |
237
|
8 |
|
self::RESERVED_CLASS_NAME => '7.0.0', |
238
|
8 |
|
self::SOFT_RESERVED_NAME => '7.0.0', |
239
|
8 |
|
self::PHP4_CONSTRUCTOR => '7.0.0', |
240
|
8 |
|
self::COALESCE_OPERATOR => '7.0.0', |
241
|
8 |
|
self::SPACESHIP_OPERATOR => '7.0.0', |
242
|
8 |
|
self::RETURN_TYPE => '7.0.0', |
243
|
8 |
|
self::YIELD_FROM => '7.0.0', |
244
|
8 |
|
self::ANON_CLASS => '7.0.0', |
245
|
4 |
|
self::NEW_ASSIGN_BY_REF_REM => '7.0.0', |
246
|
|
|
self::STRICT_TYPE_DECLARE => '7.0.0', |
247
|
|
|
]; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @codeCoverageIgnore |
252
|
|
|
*/ |
253
|
|
|
private function __construct() |
254
|
|
|
{ |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|