Complex classes like Run 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 Run, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | final class Run implements RunInterface |
||
| 19 | { |
||
| 20 | private $isRegistered; |
||
| 21 | private $allowQuit = true; |
||
| 22 | private $sendOutput = true; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var integer|false |
||
| 26 | */ |
||
| 27 | private $sendHttpCode = 500; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var HandlerInterface[] |
||
| 31 | */ |
||
| 32 | private $handlerStack = []; |
||
| 33 | |||
| 34 | private $silencedPatterns = []; |
||
| 35 | |||
| 36 | private $system; |
||
| 37 | |||
| 38 | 6 | public function __construct(SystemFacade $system = null) |
|
| 42 | |||
| 43 | /** |
||
| 44 | * Explicitly request your handler runs as the last of all currently registered handlers |
||
| 45 | */ |
||
| 46 | public function appendHandler($handler) |
||
| 51 | 3 | ||
| 52 | /** |
||
| 53 | 3 | * Explicitly request your handler runs as the first of all currently registered handlers |
|
| 54 | */ |
||
| 55 | public function prependHandler($handler) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Register your handler as the last of all currently registered handlers. |
||
| 62 | * Prefer using appendHandler and prependHandler for clarity. |
||
| 63 | * |
||
| 64 | * @throws InvalidArgumentException If argument is not callable or instance of HandlerInterface |
||
| 65 | * @param Callable|HandlerInterface $handler |
||
| 66 | * @return Run |
||
| 67 | */ |
||
| 68 | public function pushHandler($handler) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * See removeFirstHandler and removeLastHandler |
||
| 76 | 6 | * @return null|HandlerInterface |
|
| 77 | */ |
||
| 78 | 6 | public function popHandler() |
|
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * Removes the first handler |
||
| 86 | */ |
||
| 87 | public function removeFirstHandler() |
||
| 91 | 6 | ||
| 92 | /** |
||
| 93 | * Removes the last handler |
||
| 94 | */ |
||
| 95 | 6 | public function removeLastHandler() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Returns an array with all handlers, in the |
||
| 102 | 6 | * order they were added to the stack. |
|
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | public function getHandlers() |
||
| 109 | |||
| 110 | 1 | /** |
|
| 111 | * Clears all handlers in the handlerStack, including |
||
| 112 | 1 | * the default PrettyPage handler. |
|
| 113 | * @return Run |
||
| 114 | */ |
||
| 115 | public function clearHandlers() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param \Throwable $exception |
||
| 123 | * @return Inspector |
||
| 124 | */ |
||
| 125 | private function getInspector($exception) |
||
| 129 | |||
| 130 | 2 | /** |
|
| 131 | * Registers this instance as an error handler. |
||
| 132 | 2 | * @return Run |
|
| 133 | */ |
||
| 134 | public function register() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Unregisters all handlers registered by this Whoops\Run instance |
||
| 156 | * @return Run |
||
| 157 | */ |
||
| 158 | public function unregister() |
||
| 169 | 6 | ||
| 170 | 6 | /** |
|
| 171 | 6 | * Should Whoops allow Handlers to force the script to quit? |
|
| 172 | * @param bool|int $exit |
||
| 173 | 6 | * @return bool |
|
| 174 | 6 | */ |
|
| 175 | public function allowQuit($exit = null) |
||
| 183 | 1 | ||
| 184 | /** |
||
| 185 | 1 | * Silence particular errors in particular files |
|
| 186 | 1 | * @param array|string $patterns List or a single regex pattern to match |
|
| 187 | 1 | * @param int $levels Defaults to E_STRICT | E_DEPRECATED |
|
| 188 | * @return \Whoops\Run |
||
| 189 | 1 | */ |
|
| 190 | 1 | public function silenceErrorsInPaths($patterns, $levels = 10240) |
|
| 206 | 6 | ||
| 207 | |||
| 208 | /** |
||
| 209 | * Returns an array with silent errors in path configuration |
||
| 210 | * |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | public function getSilenceErrorsInPaths() |
||
| 217 | 1 | ||
| 218 | 1 | /* |
|
| 219 | 1 | * Should Whoops send HTTP error code to the browser if possible? |
|
| 220 | 1 | * Whoops will by default send HTTP code 500, but you may wish to |
|
| 221 | * use 502, 503, or another 5xx family code. |
||
| 222 | 1 | * |
|
| 223 | 1 | * @param bool|int $code |
|
| 224 | 1 | * @return int|false |
|
| 225 | 1 | */ |
|
| 226 | public function sendHttpCode($code = null) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Should Whoops push output directly to the client? |
||
| 251 | 6 | * If this is false, output will be returned by handleException |
|
| 252 | * @param bool|int $send |
||
| 253 | 6 | * @return bool |
|
| 254 | 4 | */ |
|
| 255 | public function writeToOutput($send = null) |
||
| 263 | 1 | ||
| 264 | /** |
||
| 265 | 2 | * Handles an exception, ultimately generating a Whoops error |
|
| 266 | 1 | * page. |
|
| 267 | 1 | * |
|
| 268 | 1 | * @param \Throwable $exception |
|
| 269 | * @return string Output generated by handlers |
||
| 270 | */ |
||
| 271 | 1 | public function handleException($exception) |
|
| 344 | |||
| 345 | 6 | /** |
|
| 346 | * Converts generic PHP errors to \ErrorException |
||
| 347 | * instances, before passing them off to be handled. |
||
| 348 | * |
||
| 349 | * This method MUST be compatible with set_error_handler. |
||
| 350 | * |
||
| 351 | * @param int $level |
||
| 352 | * @param string $message |
||
| 353 | * @param string $file |
||
| 354 | * @param int $line |
||
| 355 | * |
||
| 356 | * @return bool |
||
| 357 | 6 | * @throws ErrorException |
|
| 358 | 6 | */ |
|
| 359 | public function handleError($level, $message, $file = null, $line = null) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Special case to deal with Fatal errors and the like. |
||
| 391 | */ |
||
| 392 | public function handleShutdown() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * In certain scenarios, like in shutdown handler, we can not throw exceptions |
||
| 415 | * @var bool |
||
| 416 | */ |
||
| 417 | private $canThrowExceptions = true; |
||
| 418 | |||
| 419 | private function resolveHandler($handler) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Echo something to the browser |
||
| 437 | * @param string $output |
||
| 438 | * @return $this |
||
| 439 | */ |
||
| 440 | private function writeToOutputNow($output) |
||
| 452 | } |
||
| 453 |