1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Throws; |
6
|
|
|
|
7
|
|
|
use ArgumentCountError; |
8
|
|
|
use ArithmeticError; |
9
|
|
|
use AssertionError; |
10
|
|
|
use BadFunctionCallException; |
11
|
|
|
use BadMethodCallException; |
12
|
|
|
use CompileError; |
13
|
|
|
use DivisionByZeroError; |
14
|
|
|
use DomainException; |
15
|
|
|
use Error; |
16
|
|
|
use ErrorException; |
17
|
|
|
use Exception; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
use LengthException; |
20
|
|
|
use LogicException; |
21
|
|
|
use OutOfBoundsException; |
22
|
|
|
use OutOfRangeException; |
23
|
|
|
use OverflowException; |
24
|
|
|
use ParseError; |
25
|
|
|
use RangeException; |
26
|
|
|
use RuntimeException; |
27
|
|
|
use Throwable; |
28
|
|
|
use TypeError; |
29
|
|
|
use UnderflowException; |
30
|
|
|
use UnexpectedValueException; |
31
|
|
|
|
32
|
|
|
class Helper |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @param Throwable $exception |
36
|
|
|
* |
37
|
|
|
* @throws Throwable |
38
|
|
|
*/ |
39
|
12 |
|
public static function thr(Throwable $exception): void |
40
|
|
|
{ |
41
|
12 |
|
throw $exception; |
42
|
|
|
} |
43
|
|
|
|
44
|
6 |
|
public static function thrException(...$args) |
45
|
|
|
{ |
46
|
6 |
|
return static::throwFormattedException(Exception::class, ...$args); |
47
|
|
|
} |
48
|
|
|
|
49
|
3 |
|
public static function thrBadFunctionCallException(...$args) |
50
|
|
|
{ |
51
|
3 |
|
return static::throwFormattedException(BadFunctionCallException::class, ...$args); |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
public static function thrBadMethodCallException(...$args) |
55
|
|
|
{ |
56
|
3 |
|
return static::throwFormattedException(BadMethodCallException::class, ...$args); |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
public static function thrDomainException(...$args) |
60
|
|
|
{ |
61
|
3 |
|
return static::throwFormattedException(DomainException::class, ...$args); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
public static function thrInvalidArgumentException(...$args) |
65
|
|
|
{ |
66
|
3 |
|
return static::throwFormattedException(InvalidArgumentException::class, ...$args); |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
public static function thrLengthException(...$args) |
70
|
|
|
{ |
71
|
3 |
|
return static::throwFormattedException(LengthException::class, ...$args); |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
public static function thrLogicException(...$args) |
75
|
|
|
{ |
76
|
3 |
|
return static::throwFormattedException(LogicException::class, ...$args); |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
public static function thrOutOfBoundsException(...$args) |
80
|
|
|
{ |
81
|
3 |
|
return static::throwFormattedException(OutOfBoundsException::class, ...$args); |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
public static function thrOutOfRangeException(...$args) |
85
|
|
|
{ |
86
|
3 |
|
return static::throwFormattedException(OutOfRangeException::class, ...$args); |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
public static function thrOverflowException(...$args) |
90
|
|
|
{ |
91
|
3 |
|
return static::throwFormattedException(OverflowException::class, ...$args); |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
public static function thrRangeException(...$args) |
95
|
|
|
{ |
96
|
3 |
|
return static::throwFormattedException(RangeException::class, ...$args); |
97
|
|
|
} |
98
|
|
|
|
99
|
3 |
|
public static function thrRuntimeException(...$args) |
100
|
|
|
{ |
101
|
3 |
|
return static::throwFormattedException(RuntimeException::class, ...$args); |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
public static function thrUnderflowException(...$args) |
105
|
|
|
{ |
106
|
3 |
|
return static::throwFormattedException(UnderflowException::class, ...$args); |
107
|
|
|
} |
108
|
|
|
|
109
|
3 |
|
public static function thrUnexpectedValueException(...$args) |
110
|
|
|
{ |
111
|
3 |
|
return static::throwFormattedException(UnexpectedValueException::class, ...$args); |
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
public static function thrErrorException(...$args) |
115
|
|
|
{ |
116
|
3 |
|
return static::throwFormattedException(ErrorException::class, ...$args); |
117
|
|
|
} |
118
|
|
|
|
119
|
3 |
|
public static function thrError(...$args) |
120
|
|
|
{ |
121
|
3 |
|
return static::throwFormattedException(Error::class, ...$args); |
122
|
|
|
} |
123
|
|
|
|
124
|
3 |
|
public static function thrArgumentCountError(...$args) |
125
|
|
|
{ |
126
|
3 |
|
return static::throwFormattedException(ArgumentCountError::class, ...$args); |
127
|
|
|
} |
128
|
|
|
|
129
|
3 |
|
public static function thrArithmeticError(...$args) |
130
|
|
|
{ |
131
|
3 |
|
return static::throwFormattedException(ArithmeticError::class, ...$args); |
132
|
|
|
} |
133
|
|
|
|
134
|
3 |
|
public static function thrAssertionError(...$args) |
135
|
|
|
{ |
136
|
3 |
|
return static::throwFormattedException(AssertionError::class, ...$args); |
137
|
|
|
} |
138
|
|
|
|
139
|
3 |
|
public static function thrDivisionByZeroError(...$args) |
140
|
|
|
{ |
141
|
3 |
|
return static::throwFormattedException(DivisionByZeroError::class, ...$args); |
142
|
|
|
} |
143
|
|
|
|
144
|
3 |
|
public static function thrCompileError(...$args) |
145
|
|
|
{ |
146
|
3 |
|
return static::throwFormattedException(CompileError::class, ...$args); |
147
|
|
|
} |
148
|
|
|
|
149
|
3 |
|
public static function thrParseError(...$args) |
150
|
|
|
{ |
151
|
3 |
|
return static::throwFormattedException(ParseError::class, ...$args); |
152
|
|
|
} |
153
|
|
|
|
154
|
3 |
|
public static function thrTypeError(...$args) |
155
|
|
|
{ |
156
|
3 |
|
return static::throwFormattedException(TypeError::class, ...$args); |
157
|
|
|
} |
158
|
|
|
|
159
|
72 |
|
private static function throwFormattedException(string $class, ...$args): void |
160
|
|
|
{ |
161
|
72 |
|
$exceptionArgs = []; |
162
|
72 |
|
if (count($args) > 0) { |
163
|
69 |
|
$format = array_shift($args); |
164
|
|
|
|
165
|
69 |
|
$exceptionArgs[] = sprintf($format, ...$args); |
166
|
|
|
} |
167
|
|
|
|
168
|
72 |
|
throw new $class(...$exceptionArgs); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|