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 |
||
| 19 | { |
||
| 20 | const EXCEPTION_HANDLER = "handleException"; |
||
| 21 | const ERROR_HANDLER = "handleError"; |
||
| 22 | const SHUTDOWN_HANDLER = "handleShutdown"; |
||
| 23 | |||
| 24 | protected $isRegistered; |
||
| 25 | protected $allowQuit = true; |
||
| 26 | protected $sendOutput = true; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var integer|false |
||
| 30 | */ |
||
| 31 | protected $sendHttpCode = 500; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var HandlerInterface[] |
||
| 35 | */ |
||
| 36 | protected $handlerStack = array(); |
||
| 37 | |||
| 38 | protected $silencedPatterns = array(); |
||
| 39 | |||
| 40 | protected $system; |
||
| 41 | |||
| 42 | 4 | public function __construct(SystemFacade $system = null) |
|
| 46 | |||
| 47 | /** |
||
| 48 | * Pushes a handler to the end of the stack |
||
| 49 | * |
||
| 50 | * @throws InvalidArgumentException If argument is not callable or instance of HandlerInterface |
||
| 51 | * @param Callable|HandlerInterface $handler |
||
| 52 | * @return Run |
||
| 53 | */ |
||
| 54 | 8 | public function pushHandler($handler) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Removes the last handler in the stack and returns it. |
||
| 73 | * Returns null if there"s nothing else to pop. |
||
| 74 | * @return null|HandlerInterface |
||
| 75 | */ |
||
| 76 | 1 | public function popHandler() |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Returns an array with all handlers, in the |
||
| 83 | * order they were added to the stack. |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | 2 | public function getHandlers() |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Clears all handlers in the handlerStack, including |
||
| 93 | * the default PrettyPage handler. |
||
| 94 | * @return Run |
||
| 95 | */ |
||
| 96 | 1 | public function clearHandlers() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @param \Throwable $exception |
||
| 104 | * @return Inspector |
||
| 105 | */ |
||
| 106 | 1 | protected function getInspector($exception) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Registers this instance as an error handler. |
||
| 113 | * @return Run |
||
| 114 | */ |
||
| 115 | 4 | public function register() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Unregisters all handlers registered by this Whoops\Run instance |
||
| 137 | * @return Run |
||
| 138 | */ |
||
| 139 | 1 | public function unregister() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Should Whoops allow Handlers to force the script to quit? |
||
| 153 | * @param bool|int $exit |
||
| 154 | * @return bool |
||
| 155 | */ |
||
| 156 | 4 | public function allowQuit($exit = null) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Silence particular errors in particular files |
||
| 167 | * @param array|string $patterns List or a single regex pattern to match |
||
| 168 | * @param int $levels Defaults to E_STRICT | E_DEPRECATED |
||
| 169 | * @return \Whoops\Run |
||
| 170 | */ |
||
| 171 | 1 | public function silenceErrorsInPaths($patterns, $levels = 10240) |
|
| 187 | |||
| 188 | /* |
||
| 189 | * Should Whoops send HTTP error code to the browser if possible? |
||
| 190 | * Whoops will by default send HTTP code 500, but you may wish to |
||
| 191 | * use 502, 503, or another 5xx family code. |
||
| 192 | * |
||
| 193 | * @param bool|int $code |
||
| 194 | * @return int|false |
||
| 195 | */ |
||
| 196 | 3 | public function sendHttpCode($code = null) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Should Whoops push output directly to the client? |
||
| 221 | * If this is false, output will be returned by handleException |
||
| 222 | * @param bool|int $send |
||
| 223 | * @return bool |
||
| 224 | */ |
||
| 225 | 3 | public function writeToOutput($send = null) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Handles an exception, ultimately generating a Whoops error |
||
| 236 | * page. |
||
| 237 | * |
||
| 238 | * @param \Throwable $exception |
||
| 239 | * @return string Output generated by handlers |
||
| 240 | */ |
||
| 241 | 5 | public function handleException($exception) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Converts generic PHP errors to \ErrorException |
||
| 307 | * instances, before passing them off to be handled. |
||
| 308 | * |
||
| 309 | * This method MUST be compatible with set_error_handler. |
||
| 310 | * |
||
| 311 | * @param int $level |
||
| 312 | * @param string $message |
||
| 313 | * @param string $file |
||
| 314 | * @param int $line |
||
| 315 | * |
||
| 316 | * @return bool |
||
| 317 | * @throws ErrorException |
||
| 318 | */ |
||
| 319 | 5 | public function handleError($level, $message, $file = null, $line = null) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Special case to deal with Fatal errors and the like. |
||
| 350 | */ |
||
| 351 | public function handleShutdown() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * In certain scenarios, like in shutdown handler, we can not throw exceptions |
||
| 373 | * @var bool |
||
| 374 | */ |
||
| 375 | private $canThrowExceptions = true; |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Echo something to the browser |
||
| 379 | * @param string $output |
||
| 380 | * @return $this |
||
| 381 | */ |
||
| 382 | 1 | private function writeToOutputNow($output) |
|
| 394 | } |
||
| 395 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: