Complex classes like Handler 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 Handler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class Handler implements ExceptionHandlerContract |
||
39 | { |
||
40 | /** |
||
41 | * The container implementation. |
||
42 | * |
||
43 | * @var \Illuminate\Contracts\Container\Container |
||
44 | */ |
||
45 | protected $container; |
||
46 | |||
47 | /** |
||
48 | * A list of the exception types that are not reported. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $dontReport = []; |
||
53 | |||
54 | /** |
||
55 | * A list of the internal exception types that should not be reported. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $internalDontReport = [ |
||
60 | AuthenticationException::class, |
||
61 | AuthorizationException::class, |
||
62 | HttpException::class, |
||
63 | HttpResponseException::class, |
||
64 | ModelNotFoundException::class, |
||
65 | SuspiciousOperationException::class, |
||
66 | TokenMismatchException::class, |
||
67 | ValidationException::class, |
||
68 | ]; |
||
69 | |||
70 | /** |
||
71 | * A list of the inputs that are never flashed for validation exceptions. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $dontFlash = [ |
||
76 | 'password', |
||
77 | 'password_confirmation', |
||
78 | ]; |
||
79 | |||
80 | /** |
||
81 | * Create a new exception handler instance. |
||
82 | * |
||
83 | * @param \Illuminate\Contracts\Container\Container $container |
||
84 | * |
||
85 | * @return void |
||
|
|||
86 | */ |
||
87 | public function __construct(Container $container) |
||
91 | |||
92 | /** |
||
93 | * Report or log an exception. |
||
94 | * |
||
95 | * @param \Throwable $e |
||
96 | * |
||
97 | * @throws \Exception |
||
98 | * |
||
99 | * @return void |
||
100 | */ |
||
101 | public function report(Throwable $e) |
||
128 | |||
129 | /** |
||
130 | * Determine if the exception should be reported. |
||
131 | * |
||
132 | * @param \Throwable $e |
||
133 | * |
||
134 | * @return bool |
||
135 | */ |
||
136 | public function shouldReport(Throwable $e) |
||
140 | |||
141 | /** |
||
142 | * Determine if the exception is in the "do not report" list. |
||
143 | * |
||
144 | * @param \Throwable $e |
||
145 | * |
||
146 | * @return bool |
||
147 | */ |
||
148 | protected function shouldntReport(Throwable $e) |
||
156 | |||
157 | /** |
||
158 | * Get the default exception context variables for logging. |
||
159 | * |
||
160 | * @param \Throwable $e |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | protected function exceptionContext(Throwable $e) |
||
168 | |||
169 | /** |
||
170 | * Get the default context variables for logging. |
||
171 | * |
||
172 | * @return array |
||
173 | */ |
||
174 | protected function context() |
||
185 | |||
186 | /** |
||
187 | * Render an exception into an HTTP response. |
||
188 | * |
||
189 | * @param \Illuminate\Http\Request $request |
||
190 | * @param \Throwable $e |
||
191 | * |
||
192 | * @throws \Throwable |
||
193 | * |
||
194 | * @return \Symfony\Component\HttpFoundation\Response |
||
195 | */ |
||
196 | public function render($request, Throwable $e) |
||
218 | |||
219 | /** |
||
220 | * Prepare exception for rendering. |
||
221 | * |
||
222 | * @param \Throwable $e |
||
223 | * |
||
224 | * @return \Throwable |
||
225 | */ |
||
226 | protected function prepareException(Throwable $e) |
||
240 | |||
241 | /** |
||
242 | * Convert an authentication exception into a response. |
||
243 | * |
||
244 | * @param \Illuminate\Http\Request $request |
||
245 | * @param \Illuminate\Auth\AuthenticationException $exception |
||
246 | * |
||
247 | * @return \Symfony\Component\HttpFoundation\Response |
||
248 | */ |
||
249 | protected function unauthenticated($request, AuthenticationException $exception) |
||
255 | |||
256 | /** |
||
257 | * Create a response object from the given validation exception. |
||
258 | * |
||
259 | * @param \Illuminate\Validation\ValidationException $e |
||
260 | * @param \Illuminate\Http\Request $request |
||
261 | * |
||
262 | * @return \Symfony\Component\HttpFoundation\Response |
||
263 | */ |
||
264 | protected function convertValidationExceptionToResponse(ValidationException $e, $request) |
||
274 | |||
275 | /** |
||
276 | * Convert a validation exception into a response. |
||
277 | * |
||
278 | * @param \Illuminate\Http\Request $request |
||
279 | * @param \Illuminate\Validation\ValidationException $exception |
||
280 | * |
||
281 | * @return \Illuminate\Http\Response |
||
282 | */ |
||
283 | protected function invalid($request, ValidationException $exception) |
||
289 | |||
290 | /** |
||
291 | * Convert a validation exception into a JSON response. |
||
292 | * |
||
293 | * @param \Illuminate\Http\Request $request |
||
294 | * @param \Illuminate\Validation\ValidationException $exception |
||
295 | * |
||
296 | * @return \Illuminate\Http\JsonResponse |
||
297 | */ |
||
298 | protected function invalidJson($request, ValidationException $exception) |
||
305 | |||
306 | /** |
||
307 | * Prepare a response for the given exception. |
||
308 | * |
||
309 | * @param \Illuminate\Http\Request $request |
||
310 | * @param \Throwable $e |
||
311 | * |
||
312 | * @return \Symfony\Component\HttpFoundation\Response |
||
313 | */ |
||
314 | protected function prepareResponse($request, Throwable $e) |
||
329 | |||
330 | /** |
||
331 | * Create a Symfony response for the given exception. |
||
332 | * |
||
333 | * @param \Throwable $e |
||
334 | * |
||
335 | * @return \Symfony\Component\HttpFoundation\Response |
||
336 | */ |
||
337 | protected function convertExceptionToResponse(Throwable $e) |
||
345 | |||
346 | /** |
||
347 | * Get the response content for the given exception. |
||
348 | * |
||
349 | * @param \Throwable $e |
||
350 | * |
||
351 | * @return string |
||
352 | */ |
||
353 | protected function renderExceptionContent(Throwable $e) |
||
363 | |||
364 | /** |
||
365 | * Render an exception to a string using "Whoops". |
||
366 | * |
||
367 | * @param \Throwable $e |
||
368 | * |
||
369 | * @return string |
||
370 | */ |
||
371 | protected function renderExceptionWithWhoops(Throwable $e) |
||
381 | |||
382 | /** |
||
383 | * Get the Whoops handler for the application. |
||
384 | * |
||
385 | * @return \Whoops\Handler\Handler |
||
386 | */ |
||
387 | protected function whoopsHandler() |
||
395 | |||
396 | /** |
||
397 | * Render an exception to a string using Symfony. |
||
398 | * |
||
399 | * @param \Throwable $e |
||
400 | * @param bool $debug |
||
401 | * |
||
402 | * @return string |
||
403 | */ |
||
404 | protected function renderExceptionWithSymfony(Throwable $e, $debug) |
||
410 | |||
411 | /** |
||
412 | * Render the given HttpException. |
||
413 | * |
||
414 | * @param \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $e |
||
415 | * |
||
416 | * @return \Symfony\Component\HttpFoundation\Response |
||
417 | */ |
||
418 | protected function renderHttpException(HttpExceptionInterface $e) |
||
431 | |||
432 | /** |
||
433 | * Register the error template hint paths. |
||
434 | * |
||
435 | * @return void |
||
436 | */ |
||
437 | protected function registerErrorViewPaths() |
||
445 | |||
446 | /** |
||
447 | * Get the view used to render HTTP exceptions. |
||
448 | * |
||
449 | * @param \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $e |
||
450 | * |
||
451 | * @return string |
||
452 | */ |
||
453 | protected function getHttpExceptionView(HttpExceptionInterface $e) |
||
457 | |||
458 | /** |
||
459 | * Map the given exception into an Illuminate response. |
||
460 | * |
||
461 | * @param \Symfony\Component\HttpFoundation\Response $response |
||
462 | * @param \Throwable $e |
||
463 | * |
||
464 | * @return \Illuminate\Http\Response |
||
465 | */ |
||
466 | protected function toIlluminateResponse($response, Throwable $e) |
||
484 | |||
485 | /** |
||
486 | * Prepare a JSON response for the given exception. |
||
487 | * |
||
488 | * @param \Illuminate\Http\Request $request |
||
489 | * @param \Throwable $e |
||
490 | * |
||
491 | * @return \Illuminate\Http\JsonResponse |
||
492 | */ |
||
493 | protected function prepareJsonResponse($request, Throwable $e) |
||
502 | |||
503 | /** |
||
504 | * Convert the given exception to an array. |
||
505 | * |
||
506 | * @param \Throwable $e |
||
507 | * |
||
508 | * @return array |
||
509 | */ |
||
510 | protected function convertExceptionToArray(Throwable $e) |
||
524 | |||
525 | /** |
||
526 | * Render an exception to the console. |
||
527 | * |
||
528 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
529 | * @param \Throwable $e |
||
530 | * |
||
531 | * @return void |
||
532 | */ |
||
533 | public function renderForConsole($output, Throwable $e) |
||
537 | |||
538 | /** |
||
539 | * Determine if the given exception is an HTTP exception. |
||
540 | * |
||
541 | * @param \Throwable $e |
||
542 | * |
||
543 | * @return bool |
||
544 | */ |
||
545 | protected function isHttpException(Throwable $e) |
||
549 | } |
||
550 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.