Complex classes like Inspector 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 Inspector, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Inspector |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var \Throwable |
||
| 15 | */ |
||
| 16 | private $exception; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var \Whoops\Exception\FrameCollection |
||
| 20 | */ |
||
| 21 | private $frames; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var \Whoops\Exception\Inspector |
||
| 25 | */ |
||
| 26 | private $previousExceptionInspector; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var \Throwable[] |
||
| 30 | */ |
||
| 31 | 3 | private $previousExceptions; |
|
| 32 | |||
| 33 | 3 | /** |
|
| 34 | 3 | * @param \Throwable $exception The exception to inspect |
|
| 35 | */ |
||
| 36 | public function __construct($exception) |
||
| 40 | |||
| 41 | 3 | /** |
|
| 42 | * @return \Throwable |
||
| 43 | */ |
||
| 44 | public function getException() |
||
| 48 | |||
| 49 | 2 | /** |
|
| 50 | * @return string |
||
| 51 | */ |
||
| 52 | public function getExceptionName() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | public function getExceptionMessage() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return string[] |
||
| 67 | */ |
||
| 68 | public function getPreviousExceptionMessages() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return int[] |
||
| 78 | */ |
||
| 79 | public function getPreviousExceptionCodes() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns a url to the php-manual related to the underlying error - when available. |
||
| 89 | * |
||
| 90 | * @return string|null |
||
| 91 | */ |
||
| 92 | public function getExceptionDocrefUrl() |
||
| 96 | |||
| 97 | 2 | private function extractDocrefUrl($message) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Does the wrapped Exception has a previous Exception? |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | public function hasPreviousException() |
||
| 128 | 3 | ||
| 129 | /** |
||
| 130 | * Returns an Inspector for a previous Exception, if any. |
||
| 131 | 3 | * @todo Clean this up a bit, cache stuff a bit better. |
|
| 132 | 3 | * @return Inspector |
|
| 133 | */ |
||
| 134 | 3 | public function getPreviousExceptionInspector() |
|
| 146 | 3 | ||
| 147 | 3 | ||
| 148 | /** |
||
| 149 | * Returns an array of all previous exceptions for this inspector's exception |
||
| 150 | 3 | * @return \Throwable[] |
|
| 151 | 3 | */ |
|
| 152 | 3 | public function getPreviousExceptions() |
|
| 166 | |||
| 167 | 3 | /** |
|
| 168 | * Returns an iterator for the inspected exception's |
||
| 169 | 1 | * frames. |
|
| 170 | 1 | * @return \Whoops\Exception\FrameCollection |
|
| 171 | */ |
||
| 172 | 1 | public function getFrames() |
|
| 232 | 1 | ||
| 233 | 1 | /** |
|
| 234 | 1 | * Gets the backtrace from an exception. |
|
| 235 | * |
||
| 236 | * If xdebug is installed |
||
| 237 | * |
||
| 238 | * @param \Throwable $e |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | protected function getTrace($e) |
||
| 265 | |||
| 266 | 1 | /** |
|
| 267 | * Given an exception, generates an array in the format |
||
| 268 | * generated by Exception::getTrace() |
||
| 269 | * @param \Throwable $exception |
||
| 270 | 1 | * @return array |
|
| 271 | 1 | */ |
|
| 272 | protected function getFrameFromException($exception) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Given an error, generates an array in the format |
||
| 286 | * generated by ErrorException |
||
| 287 | * @param ErrorException $exception |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | protected function getFrameFromError(ErrorException $exception) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Determine if the frame can be used to fill in previous frame's missing info |
||
| 302 | * happens for call_user_func and call_user_func_array usages (PHP Bug #44428) |
||
| 303 | * |
||
| 304 | * @param array $frame |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | protected function isValidNextFrame(array $frame) |
||
| 323 | } |
||
| 324 |
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: