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 | 4 | public function __construct(SystemFacade $system = null) |
|
| 42 | |||
| 43 | /** |
||
| 44 | * Pushes a handler to the end of the stack |
||
| 45 | * |
||
| 46 | * @throws InvalidArgumentException If argument is not callable or instance of HandlerInterface |
||
| 47 | * @param Callable|HandlerInterface $handler |
||
| 48 | * @return Run |
||
| 49 | */ |
||
| 50 | 8 | public function pushHandler($handler) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Removes the last handler in the stack and returns it. |
||
| 69 | * Returns null if there"s nothing else to pop. |
||
| 70 | * @return null|HandlerInterface |
||
| 71 | */ |
||
| 72 | 1 | public function popHandler() |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Returns an array with all handlers, in the |
||
| 79 | * order they were added to the stack. |
||
| 80 | * @return array |
||
| 81 | */ |
||
| 82 | 2 | public function getHandlers() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Clears all handlers in the handlerStack, including |
||
| 89 | * the default PrettyPage handler. |
||
| 90 | * @return Run |
||
| 91 | */ |
||
| 92 | 1 | public function clearHandlers() |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @param \Throwable $exception |
||
| 100 | * @return Inspector |
||
| 101 | */ |
||
| 102 | 1 | private function getInspector($exception) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Registers this instance as an error handler. |
||
| 109 | * @return Run |
||
| 110 | */ |
||
| 111 | 4 | public function register() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Unregisters all handlers registered by this Whoops\Run instance |
||
| 133 | * @return Run |
||
| 134 | */ |
||
| 135 | 1 | public function unregister() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Should Whoops allow Handlers to force the script to quit? |
||
| 149 | * @param bool|int $exit |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | 4 | public function allowQuit($exit = null) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Silence particular errors in particular files |
||
| 163 | * @param array|string $patterns List or a single regex pattern to match |
||
| 164 | * @param int $levels Defaults to E_STRICT | E_DEPRECATED |
||
| 165 | * @return \Whoops\Run |
||
| 166 | */ |
||
| 167 | 1 | public function silenceErrorsInPaths($patterns, $levels = 10240) |
|
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * Returns an array with silent errors in path configuration |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | public function getSilenceErrorsInPaths() |
||
| 194 | |||
| 195 | /* |
||
| 196 | * Should Whoops send HTTP error code to the browser if possible? |
||
| 197 | * Whoops will by default send HTTP code 500, but you may wish to |
||
| 198 | * use 502, 503, or another 5xx family code. |
||
| 199 | * |
||
| 200 | * @param bool|int $code |
||
| 201 | * @return int|false |
||
| 202 | */ |
||
| 203 | 4 | public function sendHttpCode($code = null) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Should Whoops push output directly to the client? |
||
| 228 | * If this is false, output will be returned by handleException |
||
| 229 | * @param bool|int $send |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | 3 | public function writeToOutput($send = null) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Handles an exception, ultimately generating a Whoops error |
||
| 243 | * page. |
||
| 244 | * |
||
| 245 | * @param \Throwable $exception |
||
| 246 | * @return string Output generated by handlers |
||
| 247 | */ |
||
| 248 | 5 | public function handleException($exception) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Converts generic PHP errors to \ErrorException |
||
| 323 | * instances, before passing them off to be handled. |
||
| 324 | * |
||
| 325 | * This method MUST be compatible with set_error_handler. |
||
| 326 | * |
||
| 327 | * @param int $level |
||
| 328 | * @param string $message |
||
| 329 | * @param string $file |
||
| 330 | * @param int $line |
||
| 331 | * |
||
| 332 | * @return bool |
||
| 333 | * @throws ErrorException |
||
| 334 | */ |
||
| 335 | 5 | public function handleError($level, $message, $file = null, $line = null) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Special case to deal with Fatal errors and the like. |
||
| 367 | */ |
||
| 368 | public function handleShutdown() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * In certain scenarios, like in shutdown handler, we can not throw exceptions |
||
| 390 | * @var bool |
||
| 391 | */ |
||
| 392 | private $canThrowExceptions = true; |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Echo something to the browser |
||
| 396 | * @param string $output |
||
| 397 | * @return $this |
||
| 398 | */ |
||
| 399 | 1 | private function writeToOutputNow($output) |
|
| 411 | } |
||
| 412 |