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 |
||
| 16 | class Run |
||
| 17 | { |
||
| 18 | const EXCEPTION_HANDLER = "handleException"; |
||
| 19 | const ERROR_HANDLER = "handleError"; |
||
| 20 | const SHUTDOWN_HANDLER = "handleShutdown"; |
||
| 21 | |||
| 22 | protected $isRegistered; |
||
| 23 | protected $allowQuit = true; |
||
| 24 | protected $sendOutput = true; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var integer|false |
||
| 28 | */ |
||
| 29 | protected $sendHttpCode = 500; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var HandlerInterface[] |
||
| 33 | */ |
||
| 34 | protected $handlerStack = array(); |
||
| 35 | |||
| 36 | protected $silencedPatterns = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Pushes a handler to the end of the stack |
||
| 40 | * |
||
| 41 | * @throws InvalidArgumentException If argument is not callable or instance of HandlerInterface |
||
| 42 | * @param Callable|HandlerInterface $handler |
||
| 43 | * @return Run |
||
| 44 | */ |
||
| 45 | 9 | public function pushHandler($handler) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Removes the last handler in the stack and returns it. |
||
| 64 | * Returns null if there"s nothing else to pop. |
||
| 65 | * @return null|HandlerInterface |
||
| 66 | */ |
||
| 67 | 1 | public function popHandler() |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Returns an array with all handlers, in the |
||
| 74 | * order they were added to the stack. |
||
| 75 | * @return array |
||
| 76 | */ |
||
| 77 | 2 | public function getHandlers() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Clears all handlers in the handlerStack, including |
||
| 84 | * the default PrettyPage handler. |
||
| 85 | * @return Run |
||
| 86 | */ |
||
| 87 | 1 | public function clearHandlers() |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @param mixed $exception |
||
| 95 | * @return Inspector |
||
| 96 | */ |
||
| 97 | 2 | protected function getInspector($exception) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Registers this instance as an error handler. |
||
| 104 | * @return Run |
||
| 105 | */ |
||
| 106 | 5 | public function register() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Unregisters all handlers registered by this Whoops\Run instance |
||
| 128 | * @return Run |
||
| 129 | */ |
||
| 130 | 1 | public function unregister() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Should Whoops allow Handlers to force the script to quit? |
||
| 144 | * @param bool|int $exit |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | 5 | public function allowQuit($exit = null) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Silence particular errors in particular files |
||
| 158 | * @param array|string $patterns List or a single regex pattern to match |
||
| 159 | * @param int $levels Defaults to E_STRICT | E_DEPRECATED |
||
| 160 | * @return \Whoops\Run |
||
| 161 | */ |
||
| 162 | 1 | public function silenceErrorsInPaths($patterns, $levels = 10240) |
|
| 178 | |||
| 179 | /* |
||
| 180 | * Should Whoops send HTTP error code to the browser if possible? |
||
| 181 | * Whoops will by default send HTTP code 500, but you may wish to |
||
| 182 | * use 502, 503, or another 5xx family code. |
||
| 183 | * |
||
| 184 | * @param bool|int $code |
||
| 185 | * @return int|false |
||
| 186 | */ |
||
| 187 | 4 | public function sendHttpCode($code = null) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Should Whoops push output directly to the client? |
||
| 212 | * If this is false, output will be returned by handleException |
||
| 213 | * @param bool|int $send |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | 4 | public function writeToOutput($send = null) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Handles an exception, ultimately generating a Whoops error |
||
| 227 | * page. |
||
| 228 | * |
||
| 229 | * @param mixed $exception |
||
| 230 | * @return string Output generated by handlers |
||
| 231 | */ |
||
| 232 | 6 | public function handleException($exception) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Converts generic PHP errors to \ErrorException |
||
| 296 | * instances, before passing them off to be handled. |
||
| 297 | * |
||
| 298 | * This method MUST be compatible with set_error_handler. |
||
| 299 | * |
||
| 300 | * @param int $level |
||
| 301 | * @param string $message |
||
| 302 | * @param string $file |
||
| 303 | * @param int $line |
||
| 304 | * |
||
| 305 | * @return bool |
||
| 306 | * @throws ErrorException |
||
| 307 | */ |
||
| 308 | 5 | public function handleError($level, $message, $file = null, $line = null) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Special case to deal with Fatal errors and the like. |
||
| 339 | */ |
||
| 340 | public function handleShutdown() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * In certain scenarios, like in shutdown handler, we can not throw exceptions |
||
| 362 | * @var bool |
||
| 363 | */ |
||
| 364 | private $canThrowExceptions = true; |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Echo something to the browser |
||
| 368 | * @param string $output |
||
| 369 | * @return $this |
||
| 370 | */ |
||
| 371 | 2 | private function writeToOutputNow($output) |
|
| 396 | |||
| 397 | private static function isLevelFatal($level) |
||
| 407 | } |
||
| 408 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.