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 |
||
| 17 | class Run |
||
| 18 | { |
||
| 19 | const EXCEPTION_HANDLER = "handleExceptionFromPHP"; |
||
| 20 | const ERROR_HANDLER = "handleError"; |
||
| 21 | const SHUTDOWN_HANDLER = "handleShutdown"; |
||
| 22 | |||
| 23 | protected $isRegistered; |
||
| 24 | protected $allowQuit = true; |
||
| 25 | protected $sendOutput = true; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var integer|false |
||
| 29 | */ |
||
| 30 | protected $sendHttpCode = 500; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var HandlerInterface[] |
||
| 34 | */ |
||
| 35 | protected $handlerStack = array(); |
||
| 36 | |||
| 37 | protected $silencedPatterns = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Pushes a handler to the end of the stack |
||
| 41 | * |
||
| 42 | * @throws InvalidArgumentException If argument is not callable or instance of HandlerInterface |
||
| 43 | * @param Callable|HandlerInterface $handler |
||
| 44 | * @return Run |
||
| 45 | */ |
||
| 46 | 10 | public function pushHandler($handler) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Removes the last handler in the stack and returns it. |
||
| 65 | * Returns null if there"s nothing else to pop. |
||
| 66 | * @return null|HandlerInterface |
||
| 67 | */ |
||
| 68 | 1 | public function popHandler() |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Returns an array with all handlers, in the |
||
| 75 | * order they were added to the stack. |
||
| 76 | * @return array |
||
| 77 | */ |
||
| 78 | 2 | public function getHandlers() |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Clears all handlers in the handlerStack, including |
||
| 85 | * the default PrettyPage handler. |
||
| 86 | * @return Run |
||
| 87 | */ |
||
| 88 | 1 | public function clearHandlers() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * @param Exception $exception |
||
| 96 | * @return Inspector |
||
| 97 | */ |
||
| 98 | 3 | protected function getInspector(Exception $exception) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Registers this instance as an error handler. |
||
| 105 | * @return Run |
||
| 106 | */ |
||
| 107 | 5 | public function register() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Unregisters all handlers registered by this Whoops\Run instance |
||
| 129 | * @return Run |
||
| 130 | */ |
||
| 131 | 1 | public function unregister() |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Should Whoops allow Handlers to force the script to quit? |
||
| 145 | * @param bool|int $exit |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | 6 | public function allowQuit($exit = null) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Silence particular errors in particular files |
||
| 159 | * @param array|string $patterns List or a single regex pattern to match |
||
| 160 | * @param int $levels Defaults to E_STRICT | E_DEPRECATED |
||
| 161 | * @return \Whoops\Run |
||
| 162 | */ |
||
| 163 | 1 | public function silenceErrorsInPaths($patterns, $levels = 10240) |
|
| 179 | |||
| 180 | /* |
||
| 181 | * Should Whoops send HTTP error code to the browser if possible? |
||
| 182 | * Whoops will by default send HTTP code 500, but you may wish to |
||
| 183 | * use 502, 503, or another 5xx family code. |
||
| 184 | * |
||
| 185 | * @param bool|int $code |
||
| 186 | * @return int|false |
||
| 187 | */ |
||
| 188 | 5 | public function sendHttpCode($code = null) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Should Whoops push output directly to the client? |
||
| 213 | * If this is false, output will be returned by handleException |
||
| 214 | * @param bool|int $send |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | 5 | public function writeToOutput($send = null) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Handles an exception, ultimately generating a Whoops error |
||
| 228 | * page. |
||
| 229 | * |
||
| 230 | * @param Exception $exception |
||
| 231 | * @return string Output generated by handlers |
||
| 232 | */ |
||
| 233 | 7 | public function handleException(Exception $exception) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * This is a private implementation detail, you are not supposed to call this. |
||
| 297 | */ |
||
| 298 | 1 | final public function handleExceptionFromPHP($throwable) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Converts generic PHP errors to \ErrorException |
||
| 315 | * instances, before passing them off to be handled. |
||
| 316 | * |
||
| 317 | * This method MUST be compatible with set_error_handler. |
||
| 318 | * |
||
| 319 | * @param int $level |
||
| 320 | * @param string $message |
||
| 321 | * @param string $file |
||
| 322 | * @param int $line |
||
| 323 | * |
||
| 324 | * @return bool |
||
| 325 | * @throws ErrorException |
||
| 326 | */ |
||
| 327 | 5 | public function handleError($level, $message, $file = null, $line = null) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Special case to deal with Fatal errors and the like. |
||
| 358 | */ |
||
| 359 | public function handleShutdown() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * In certain scenarios, like in shutdown handler, we can not throw exceptions |
||
| 381 | * @var bool |
||
| 382 | */ |
||
| 383 | private $canThrowExceptions = true; |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Echo something to the browser |
||
| 387 | * @param string $output |
||
| 388 | * @return $this |
||
| 389 | */ |
||
| 390 | 3 | private function writeToOutputNow($output) |
|
| 415 | |||
| 416 | 1 | private static function isPHP7Throwable($something) |
|
| 423 | |||
| 424 | private static function isLevelFatal($level) |
||
| 434 | } |
||
| 435 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.