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) |
||
| 47 | { |
||
| 48 | array_unshift($this->handlerStack, $this->resolveHandler($handler)); |
||
| 49 | return $this; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Explicitly request your handler runs as the first of all currently registered handlers |
||
| 54 | */ |
||
| 55 | public function prependHandler($handler) |
||
| 56 | { |
||
| 57 | return $this->pushHandler($handler); |
||
| 58 | } |
||
| 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 | 10 | public function pushHandler($handler) |
|
| 69 | { |
||
| 70 | 10 | $this->handlerStack[] = $this->resolveHandler($handler); |
|
| 71 | 9 | return $this; |
|
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * See removeFirstHandler and removeLastHandler |
||
| 76 | * @return null|HandlerInterface |
||
| 77 | */ |
||
| 78 | 1 | public function popHandler() |
|
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * Removes the first handler |
||
| 86 | */ |
||
| 87 | 1 | public function removeFirstHandler() |
|
| 88 | { |
||
| 89 | 1 | array_pop($this->handlerStack); |
|
| 90 | 1 | } |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Removes the last handler |
||
| 94 | */ |
||
| 95 | 1 | public function removeLastHandler() |
|
| 96 | { |
||
| 97 | 1 | array_shift($this->handlerStack); |
|
| 98 | 1 | } |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Returns an array with all handlers, in the |
||
| 102 | * order they were added to the stack. |
||
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | 3 | public function getHandlers() |
|
| 106 | { |
||
| 107 | 3 | return $this->handlerStack; |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Clears all handlers in the handlerStack, including |
||
| 112 | * the default PrettyPage handler. |
||
| 113 | * @return Run |
||
| 114 | */ |
||
| 115 | 1 | public function clearHandlers() |
|
| 116 | { |
||
| 117 | 1 | $this->handlerStack = []; |
|
| 118 | 1 | return $this; |
|
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param \Throwable $exception |
||
| 123 | * @return Inspector |
||
| 124 | */ |
||
| 125 | 3 | private function getInspector($exception) |
|
| 126 | { |
||
| 127 | 3 | return new Inspector($exception); |
|
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Registers this instance as an error handler. |
||
| 132 | * @return Run |
||
| 133 | */ |
||
| 134 | 6 | public function register() |
|
| 135 | { |
||
| 136 | 6 | if (!$this->isRegistered) { |
|
| 137 | // Workaround PHP bug 42098 |
||
| 138 | // https://bugs.php.net/bug.php?id=42098 |
||
| 139 | 6 | class_exists("\\Whoops\\Exception\\ErrorException"); |
|
| 140 | 6 | class_exists("\\Whoops\\Exception\\FrameCollection"); |
|
| 141 | 6 | class_exists("\\Whoops\\Exception\\Frame"); |
|
| 142 | 6 | class_exists("\\Whoops\\Exception\\Inspector"); |
|
| 143 | |||
| 144 | 6 | $this->system->setErrorHandler([$this, self::ERROR_HANDLER]); |
|
| 145 | 6 | $this->system->setExceptionHandler([$this, self::EXCEPTION_HANDLER]); |
|
| 146 | 6 | $this->system->registerShutdownFunction([$this, self::SHUTDOWN_HANDLER]); |
|
| 147 | |||
| 148 | 6 | $this->isRegistered = true; |
|
| 149 | 6 | } |
|
| 150 | |||
| 151 | 6 | return $this; |
|
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Unregisters all handlers registered by this Whoops\Run instance |
||
| 156 | * @return Run |
||
| 157 | */ |
||
| 158 | 1 | public function unregister() |
|
| 159 | { |
||
| 160 | 1 | if ($this->isRegistered) { |
|
| 161 | 1 | $this->system->restoreExceptionHandler(); |
|
| 162 | 1 | $this->system->restoreErrorHandler(); |
|
| 163 | |||
| 164 | 1 | $this->isRegistered = false; |
|
| 165 | 1 | } |
|
| 166 | |||
| 167 | 1 | return $this; |
|
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Should Whoops allow Handlers to force the script to quit? |
||
| 172 | * @param bool|int $exit |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | 6 | public function allowQuit($exit = null) |
|
| 176 | { |
||
| 177 | 6 | if (func_num_args() == 0) { |
|
| 178 | 3 | return $this->allowQuit; |
|
| 179 | } |
||
| 180 | |||
| 181 | 6 | return $this->allowQuit = (bool) $exit; |
|
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Silence particular errors in particular files |
||
| 186 | * @param array|string $patterns List or a single regex pattern to match |
||
| 187 | * @param int $levels Defaults to E_STRICT | E_DEPRECATED |
||
| 188 | * @return \Whoops\Run |
||
| 189 | */ |
||
| 190 | 1 | public function silenceErrorsInPaths($patterns, $levels = 10240) |
|
| 191 | { |
||
| 192 | 1 | $this->silencedPatterns = array_merge( |
|
| 193 | 1 | $this->silencedPatterns, |
|
| 194 | 1 | array_map( |
|
| 195 | 1 | function ($pattern) use ($levels) { |
|
| 196 | return [ |
||
| 197 | 1 | "pattern" => $pattern, |
|
| 198 | 1 | "levels" => $levels, |
|
| 199 | 1 | ]; |
|
| 200 | 1 | }, |
|
| 201 | (array) $patterns |
||
| 202 | 1 | ) |
|
| 203 | 1 | ); |
|
| 204 | 1 | return $this; |
|
| 205 | } |
||
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * Returns an array with silent errors in path configuration |
||
| 210 | * |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | public function getSilenceErrorsInPaths() |
||
| 214 | { |
||
| 215 | return $this->silencedPatterns; |
||
| 216 | } |
||
| 217 | |||
| 218 | /* |
||
| 219 | * Should Whoops send HTTP error code to the browser if possible? |
||
| 220 | * Whoops will by default send HTTP code 500, but you may wish to |
||
| 221 | * use 502, 503, or another 5xx family code. |
||
| 222 | * |
||
| 223 | * @param bool|int $code |
||
| 224 | * @return int|false |
||
| 225 | */ |
||
| 226 | 6 | public function sendHttpCode($code = null) |
|
| 227 | { |
||
| 228 | 6 | if (func_num_args() == 0) { |
|
| 229 | 4 | return $this->sendHttpCode; |
|
| 230 | } |
||
| 231 | |||
| 232 | 3 | if (!$code) { |
|
| 233 | 1 | return $this->sendHttpCode = false; |
|
| 234 | } |
||
| 235 | |||
| 236 | 2 | if ($code === true) { |
|
| 237 | 1 | $code = 500; |
|
| 238 | 1 | } |
|
| 239 | |||
| 240 | 2 | if ($code < 400 || 600 <= $code) { |
|
| 241 | 1 | throw new InvalidArgumentException( |
|
| 242 | 1 | "Invalid status code '$code', must be 4xx or 5xx" |
|
| 243 | 1 | ); |
|
| 244 | } |
||
| 245 | |||
| 246 | 1 | return $this->sendHttpCode = $code; |
|
|
|
|||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Should Whoops push output directly to the client? |
||
| 251 | * If this is false, output will be returned by handleException |
||
| 252 | * @param bool|int $send |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | 5 | public function writeToOutput($send = null) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Handles an exception, ultimately generating a Whoops error |
||
| 266 | * page. |
||
| 267 | * |
||
| 268 | * @param \Throwable $exception |
||
| 269 | * @return string Output generated by handlers |
||
| 270 | */ |
||
| 271 | 7 | public function handleException($exception) |
|
| 272 | { |
||
| 273 | // Walk the registered handlers in the reverse order |
||
| 274 | // they were registered, and pass off the exception |
||
| 275 | 7 | $inspector = $this->getInspector($exception); |
|
| 276 | |||
| 277 | // Capture output produced while handling the exception, |
||
| 344 | |||
| 345 | /** |
||
| 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 | * @throws ErrorException |
||
| 358 | */ |
||
| 359 | 5 | 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 | 6 | private function resolveHandler($handler) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Echo something to the browser |
||
| 437 | * @param string $output |
||
| 438 | * @return $this |
||
| 439 | */ |
||
| 440 | 3 | private function writeToOutputNow($output) |
|
| 452 | } |
||
| 453 |