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 $handlerQueue = []; |
||
| 33 | |||
| 34 | private $silencedPatterns = []; |
||
| 35 | |||
| 36 | private $system; |
||
| 37 | |||
| 38 | 6 | public function __construct(SystemFacade $system = null) |
|
| 42 | |||
| 43 | /** |
||
| 44 | * Prepends a handler to the start of the queue |
||
| 45 | * |
||
| 46 | * @throws InvalidArgumentException If argument is not callable or instance of HandlerInterface |
||
| 47 | * @param Callable|HandlerInterface $handler |
||
| 48 | * @return Run |
||
| 49 | * @deprecated use appendHandler and prependHandler instead |
||
| 50 | */ |
||
| 51 | 3 | public function pushHandler($handler) |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Appends a handler to the end of the queue |
||
| 58 | * |
||
| 59 | * @throws InvalidArgumentException If argument is not callable or instance of HandlerInterface |
||
| 60 | * @param Callable|HandlerInterface $handler |
||
| 61 | * @return Run |
||
| 62 | */ |
||
| 63 | public function appendHandler($handler) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Prepends a handler to the start of the queue |
||
| 71 | * |
||
| 72 | * @throws InvalidArgumentException If argument is not callable or instance of HandlerInterface |
||
| 73 | * @param Callable|HandlerInterface $handler |
||
| 74 | * @return Run |
||
| 75 | */ |
||
| 76 | 6 | public function prependHandler($handler) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Create a CallbackHandler from callable and throw if handler is invalid |
||
| 84 | * |
||
| 85 | * @throws InvalidArgumentException If argument is not callable or instance of HandlerInterface |
||
| 86 | * @param Callable|HandlerInterface $handler |
||
| 87 | * @return HandlerInterface |
||
| 88 | */ |
||
| 89 | 6 | private function resolveHandler($handler) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Removes the last handler in the queue and returns it. |
||
| 107 | * Returns null if there"s nothing else to pop. |
||
| 108 | * @return null|HandlerInterface |
||
| 109 | */ |
||
| 110 | 1 | public function popHandler() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Removes the first handler in the queue and returns it. |
||
| 117 | * Returns null if there"s nothing else to shift. |
||
| 118 | * @return null|HandlerInterface |
||
| 119 | */ |
||
| 120 | public function shiftHandler() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Returns an array with all handlers, in the |
||
| 127 | * order they were added to the queue. |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | 2 | public function getHandlers() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Clears all handlers in the handlerQueue, including |
||
| 137 | * the default PrettyPage handler. |
||
| 138 | * @return Run |
||
| 139 | */ |
||
| 140 | 1 | public function clearHandlers() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @param \Throwable $exception |
||
| 148 | * @return Inspector |
||
| 149 | */ |
||
| 150 | 3 | private function getInspector($exception) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Registers this instance as an error handler. |
||
| 157 | * @return Run |
||
| 158 | */ |
||
| 159 | 6 | public function register() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Unregisters all handlers registered by this Whoops\Run instance |
||
| 181 | * @return Run |
||
| 182 | */ |
||
| 183 | 1 | public function unregister() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Should Whoops allow Handlers to force the script to quit? |
||
| 197 | * @param bool|int $exit |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | 6 | public function allowQuit($exit = null) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Silence particular errors in particular files |
||
| 211 | * @param array|string $patterns List or a single regex pattern to match |
||
| 212 | * @param int $levels Defaults to E_STRICT | E_DEPRECATED |
||
| 213 | * @return \Whoops\Run |
||
| 214 | */ |
||
| 215 | 1 | public function silenceErrorsInPaths($patterns, $levels = 10240) |
|
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * Returns an array with silent errors in path configuration |
||
| 235 | * |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | public function getSilenceErrorsInPaths() |
||
| 242 | |||
| 243 | /* |
||
| 244 | * Should Whoops send HTTP error code to the browser if possible? |
||
| 245 | * Whoops will by default send HTTP code 500, but you may wish to |
||
| 246 | * use 502, 503, or another 5xx family code. |
||
| 247 | * |
||
| 248 | * @param bool|int $code |
||
| 249 | * @return int|false |
||
| 250 | */ |
||
| 251 | 6 | public function sendHttpCode($code = null) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Should Whoops push output directly to the client? |
||
| 276 | * If this is false, output will be returned by handleException |
||
| 277 | * @param bool|int $send |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | 5 | public function writeToOutput($send = null) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Handles an exception, ultimately generating a Whoops error |
||
| 291 | * page. |
||
| 292 | * |
||
| 293 | * @param \Throwable $exception |
||
| 294 | * @return string Output generated by handlers |
||
| 295 | */ |
||
| 296 | 7 | public function handleException($exception) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Converts generic PHP errors to \ErrorException |
||
| 370 | * instances, before passing them off to be handled. |
||
| 371 | * |
||
| 372 | * This method MUST be compatible with set_error_handler. |
||
| 373 | * |
||
| 374 | * @param int $level |
||
| 375 | * @param string $message |
||
| 376 | * @param string $file |
||
| 377 | * @param int $line |
||
| 378 | * |
||
| 379 | * @return bool |
||
| 380 | * @throws ErrorException |
||
| 381 | */ |
||
| 382 | 5 | public function handleError($level, $message, $file = null, $line = null) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Special case to deal with Fatal errors and the like. |
||
| 414 | */ |
||
| 415 | public function handleShutdown() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * In certain scenarios, like in shutdown handler, we can not throw exceptions |
||
| 438 | * @var bool |
||
| 439 | */ |
||
| 440 | private $canThrowExceptions = true; |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Echo something to the browser |
||
| 444 | * @param string $output |
||
| 445 | * @return $this |
||
| 446 | */ |
||
| 447 | 3 | private function writeToOutputNow($output) |
|
| 459 | } |
||
| 460 |