Complex classes like ErrorHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ErrorHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class ErrorHandler implements LoggerAwareInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var LoggerInterface |
||
20 | */ |
||
21 | protected $logger; |
||
22 | |||
23 | /** |
||
24 | * @var \Exception|\Error |
||
25 | */ |
||
26 | protected $error; |
||
27 | |||
28 | /** |
||
29 | * @var callable|false |
||
30 | */ |
||
31 | protected $chainedErrorHandler; |
||
32 | |||
33 | /** |
||
34 | * @var boolean |
||
35 | */ |
||
36 | protected $registeredShutdown = false; |
||
37 | |||
38 | /** |
||
39 | * Convert fatal errors to exceptions |
||
40 | * @var boolean |
||
41 | */ |
||
42 | protected $convertFatalErrors = false; |
||
43 | |||
44 | /** |
||
45 | * Log the following error types (in addition to caugth errors) |
||
46 | * @var int |
||
47 | */ |
||
48 | protected $logErrorTypes = 0; |
||
49 | |||
50 | /** |
||
51 | * A string which reserves memory that can be used to log the error in case of an out of memory fatal error |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $reservedMemory; |
||
55 | |||
56 | /** |
||
57 | * @var callback |
||
58 | */ |
||
59 | protected $onFatalError; |
||
60 | |||
61 | |||
62 | /** |
||
63 | * Set the logger for logging errors |
||
64 | * |
||
65 | * @param LoggerInterface $logger |
||
66 | */ |
||
67 | 106 | public function setLogger(LoggerInterface $logger) |
|
71 | |||
72 | /** |
||
73 | * Set the logger for logging errors |
||
74 | * |
||
75 | * @return LoggerInterface |
||
76 | */ |
||
77 | 61 | public function getLogger() |
|
85 | |||
86 | /** |
||
87 | * Log an error or exception |
||
88 | * |
||
89 | * @param \Exception|\Error $error |
||
90 | */ |
||
91 | 59 | public function log($error) |
|
104 | |||
105 | /** |
||
106 | * Log an error |
||
107 | * |
||
108 | * @param \Error|\ErrorException $error |
||
109 | */ |
||
110 | 49 | protected function logError($error) |
|
128 | |||
129 | /** |
||
130 | * Log an exception |
||
131 | * |
||
132 | * @param \Exception $error |
||
133 | */ |
||
134 | 6 | protected function logException(\Exception $error) |
|
145 | |||
146 | |||
147 | /** |
||
148 | * Get the caught error |
||
149 | * |
||
150 | * @return \Throwable|\Exception|\Error |
||
151 | */ |
||
152 | 3 | public function getError() |
|
156 | |||
157 | /** |
||
158 | * Get the error handler that has been replaced. |
||
159 | * |
||
160 | * @return callable|false|null |
||
161 | */ |
||
162 | 2 | public function getChainedErrorHandler() |
|
166 | |||
167 | /** |
||
168 | * Get the types of errors that will be logged |
||
169 | * |
||
170 | * @return int Binary set of E_* constants |
||
171 | */ |
||
172 | 22 | public function getLoggedErrorTypes() |
|
176 | |||
177 | |||
178 | /** |
||
179 | * Run middleware action |
||
180 | * |
||
181 | * @param ServerRequestInterface $request |
||
182 | * @param ResponseInterface $response |
||
183 | * @param callback $next |
||
184 | * @return ResponseInterface |
||
185 | */ |
||
186 | 9 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next) |
|
208 | |||
209 | /** |
||
210 | * Handle caught error |
||
211 | * |
||
212 | * @param ServerRequestInterface $request |
||
213 | * @param ResponseInterface $response |
||
214 | * @return ResponseInterface |
||
215 | */ |
||
216 | 5 | protected function errorResponse(ServerRequestInterface $request, ResponseInterface $response) |
|
223 | |||
224 | |||
225 | /** |
||
226 | * Use the global error handler to convert E_USER_ERROR and E_RECOVERABLE_ERROR to an ErrorException |
||
227 | */ |
||
228 | 28 | public function converErrorsToExceptions() |
|
233 | |||
234 | /** |
||
235 | * Also log these types of errors in addition to caught errors and exceptions |
||
236 | * |
||
237 | * @param int $errorTypes E_* contants as binary set |
||
238 | */ |
||
239 | 62 | public function alsoLog($errorTypes) |
|
254 | |||
255 | /** |
||
256 | * Set a callback for when the script dies because of a fatal, non-catchable error. |
||
257 | * The callback should have an `ErrorException` as only argument. |
||
258 | * |
||
259 | * @param callable $callback |
||
260 | * @param boolean $clearOutput Clear the output buffer before calling the callback |
||
261 | */ |
||
262 | 4 | public function onFatalError($callback, $clearOutput = false) |
|
273 | |||
274 | /** |
||
275 | * Use the global error handler |
||
276 | */ |
||
277 | 70 | protected function initErrorHandler() |
|
283 | |||
284 | /** |
||
285 | * Uncaught error handler |
||
286 | * @ignore |
||
287 | * |
||
288 | * @param int $type |
||
289 | * @param string $message |
||
290 | * @param string $file |
||
291 | * @param int $line |
||
292 | * @param array $context |
||
293 | */ |
||
294 | 52 | public function handleError($type, $message, $file, $line, $context) |
|
312 | |||
313 | /** |
||
314 | * Reserve memory for shutdown function in case of out of memory |
||
315 | */ |
||
316 | 30 | protected function reserveMemory() |
|
320 | |||
321 | /** |
||
322 | * Register a shutdown function |
||
323 | */ |
||
324 | 30 | protected function initShutdownFunction() |
|
333 | |||
334 | /** |
||
335 | * Called when the script has ends |
||
336 | * @ignore |
||
337 | */ |
||
338 | 16 | public function shutdownFunction() |
|
359 | |||
360 | |||
361 | /** |
||
362 | * Get the log level for an error code |
||
363 | * |
||
364 | * @param int $code E_* error code |
||
365 | * @return string |
||
366 | */ |
||
367 | 55 | protected function getLogLevel($code = null) |
|
394 | |||
395 | /** |
||
396 | * Turn an error code into a string |
||
397 | * |
||
398 | * @param int $code |
||
399 | * @return string |
||
400 | */ |
||
401 | 49 | protected function codeToString($code) |
|
433 | |||
434 | |||
435 | /** |
||
436 | * Clear and destroy all the output buffers |
||
437 | * @codeCoverageIgnore |
||
438 | */ |
||
439 | protected function clearOutputBuffer() |
||
445 | |||
446 | /** |
||
447 | * Wrapper method for `error_reporting` |
||
448 | * @codeCoverageIgnore |
||
449 | * |
||
450 | * @return int |
||
451 | */ |
||
452 | protected function errorReporting() |
||
456 | |||
457 | /** |
||
458 | * Wrapper method for `error_get_last` |
||
459 | * @codeCoverageIgnore |
||
460 | * |
||
461 | * @return array |
||
462 | */ |
||
463 | protected function errorGetLast() |
||
467 | |||
468 | /** |
||
469 | * Wrapper method for `set_error_handler` |
||
470 | * @codeCoverageIgnore |
||
471 | * |
||
472 | * @param callable $callback |
||
473 | * @param int $error_types |
||
474 | * @return callable|null |
||
475 | */ |
||
476 | protected function setErrorHandler($callback, $error_types = E_ALL) |
||
480 | |||
481 | /** |
||
482 | * Wrapper method for `register_shutdown_function` |
||
483 | * @codeCoverageIgnore |
||
484 | * |
||
485 | * @param callable $callback |
||
486 | */ |
||
487 | protected function registerShutdownFunction($callback) |
||
491 | } |
||
492 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.