1 | <?php |
||
8 | trait HandleUncaughtError |
||
9 | { |
||
10 | /** |
||
11 | * @var callable|false |
||
12 | */ |
||
13 | protected $chainedErrorHandler; |
||
14 | |||
15 | /** |
||
16 | * Convert fatal errors to exceptions |
||
17 | * @var boolean |
||
18 | */ |
||
19 | protected $convertFatalErrors = false; |
||
20 | |||
21 | /** |
||
22 | * Log the following error types (in addition to caugth errors) |
||
23 | * @var int |
||
24 | */ |
||
25 | protected $logErrorTypes = 0; |
||
26 | |||
27 | |||
28 | /** |
||
29 | * Run the fatal error callback |
||
30 | * |
||
31 | * @param \Exception|\Error $error |
||
32 | */ |
||
33 | abstract protected function callOnFatalError($error); |
||
34 | |||
35 | /** |
||
36 | * Wrapper method for `error_reporting` |
||
37 | * |
||
38 | * @return int |
||
39 | */ |
||
40 | abstract protected function errorReporting(); |
||
41 | |||
42 | /** |
||
43 | * Wrapper method for `set_error_handler` |
||
44 | * |
||
45 | * @param callable $callback |
||
46 | * @param int $error_types |
||
47 | * @return callable|null |
||
48 | */ |
||
49 | abstract protected function setErrorHandler($callback, $error_types = E_ALL); |
||
50 | |||
51 | /** |
||
52 | * Register the shutdown function |
||
53 | */ |
||
54 | abstract protected function initShutdownFunction(); |
||
55 | |||
56 | /** |
||
57 | * Log an error or exception |
||
58 | * |
||
59 | * @param \Exception|\Error $error |
||
60 | */ |
||
61 | abstract public function log($error); |
||
62 | |||
63 | |||
64 | /** |
||
65 | * Get the error handler that has been replaced. |
||
66 | * |
||
67 | * @return callable|false|null |
||
68 | */ |
||
69 | 2 | public function getChainedErrorHandler() |
|
73 | |||
74 | /** |
||
75 | * Get the types of errors that will be logged |
||
76 | * |
||
77 | * @return int Binary set of E_* constants |
||
78 | */ |
||
79 | 22 | public function getLoggedErrorTypes() |
|
83 | |||
84 | /** |
||
85 | * Log these types of errors or exceptions |
||
86 | * |
||
87 | * @param int $type E_* contants as binary set |
||
88 | */ |
||
89 | 66 | protected function logUncaughtErrors($type) |
|
103 | |||
104 | |||
105 | /** |
||
106 | * Use the global error handler to convert E_USER_ERROR and E_RECOVERABLE_ERROR to an ErrorException |
||
107 | */ |
||
108 | 28 | public function converErrorsToExceptions() |
|
113 | |||
114 | |||
115 | /** |
||
116 | * Use the global error handler |
||
117 | */ |
||
118 | 84 | protected function initErrorHandler() |
|
124 | |||
125 | /** |
||
126 | * Uncaught error handler |
||
127 | * @ignore |
||
128 | * |
||
129 | * @param int $type |
||
130 | * @param string $message |
||
131 | * @param string $file |
||
132 | * @param int $line |
||
133 | * @param array $context |
||
134 | */ |
||
135 | 52 | public function handleError($type, $message, $file, $line, $context) |
|
153 | } |
||
154 | |||
155 |
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@return
doc comment to communicate to implementors of these methods what they are expected to return.