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 | 8 | 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 | * @deprecated use appendHandler and prependHandler instead |
||
| 50 | */ |
||
| 51 | 10 | public function pushHandler($handler) |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Adds a handler to be executed as last. |
||
| 58 | * |
||
| 59 | * @throws InvalidArgumentException If argument is not callable or instance of HandlerInterface |
||
| 60 | * @param Callable|HandlerInterface $handler |
||
| 61 | * @return Run |
||
| 62 | */ |
||
| 63 | 1 | public function appendHandler($handler) |
|
| 64 | { |
||
| 65 | 1 | array_unshift($this->handlerStack, $this->resolveHandler($handler)); |
|
| 66 | 1 | return $this; |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Adds a handler to be executed as first. |
||
| 71 | * |
||
| 72 | * @throws InvalidArgumentException If argument is not callable or instance of HandlerInterface |
||
| 73 | * @param Callable|HandlerInterface $handler |
||
| 74 | * @return Run |
||
| 75 | */ |
||
| 76 | 7 | 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 | 8 | private function resolveHandler($handler) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Removes the last handler in the stack and returns it. |
||
| 107 | * As implementation is using stack, the last element added to the stack |
||
| 108 | * is executed as first. |
||
| 109 | * |
||
| 110 | * Returns null if there"s nothing else to pop. |
||
| 111 | * @return null|HandlerInterface |
||
| 112 | */ |
||
| 113 | 1 | public function popHandler() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Removes the first handler in the stack and returns it. |
||
| 120 | * As implementation is using stack, the first element added to the stack |
||
| 121 | * is executed as last. |
||
| 122 | * |
||
| 123 | * Returns null if there"s nothing else to shift. |
||
| 124 | * @return null|HandlerInterface |
||
| 125 | */ |
||
| 126 | public function shiftHandler() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns an array with all handlers, in the |
||
| 133 | * order they were added to the stack. |
||
| 134 | * |
||
| 135 | * As stack implementation is used, handlers are executed from the end of that list |
||
| 136 | * (from the top of the stack). |
||
| 137 | * |
||
| 138 | * You can use: |
||
| 139 | * - appendHandler to add handler to be executed as last (add to the top of the stack) |
||
| 140 | * - prependHandler to add the handler to be executed as first (add to the bottom of the stack) |
||
| 141 | * |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | 2 | public function getHandlers() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Clears all handlers in the handlerStack, including |
||
| 151 | * the default PrettyPage handler. |
||
| 152 | * @return Run |
||
| 153 | */ |
||
| 154 | 1 | public function clearHandlers() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @param \Throwable $exception |
||
| 162 | * @return Inspector |
||
| 163 | */ |
||
| 164 | 5 | private function getInspector($exception) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Registers this instance as an error handler. |
||
| 171 | * @return Run |
||
| 172 | */ |
||
| 173 | 6 | public function register() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Unregisters all handlers registered by this Whoops\Run instance |
||
| 195 | * @return Run |
||
| 196 | */ |
||
| 197 | 1 | public function unregister() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Should Whoops allow Handlers to force the script to quit? |
||
| 211 | * @param bool|int $exit |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | 8 | public function allowQuit($exit = null) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Silence particular errors in particular files |
||
| 225 | * @param array|string $patterns List or a single regex pattern to match |
||
| 226 | * @param int $levels Defaults to E_STRICT | E_DEPRECATED |
||
| 227 | * @return \Whoops\Run |
||
| 228 | */ |
||
| 229 | 1 | public function silenceErrorsInPaths($patterns, $levels = 10240) |
|
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * Returns an array with silent errors in path configuration |
||
| 249 | * |
||
| 250 | * @return array |
||
| 251 | */ |
||
| 252 | public function getSilenceErrorsInPaths() |
||
| 256 | |||
| 257 | /* |
||
| 258 | * Should Whoops send HTTP error code to the browser if possible? |
||
| 259 | * Whoops will by default send HTTP code 500, but you may wish to |
||
| 260 | * use 502, 503, or another 5xx family code. |
||
| 261 | * |
||
| 262 | * @param bool|int $code |
||
| 263 | * @return int|false |
||
| 264 | */ |
||
| 265 | 8 | public function sendHttpCode($code = null) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Should Whoops push output directly to the client? |
||
| 290 | * If this is false, output will be returned by handleException |
||
| 291 | * @param bool|int $send |
||
| 292 | * @return bool |
||
| 293 | */ |
||
| 294 | 7 | public function writeToOutput($send = null) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Handles an exception, ultimately generating a Whoops error |
||
| 305 | * page. |
||
| 306 | * |
||
| 307 | * @param \Throwable $exception |
||
| 308 | * @return string Output generated by handlers |
||
| 309 | */ |
||
| 310 | 9 | public function handleException($exception) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Converts generic PHP errors to \ErrorException |
||
| 386 | * instances, before passing them off to be handled. |
||
| 387 | * |
||
| 388 | * This method MUST be compatible with set_error_handler. |
||
| 389 | * |
||
| 390 | * @param int $level |
||
| 391 | * @param string $message |
||
| 392 | * @param string $file |
||
| 393 | * @param int $line |
||
| 394 | * |
||
| 395 | * @return bool |
||
| 396 | * @throws ErrorException |
||
| 397 | */ |
||
| 398 | 5 | public function handleError($level, $message, $file = null, $line = null) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Special case to deal with Fatal errors and the like. |
||
| 430 | */ |
||
| 431 | public function handleShutdown() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * In certain scenarios, like in shutdown handler, we can not throw exceptions |
||
| 454 | * @var bool |
||
| 455 | */ |
||
| 456 | private $canThrowExceptions = true; |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Echo something to the browser |
||
| 460 | * @param string $output |
||
| 461 | * @return $this |
||
| 462 | */ |
||
| 463 | 5 | private function writeToOutputNow($output) |
|
| 475 | } |
||
| 476 |