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 | private $previousExceptions; |
||
32 | |||
33 | /** |
||
34 | * @param \Throwable $exception The exception to inspect |
||
35 | */ |
||
36 | 5 | public function __construct($exception) |
|
37 | { |
||
38 | 5 | $this->exception = $exception; |
|
39 | 5 | } |
|
40 | |||
41 | /** |
||
42 | * @return \Throwable |
||
43 | */ |
||
44 | 3 | public function getException() |
|
45 | { |
||
46 | 3 | return $this->exception; |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * @return string |
||
51 | */ |
||
52 | 2 | public function getExceptionName() |
|
53 | { |
||
54 | 2 | return get_class($this->exception); |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * @return string |
||
59 | */ |
||
60 | public function getExceptionMessage() |
||
61 | { |
||
62 | return $this->extractDocrefUrl($this->exception->getMessage())['message']; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @return string[] |
||
67 | */ |
||
68 | 1 | public function getPreviousExceptionMessages() |
|
69 | { |
||
70 | return array_map(function ($prev) { |
||
71 | /** @var \Throwable $prev */ |
||
72 | 1 | return $this->extractDocrefUrl($prev->getMessage())['message']; |
|
73 | 1 | }, $this->getPreviousExceptions()); |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * @return int[] |
||
78 | */ |
||
79 | public function getPreviousExceptionCodes() |
||
80 | { |
||
81 | 1 | return array_map(function ($prev) { |
|
82 | /** @var \Throwable $prev */ |
||
83 | 1 | return $prev->getCode(); |
|
84 | 1 | }, $this->getPreviousExceptions()); |
|
85 | } |
||
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() |
||
93 | { |
||
94 | return $this->extractDocrefUrl($this->exception->getMessage())['url']; |
||
95 | } |
||
96 | |||
97 | private function extractDocrefUrl($message) |
||
98 | { |
||
99 | $docref = [ |
||
100 | 'message' => $message, |
||
101 | 'url' => null, |
||
102 | ]; |
||
103 | |||
104 | // php embbeds urls to the manual into the Exception message with the following ini-settings defined |
||
105 | // http://php.net/manual/en/errorfunc.configuration.php#ini.docref-root |
||
106 | if (!ini_get('html_errors') || !ini_get('docref_root')) { |
||
107 | return $docref; |
||
108 | } |
||
109 | |||
110 | $pattern = "/\[<a href='([^']+)'>(?:[^<]+)<\/a>\]/"; |
||
111 | if (preg_match($pattern, $message, $matches)) { |
||
112 | // -> strip those automatically generated links from the exception message |
||
113 | $docref['message'] = preg_replace($pattern, '', $message, 1); |
||
114 | $docref['url'] = $matches[1]; |
||
115 | } |
||
116 | |||
117 | return $docref; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Does the wrapped Exception has a previous Exception? |
||
122 | * @return bool |
||
123 | */ |
||
124 | 2 | public function hasPreviousException() |
|
128 | |||
129 | /** |
||
130 | * Returns an Inspector for a previous Exception, if any. |
||
131 | * @todo Clean this up a bit, cache stuff a bit better. |
||
132 | * @return Inspector |
||
133 | */ |
||
134 | 2 | public function getPreviousExceptionInspector() |
|
135 | { |
||
136 | 2 | if ($this->previousExceptionInspector === null) { |
|
146 | |||
147 | |||
148 | /** |
||
149 | * Returns an array of all previous exceptions for this inspector's exception |
||
150 | * @return \Throwable[] |
||
151 | */ |
||
152 | 2 | public function getPreviousExceptions() |
|
166 | |||
167 | /** |
||
168 | * Returns an iterator for the inspected exception's |
||
169 | * frames. |
||
170 | * @return \Whoops\Exception\FrameCollection |
||
171 | */ |
||
172 | 3 | public function getFrames() |
|
232 | |||
233 | /** |
||
234 | * Gets the backtrace from an exception. |
||
235 | * |
||
236 | * If xdebug is installed |
||
237 | * |
||
238 | * @param \Throwable $e |
||
239 | * @return array |
||
240 | */ |
||
241 | 1 | protected function getTrace($e) |
|
242 | { |
||
243 | 1 | $traces = $e->getTrace(); |
|
244 | |||
245 | // Get trace from xdebug if enabled, failure exceptions only trace to the shutdown handler by default |
||
246 | 1 | if (!$e instanceof \ErrorException) { |
|
247 | 1 | return $traces; |
|
248 | } |
||
249 | |||
250 | if (!Misc::isLevelFatal($e->getSeverity())) { |
||
251 | return $traces; |
||
252 | } |
||
253 | |||
254 | if (!extension_loaded('xdebug') || !function_exists('xdebug_is_enabled') || !xdebug_is_enabled()) { |
||
255 | return $traces; |
||
256 | } |
||
257 | |||
258 | // Use xdebug to get the full stack trace and remove the shutdown handler stack trace |
||
259 | $stack = array_reverse(xdebug_get_function_stack()); |
||
260 | $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
||
261 | $traces = array_diff_key($stack, $trace); |
||
262 | |||
263 | return $traces; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Given an exception, generates an array in the format |
||
268 | * generated by Exception::getTrace() |
||
269 | * @param \Throwable $exception |
||
270 | * @return array |
||
271 | */ |
||
272 | 1 | 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 | 1 | 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: