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) |
|
| 39 | { |
||
| 40 | 4 | $this->system = $system ?: new SystemFacade; |
|
| 41 | 4 | } |
|
| 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) |
|
| 103 | { |
||
| 104 | 1 | return new Inspector($exception); |
|
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Registers this instance as an error handler. |
||
| 109 | * @return Run |
||
| 110 | */ |
||
| 111 | 4 | public function register() |
|
| 112 | { |
||
| 113 | 4 | if (!$this->isRegistered) { |
|
| 114 | // Workaround PHP bug 42098 |
||
| 115 | // https://bugs.php.net/bug.php?id=42098 |
||
| 116 | 4 | class_exists("\\Whoops\\Exception\\ErrorException"); |
|
| 117 | 4 | class_exists("\\Whoops\\Exception\\FrameCollection"); |
|
| 118 | 4 | class_exists("\\Whoops\\Exception\\Frame"); |
|
| 119 | 4 | class_exists("\\Whoops\\Exception\\Inspector"); |
|
| 120 | |||
| 121 | 4 | $this->system->setErrorHandler([$this, self::ERROR_HANDLER]); |
|
| 122 | 4 | $this->system->setExceptionHandler([$this, self::EXCEPTION_HANDLER]); |
|
| 123 | 4 | $this->system->registerShutdownFunction([$this, self::SHUTDOWN_HANDLER]); |
|
| 124 | |||
| 125 | 4 | $this->isRegistered = true; |
|
| 126 | 4 | } |
|
| 127 | |||
| 128 | 4 | return $this; |
|
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Unregisters all handlers registered by this Whoops\Run instance |
||
| 133 | * @return Run |
||
| 134 | */ |
||
| 135 | 1 | public function unregister() |
|
| 136 | { |
||
| 137 | 1 | if ($this->isRegistered) { |
|
| 138 | 1 | $this->system->restoreExceptionHandler(); |
|
| 139 | 1 | $this->system->restoreErrorHandler(); |
|
| 140 | |||
| 141 | 1 | $this->isRegistered = false; |
|
| 142 | 1 | } |
|
| 143 | |||
| 144 | 1 | return $this; |
|
| 145 | } |
||
| 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 | 3 | 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) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Converts generic PHP errors to \ErrorException |
||
| 314 | * instances, before passing them off to be handled. |
||
| 315 | * |
||
| 316 | * This method MUST be compatible with set_error_handler. |
||
| 317 | * |
||
| 318 | * @param int $level |
||
| 319 | * @param string $message |
||
| 320 | * @param string $file |
||
| 321 | * @param int $line |
||
| 322 | * |
||
| 323 | * @return bool |
||
| 324 | * @throws ErrorException |
||
| 325 | */ |
||
| 326 | 5 | public function handleError($level, $message, $file = null, $line = null) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Special case to deal with Fatal errors and the like. |
||
| 357 | */ |
||
| 358 | public function handleShutdown() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * In certain scenarios, like in shutdown handler, we can not throw exceptions |
||
| 380 | * @var bool |
||
| 381 | */ |
||
| 382 | private $canThrowExceptions = true; |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Echo something to the browser |
||
| 386 | * @param string $output |
||
| 387 | * @return $this |
||
| 388 | */ |
||
| 389 | 1 | private function writeToOutputNow($output) |
|
| 401 | } |
||
| 402 |