1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mpyw\Exceper; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Exceper |
7
|
|
|
* @package mpyw\Exceper |
8
|
|
|
* @method static mixed to(string $class, callable $callback, int $types = E_ALL | E_STRICT) |
9
|
|
|
* @method static mixed toException(callable $callback, int $types = E_ALL | E_STRICT) |
10
|
|
|
* @method static mixed toError(callable $callback, int $types = E_ALL | E_STRICT) |
11
|
|
|
* @method static mixed toParseError(callable $callback, int $types = E_ALL | E_STRICT) |
12
|
|
|
* @method static mixed toTypeError(callable $callback, int $types = E_ALL | E_STRICT) |
13
|
|
|
* @method static mixed toArgumentCountError(callable $callback, int $types = E_ALL | E_STRICT) |
14
|
|
|
* @method static mixed toArithmeticError(callable $callback, int $types = E_ALL | E_STRICT) |
15
|
|
|
* @method static mixed toDivisionByZeroError(callable $callback, int $types = E_ALL | E_STRICT) |
16
|
|
|
* @method static mixed toClosedGeneratorException(callable $callback, int $types = E_ALL | E_STRICT) |
17
|
|
|
* @method static mixed toDOMException(callable $callback, int $types = E_ALL | E_STRICT) |
18
|
|
|
* @method static mixed toLogicException(callable $callback, int $types = E_ALL | E_STRICT) |
19
|
|
|
* @method static mixed toBadFunctionCallException(callable $callback, int $types = E_ALL | E_STRICT) |
20
|
|
|
* @method static mixed toBadMethodCallException(callable $callback, int $types = E_ALL | E_STRICT) |
21
|
|
|
* @method static mixed toDomainException(callable $callback, int $types = E_ALL | E_STRICT) |
22
|
|
|
* @method static mixed toInvalidArgumentException(callable $callback, int $types = E_ALL | E_STRICT) |
23
|
|
|
* @method static mixed toLengthException(callable $callback, int $types = E_ALL | E_STRICT) |
24
|
|
|
* @method static mixed toOutOfRangeException(callable $callback, int $types = E_ALL | E_STRICT) |
25
|
|
|
* @method static mixed toRuntimeException(callable $callback, int $types = E_ALL | E_STRICT) |
26
|
|
|
* @method static mixed toOutOfBoundsException(callable $callback, int $types = E_ALL | E_STRICT) |
27
|
|
|
* @method static mixed toOverflowException(callable $callback, int $types = E_ALL | E_STRICT) |
28
|
|
|
* @method static mixed toRangeException(callable $callback, int $types = E_ALL | E_STRICT) |
29
|
|
|
* @method static mixed toUnderflowException(callable $callback, int $types = E_ALL | E_STRICT) |
30
|
|
|
* @method static mixed toUnexpectedValueException(callable $callback, int $types = E_ALL | E_STRICT) |
31
|
|
|
* @method static mixed toAssertionError(callable $callback, int $types = E_ALL | E_STRICT) |
32
|
|
|
* @method static mixed toPDOException(callable $callback, int $types = E_ALL | E_STRICT) |
33
|
|
|
* @method static mixed toPharException(callable $callback, int $types = E_ALL | E_STRICT) |
34
|
|
|
* @method static mixed toReflectionException(callable $callback, int $types = E_ALL | E_STRICT) |
35
|
|
|
* @method static mixed tomysqli_sql_exception(callable $callback, int $types = E_ALL | E_STRICT) |
36
|
|
|
* @method static mixed toSoapFault(callable $callback, int $types = E_ALL | E_STRICT) |
37
|
|
|
*/ |
38
|
|
|
class Convert |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @param string $method |
42
|
|
|
* @param array $args |
43
|
|
|
* @param int $required |
44
|
|
|
*/ |
45
|
16 |
|
protected static function validateArgumentTypes($method, array $args, $required) |
46
|
|
|
{ |
47
|
16 |
|
$argc = count($args); |
48
|
16 |
|
if ($argc < $required) { |
49
|
3 |
|
throw new \InvalidArgumentException(get_called_class() . "::$method() expects at least $required parameters, $argc given"); |
50
|
|
|
} |
51
|
13 |
|
if ($required === 2 and !is_string($args[0]) and !is_object($args[0]) || !method_exists($args[0], '__toString')) { |
52
|
1 |
|
throw new \InvalidArgumentException(get_called_class() . "::$method() expects parameter 1 to be string, " . gettype($args[0]) . ' given'); |
53
|
|
|
} |
54
|
12 |
|
if (!is_callable($args[$required - 1])) { |
55
|
2 |
|
throw new \InvalidArgumentException(get_called_class() . "::$method() expects parameter $required to be callable, " . gettype($args[$required - 1]) . ' given'); |
56
|
|
|
} |
57
|
10 |
|
if (isset($args[$required]) && !is_numeric($args[$required])) { |
58
|
2 |
|
throw new \InvalidArgumentException(get_called_class() . "::$method() expects parameter " . ($required + 1) . ' to be integer, ' . gettype($args[$required]) . ' given'); |
59
|
|
|
} |
60
|
8 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $method |
64
|
|
|
* @param array $args |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
17 |
|
public static function __callStatic($method, array $args) |
68
|
|
|
{ |
69
|
17 |
|
if (strtolower(substr($method, 0, 2)) !== 'to') { |
70
|
1 |
|
throw new \BadMethodCallException('Call to undefined method ' . get_called_class() . "::$method()"); |
71
|
|
|
} |
72
|
|
|
|
73
|
16 |
|
$class = substr($method, 2); |
74
|
16 |
|
$required = $class === '' ? 2 : 1; |
75
|
16 |
|
self::validateArgumentTypes($method, $args, $required); |
76
|
|
|
|
77
|
8 |
|
if ($class === '') { |
78
|
5 |
|
$class = (string)array_shift($args); |
79
|
|
|
} |
80
|
|
|
|
81
|
8 |
|
if (!class_exists($class) and !is_subclass_of($class, '\Exception') and \PHP_VERSION < 7 || !is_subclass_of($class, '\Throwable')) { |
82
|
1 |
|
throw new \DomainException("The class \"$class\" must be an instance of Exception or Throwable"); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return Core::handle($args[0], function ($severity, $message, $file, $line) use ($class, $args) { |
86
|
5 |
|
if (!(error_reporting() & $severity)) { |
87
|
1 |
|
return; |
88
|
|
|
} |
89
|
4 |
|
throw Core::rewriteLocation(new $class($message), $file, $line); |
90
|
7 |
|
}, isset($args[1]) ? (int)$args[1] : E_ALL | E_STRICT); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param callable $callback |
95
|
|
|
* @param int $types |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
5 |
|
public static function toErrorException($callback, $types = E_ALL | E_STRICT) |
99
|
|
|
{ |
100
|
5 |
View Code Duplication |
if (!is_callable($callback)) { |
101
|
1 |
|
throw new \InvalidArgumentException(get_called_class() . '::' . __METHOD__ . '() expects parameter 1 to be callable, ' . gettype($callback) . ' given'); |
102
|
|
|
} |
103
|
4 |
View Code Duplication |
if ($types !== null && !is_numeric($types)) { |
104
|
1 |
|
throw new \InvalidArgumentException(get_called_class() . '::' . __METHOD__ . '() expects parameter 2 to be integer, ' . gettype($types) . ' given'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return Core::handle($callback, function ($severity, $message, $file, $line) { |
108
|
2 |
|
if (!(error_reporting() & $severity)) { |
109
|
1 |
|
return; |
110
|
|
|
} |
111
|
1 |
|
throw new \ErrorException($message, 0, $severity, $file, $line); |
112
|
3 |
|
}, (int)$types); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param callable $callback |
117
|
|
|
* @param int $types |
118
|
|
|
* @return mixed |
119
|
|
|
*/ |
120
|
5 |
|
public static function silent($callback, $types = E_ALL | E_STRICT) |
121
|
|
|
{ |
122
|
5 |
View Code Duplication |
if (!is_callable($callback)) { |
|
|
|
|
123
|
1 |
|
throw new \InvalidArgumentException(get_called_class() . '::' . __METHOD__ . '() expects parameter 1 to be callable, ' . gettype($callback) . ' given'); |
124
|
|
|
} |
125
|
4 |
View Code Duplication |
if ($types !== null && !is_numeric($types)) { |
|
|
|
|
126
|
1 |
|
throw new \InvalidArgumentException(get_called_class() . '::' . __METHOD__ . '() expects parameter 2 to be integer, ' . gettype($types) . ' given'); |
127
|
|
|
} |
128
|
|
|
|
129
|
3 |
|
$e = new \Exception; |
130
|
|
|
try { |
131
|
3 |
|
return Core::handle($callback, function ($severity) use ($e) { |
132
|
2 |
|
if (!(error_reporting() & $severity)) { |
133
|
1 |
|
return; |
134
|
|
|
} |
135
|
2 |
|
throw $e; |
136
|
3 |
|
}, (int)$types); |
137
|
3 |
|
} catch (\Exception $e) { |
138
|
1 |
|
} catch (\Throwable $e) { |
139
|
|
|
} |
140
|
3 |
|
} |
141
|
|
|
} |
142
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.