Complex classes like Handle 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 Handle, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Handle |
||
28 | { |
||
29 | /** @var App */ |
||
30 | protected $app; |
||
31 | |||
32 | protected $ignoreReport = [ |
||
33 | HttpException::class, |
||
34 | HttpResponseException::class, |
||
35 | ModelNotFoundException::class, |
||
36 | DataNotFoundException::class, |
||
37 | ValidateException::class, |
||
38 | ]; |
||
39 | |||
40 | protected $isJson = false; |
||
41 | |||
42 | public function __construct(App $app) |
||
46 | |||
47 | /** |
||
48 | * Report or log an exception. |
||
49 | * |
||
50 | * @access public |
||
51 | * @param Throwable $exception |
||
52 | * @return void |
||
53 | */ |
||
54 | public function report(Throwable $exception): void |
||
83 | |||
84 | protected function isIgnoreReport(Throwable $exception): bool |
||
94 | |||
95 | /** |
||
96 | * Render an exception into an HTTP response. |
||
97 | * |
||
98 | * @access public |
||
99 | * @param Request $request |
||
100 | * @param Throwable $e |
||
101 | * @return Response |
||
102 | */ |
||
103 | public function render($request, Throwable $e): Response |
||
114 | |||
115 | /** |
||
116 | * @access public |
||
117 | * @param Output $output |
||
118 | * @param Throwable $e |
||
119 | */ |
||
120 | public function renderForConsole(Output $output, Throwable $e): void |
||
128 | |||
129 | /** |
||
130 | * @access protected |
||
131 | * @param HttpException $e |
||
132 | * @return Response |
||
133 | */ |
||
134 | protected function renderHttpException(HttpException $e): Response |
||
145 | |||
146 | /** |
||
147 | * 收集异常数据 |
||
148 | * @param Throwable $exception |
||
149 | * @return array |
||
150 | */ |
||
151 | protected function convertExceptionToArray(Throwable $exception): array |
||
199 | |||
200 | /** |
||
201 | * @access protected |
||
202 | * @param Throwable $exception |
||
203 | * @return Response |
||
204 | */ |
||
205 | protected function convertExceptionToResponse(Throwable $exception): Response |
||
220 | |||
221 | protected function renderExceptionContent(Throwable $exception): string |
||
230 | |||
231 | /** |
||
232 | * 获取错误编码 |
||
233 | * ErrorException则使用错误级别作为错误编码 |
||
234 | * @access protected |
||
235 | * @param Throwable $exception |
||
236 | * @return integer 错误编码 |
||
237 | */ |
||
238 | protected function getCode(Throwable $exception) |
||
248 | |||
249 | /** |
||
250 | * 获取错误信息 |
||
251 | * ErrorException则使用错误级别作为错误编码 |
||
252 | * @access protected |
||
253 | * @param Throwable $exception |
||
254 | * @return string 错误信息 |
||
255 | */ |
||
256 | protected function getMessage(Throwable $exception): string |
||
278 | |||
279 | /** |
||
280 | * 获取出错文件内容 |
||
281 | * 获取错误的前9行和后9行 |
||
282 | * @access protected |
||
283 | * @param Throwable $exception |
||
284 | * @return array 错误文件内容 |
||
285 | */ |
||
286 | protected function getSourceCode(Throwable $exception): array |
||
304 | |||
305 | /** |
||
306 | * 获取异常扩展信息 |
||
307 | * 用于非调试模式html返回类型显示 |
||
308 | * @access protected |
||
309 | * @param Throwable $exception |
||
310 | * @return array 异常类定义的扩展数据 |
||
311 | */ |
||
312 | protected function getExtendData(Throwable $exception): array |
||
322 | |||
323 | /** |
||
324 | * 获取常量列表 |
||
325 | * @access protected |
||
326 | * @return array 常量列表 |
||
327 | */ |
||
328 | protected function getConst(): array |
||
334 | } |
||
335 |