Total Complexity | 59 |
Total Lines | 513 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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.
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 |
||
55 | class Handler implements ExceptionHandlerContract { |
||
56 | |||
57 | /** |
||
58 | * The container implementation. |
||
59 | * |
||
60 | * @var \Illuminate\Contracts\Container\Container |
||
61 | */ |
||
62 | protected $container; |
||
63 | |||
64 | /** |
||
65 | * A list of the exception types that are not reported. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $dont_report = array(); |
||
70 | |||
71 | /** |
||
72 | * A list of the internal exception types that should not be reported. |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $internal_dont_report = array( |
||
77 | AuthenticationException::class, |
||
78 | AuthorizationException::class, |
||
79 | HttpException::class, |
||
80 | HttpResponseException::class, |
||
81 | ModelNotFoundException::class, |
||
82 | SuspiciousOperationException::class, |
||
83 | TokenMismatchException::class, |
||
84 | ValidationException::class, |
||
85 | ); |
||
86 | |||
87 | /** |
||
88 | * A list of the inputs that are never flashed for validation exceptions. |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $dont_flash = array( |
||
93 | 'password', |
||
94 | 'password_confirmation', |
||
95 | ); |
||
96 | |||
97 | /** |
||
98 | * Create a new exception handler instance. |
||
99 | * |
||
100 | * @param \Illuminate\Contracts\Container\Container $container The app container. |
||
101 | * |
||
102 | * @return void |
||
103 | */ |
||
104 | public function __construct( Container $container ) { |
||
105 | $this->container = $container; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Report or log an exception. |
||
110 | * |
||
111 | * @param \Throwable $e The throwable exception. |
||
112 | * |
||
113 | * @throws \Exception Throw the exception. |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | public function report( Throwable $e ) { |
||
118 | if ( $this->shouldnt_report( $e ) ) { |
||
119 | return; |
||
120 | } |
||
121 | |||
122 | $report_callable = array( $e, 'report' ); |
||
123 | |||
124 | if ( is_callable( $report_callable ) ) { |
||
125 | $this->container->call( $report_callable ); |
||
126 | |||
127 | return; |
||
128 | } |
||
129 | |||
130 | try { |
||
131 | $logger = $this->container->make( LoggerInterface::class ); |
||
132 | } catch ( Exception $ex ) { |
||
133 | throw $e; |
||
134 | } |
||
135 | |||
136 | $logger->error( |
||
137 | $e->getMessage(), |
||
138 | array_merge( |
||
139 | $this->exception_context( $e ), |
||
140 | $this->context(), |
||
141 | array( 'exception' => $e ) |
||
142 | ) |
||
143 | ); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Determine if the exception should be reported. |
||
148 | * |
||
149 | * @param \Throwable $e The throwable exception. |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function should_report( Throwable $e ) { |
||
154 | return ! $this->shouldnt_report( $e ); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Determine if the exception is in the "do not report" list. |
||
159 | * |
||
160 | * @param \Throwable $e The throwable exception. |
||
161 | * |
||
162 | * @return bool |
||
163 | */ |
||
164 | protected function shouldnt_report( Throwable $e ) { |
||
165 | $dont_report = array_merge( $this->dont_report, $this->internal_dont_report ); |
||
166 | |||
167 | return ! is_null( |
||
168 | Arr::first( |
||
169 | $dont_report, |
||
170 | function ( $type ) use ( $e ) { |
||
171 | return $e instanceof $type; |
||
172 | } |
||
173 | ) |
||
174 | ); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Get the default exception context variables for logging. |
||
179 | * |
||
180 | * @param \Throwable $e The throwable exception. |
||
181 | * |
||
182 | * @return array |
||
183 | */ |
||
184 | protected function exception_context( Throwable $e ) { |
||
185 | return array(); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Get the default context variables for logging. |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | protected function context() { |
||
203 | } |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Render an exception into an HTTP response. |
||
208 | * |
||
209 | * @param \Illuminate\Http\Request $request The app request. |
||
210 | * @param \Throwable $e The throwable exception. |
||
211 | * |
||
212 | * @throws \Throwable Throw the exception. |
||
213 | * |
||
214 | * @return \Symfony\Component\HttpFoundation\Response |
||
215 | */ |
||
216 | public function render( $request, Throwable $e ) { |
||
217 | $response = $e->render( $request ); |
||
218 | if ( method_exists( $e, 'render' ) && $response ) { |
||
219 | return Router::toResponse( $request, $response ); |
||
220 | } elseif ( $e instanceof Responsable ) { |
||
221 | return $e->toResponse( $request ); |
||
222 | } |
||
223 | |||
224 | $e = $this->prepare_exception( $e ); |
||
225 | |||
226 | if ( $e instanceof HttpResponseException ) { |
||
227 | return $e->getResponse(); |
||
228 | } elseif ( $e instanceof AuthenticationException ) { |
||
229 | return $this->unauthenticated( $request, $e ); |
||
230 | } elseif ( $e instanceof ValidationException ) { |
||
231 | return $this->convert_validation_exception_to_response( $e, $request ); |
||
232 | } |
||
233 | |||
234 | return $request->expectsJson() |
||
235 | ? $this->prepare_json_response( $request, $e ) |
||
236 | : $this->prepare_response( $request, $e ); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Prepare exception for rendering. |
||
241 | * |
||
242 | * @param \Throwable $e The throwable exception. |
||
243 | * |
||
244 | * @return \Throwable |
||
245 | */ |
||
246 | protected function prepare_exception( Throwable $e ) { |
||
247 | if ( $e instanceof ModelNotFoundException ) { |
||
248 | $e = new NotFoundHttpException( $e->getMessage(), $e ); |
||
249 | } elseif ( $e instanceof AuthorizationException ) { |
||
250 | $e = new AccessDeniedHttpException( $e->getMessage(), $e ); |
||
251 | } elseif ( $e instanceof TokenMismatchException ) { |
||
252 | $e = new HttpException( 419, $e->getMessage(), $e ); |
||
253 | } elseif ( $e instanceof SuspiciousOperationException ) { |
||
254 | $e = new NotFoundHttpException( 'Bad hostname provided.', $e ); |
||
255 | } |
||
256 | |||
257 | return $e; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Convert an authentication exception into a response. |
||
262 | * |
||
263 | * @param \Illuminate\Http\Request $request The app request. |
||
264 | * @param \Illuminate\Auth\AuthenticationException $exception The authenticated exception. |
||
265 | * |
||
266 | * @return \Symfony\Component\HttpFoundation\Response |
||
267 | */ |
||
268 | protected function unauthenticated( $request, AuthenticationException $exception ) { |
||
269 | return $request->expectsJson() |
||
270 | ? response()->json( array( 'message' => $exception->getMessage() ), 401 ) |
||
271 | : redirect()->guest( $exception->redirectTo() ?? route( 'login' ) ); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Create a response object from the given validation exception. |
||
276 | * |
||
277 | * @param \Illuminate\Validation\ValidationException $e The validation exception. |
||
278 | * @param \Illuminate\Http\Request $request The app request. |
||
279 | * |
||
280 | * @return \Symfony\Component\HttpFoundation\Response |
||
281 | */ |
||
282 | protected function convert_validation_exception_to_response( ValidationException $e, $request ) { |
||
283 | if ( $e->response ) { |
||
284 | return $e->response; |
||
285 | } |
||
286 | |||
287 | return $request->expectsJson() |
||
288 | ? $this->invalid_json( $request, $e ) |
||
289 | : $this->invalid( $request, $e ); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Convert a validation exception into a response. |
||
294 | * |
||
295 | * @param \Illuminate\Http\Request $request The app request. |
||
296 | * @param \Illuminate\Validation\ValidationException $exception The validation exception. |
||
297 | * |
||
298 | * @return \Illuminate\Http\Response |
||
299 | */ |
||
300 | protected function invalid( $request, ValidationException $exception ) { |
||
301 | return redirect( $exception->redirectTo ?? url()->previous() ) |
||
302 | ->withInput( Arr::except( $request->input(), $this->dont_flash ) ) |
||
303 | ->withErrors( $exception->errors(), $exception->errorBag ); |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Convert a validation exception into a JSON response. |
||
308 | * |
||
309 | * @param \Illuminate\Http\Request $request The app request. |
||
310 | * @param \Illuminate\Validation\ValidationException $exception The validation exception. |
||
311 | * |
||
312 | * @return \Illuminate\Http\JsonResponse |
||
313 | */ |
||
314 | protected function invalid_json( $request, ValidationException $exception ) { |
||
315 | return response()->json( |
||
316 | array( |
||
317 | 'message' => $exception->getMessage(), |
||
318 | 'errors' => $exception->errors(), |
||
319 | ), |
||
320 | $exception->status |
||
321 | ); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Prepare a response for the given exception. |
||
326 | * |
||
327 | * @param \Illuminate\Http\Request $request The app request. |
||
328 | * @param \Throwable $e The throwable exception. |
||
329 | * |
||
330 | * @return \Symfony\Component\HttpFoundation\Response |
||
331 | */ |
||
332 | protected function prepare_response( $request, Throwable $e ) { |
||
333 | if ( ! $this->is_http_exception( $e ) && $this->container['config']['app.debug'] ) { |
||
334 | return $this->to_illuminate_response( $this->convert_exception_to_response( $e ), $e ); |
||
335 | } |
||
336 | |||
337 | if ( ! $this->is_http_exception( $e ) ) { |
||
338 | $e = new HttpException( 500, $e->getMessage() ); |
||
339 | } |
||
340 | |||
341 | return $this->to_illuminate_response( |
||
342 | $this->render_http_exception( $e ), |
||
343 | $e |
||
344 | ); |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Create a Symfony response for the given exception. |
||
349 | * |
||
350 | * @param \Throwable $e The throwable exception. |
||
351 | * |
||
352 | * @return \Symfony\Component\HttpFoundation\Response |
||
353 | */ |
||
354 | protected function convert_exception_to_response( Throwable $e ) { |
||
355 | return SymfonyResponse::create( |
||
356 | $this->render_exception_content( $e ), |
||
357 | $this->is_http_exception( $e ) ? $e->getStatusCode() : 500, |
||
358 | $this->is_http_exception( $e ) ? $e->getHeaders() : array() |
||
359 | ); |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Get the response content for the given exception. |
||
364 | * |
||
365 | * @param \Throwable $e The throwable exception. |
||
366 | * |
||
367 | * @return string |
||
368 | */ |
||
369 | protected function render_exception_content( Throwable $e ) { |
||
370 | try { |
||
371 | return $this->container['config']['app.debug'] && class_exists( Whoops::class ) |
||
372 | ? $this->render_exception_with_whoops( $e ) |
||
373 | : $this->render_exception_with_symfony( $e, $this->container['config']['app.debug'] ); |
||
374 | } catch ( Exception $e ) { |
||
375 | return $this->render_exception_with_symfony( $e, $this->container['config']['app.debug'] ); |
||
376 | } |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * Render an exception to a string using "Whoops". |
||
381 | * |
||
382 | * @param \Throwable $e The throwable exception. |
||
383 | * |
||
384 | * @return string |
||
385 | */ |
||
386 | protected function render_exception_with_whoops( Throwable $e ) { |
||
387 | return tap( |
||
388 | new Whoops(), |
||
389 | function ( $whoops ) { |
||
390 | $whoops->appendHandler( $this->whoops_handler() ); |
||
391 | |||
392 | $whoops->writeToOutput( false ); |
||
393 | |||
394 | $whoops->allowQuit( false ); |
||
395 | } |
||
396 | )->handleException( $e ); |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * Get the Whoops handler for the application. |
||
401 | * |
||
402 | * @return \Whoops\Handler\Handler |
||
403 | */ |
||
404 | protected function whoops_handler() { |
||
405 | try { |
||
406 | return $this->container( HandlerInterface::class ); |
||
407 | } catch ( BindingResolutionException $e ) { |
||
408 | return ( new WhoopsHandler() )->forDebug(); |
||
409 | } |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * Render an exception to a string using Symfony. |
||
414 | * |
||
415 | * @param \Throwable $e The throwable exception. |
||
416 | * @param bool $debug Enable or disable debug. |
||
417 | * |
||
418 | * @return string |
||
419 | */ |
||
420 | protected function render_exception_with_symfony( Throwable $e, $debug ) { |
||
421 | $renderer = new HtmlErrorRenderer( $debug ); |
||
422 | |||
423 | return $renderer->getBody( $renderer->render( $e ) ); |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * Render the given HttpException. |
||
428 | * |
||
429 | * @param \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $e The http exception. |
||
430 | * |
||
431 | * @return \Symfony\Component\HttpFoundation\Response |
||
432 | */ |
||
433 | protected function render_http_exception( HttpExceptionInterface $e ) { |
||
434 | $this->register_error_view_paths(); |
||
435 | $view = $this->get_http_exception_view( $e ); |
||
436 | |||
437 | if ( view()->exists( $view ) ) { |
||
438 | return response()->view( |
||
439 | $view, |
||
440 | array( |
||
441 | 'errors' => new ViewErrorBag(), |
||
442 | 'exception' => $e, |
||
443 | ), |
||
444 | $e->getStatusCode(), |
||
445 | $e->getHeaders() |
||
446 | ); |
||
447 | } |
||
448 | |||
449 | return $this->convert_exception_to_response( $e ); |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * Register the error template hint paths. |
||
454 | * |
||
455 | * @return void |
||
456 | */ |
||
457 | protected function register_error_view_paths() { |
||
458 | $paths = collect( $this->container['config']['view.paths'] ); |
||
459 | |||
460 | View::replaceNamespace( |
||
461 | 'errors', |
||
462 | $paths->map( |
||
463 | function ( $path ) { |
||
464 | return "{$path}/errors"; |
||
465 | } |
||
466 | )->push( __DIR__ . '/views' )->all() |
||
467 | ); |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * Get the view used to render HTTP exceptions. |
||
472 | * |
||
473 | * @param \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $e The http exceptions. |
||
474 | * |
||
475 | * @return string |
||
476 | */ |
||
477 | protected function get_http_exception_view( HttpExceptionInterface $e ) { |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * Map the given exception into an Illuminate response. |
||
483 | * |
||
484 | * @param \Symfony\Component\HttpFoundation\Response $response The app http response. |
||
485 | * @param \Throwable $e The throwable exception. |
||
486 | * |
||
487 | * @return \Illuminate\Http\Response |
||
488 | */ |
||
489 | protected function to_illuminate_response( $response, Throwable $e ) { |
||
490 | if ( $response instanceof SymfonyRedirectResponse ) { |
||
491 | $response = new RedirectResponse( |
||
492 | $response->getTargetUrl(), |
||
493 | $response->getStatusCode(), |
||
494 | $response->headers->all() |
||
495 | ); |
||
496 | } else { |
||
497 | $response = new Response( |
||
498 | $response->getContent(), |
||
499 | $response->getStatusCode(), |
||
500 | $response->headers->all() |
||
501 | ); |
||
502 | } |
||
503 | |||
504 | return $response->withException( $e ); |
||
505 | } |
||
506 | |||
507 | /** |
||
508 | * Prepare a JSON response for the given exception. |
||
509 | * |
||
510 | * @param \Illuminate\Http\Request $request The app http request. |
||
511 | * @param \Throwable $e The throwable exception. |
||
512 | * |
||
513 | * @return \Illuminate\Http\JsonResponse |
||
514 | */ |
||
515 | protected function prepare_json_response( $request, Throwable $e ) { |
||
516 | return new Json_response( |
||
517 | $this->convert_exception_to_array( $e ), |
||
518 | $this->is_http_exception( $e ) ? $e->getStatusCode() : 500, |
||
519 | $this->is_http_exception( $e ) ? $e->getHeaders() : array(), |
||
520 | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES |
||
521 | ); |
||
522 | } |
||
523 | |||
524 | /** |
||
525 | * Convert the given exception to an array. |
||
526 | * |
||
527 | * @param \Throwable $e The throwable exception. |
||
528 | * |
||
529 | * @return array |
||
530 | */ |
||
531 | protected function convert_exception_to_array( Throwable $e ) { |
||
532 | return $this->container['config']['app.debug'] ? array( |
||
533 | 'message' => $e->getMessage(), |
||
534 | 'exception' => get_class( $e ), |
||
535 | 'file' => $e->getFile(), |
||
536 | 'line' => $e->getLine(), |
||
537 | 'trace' => collect( $e->getTrace() )->map( |
||
538 | function ( $trace ) { |
||
539 | return Arr::except( $trace, array( 'args' ) ); |
||
540 | } |
||
541 | )->all(), |
||
542 | ) : array( |
||
543 | 'message' => $this->is_http_exception( $e ) ? $e->getMessage() : 'Server Error', |
||
544 | ); |
||
545 | } |
||
546 | |||
547 | /** |
||
548 | * Render an exception to the console. |
||
549 | * |
||
550 | * @param \Symfony\Component\Console\Output\OutputInterface $output The symfony console output. |
||
551 | * @param \Throwable $e The throwable exception. |
||
552 | * |
||
553 | * @return void |
||
554 | */ |
||
555 | public function render_for_console( $output, Throwable $e ) { |
||
556 | ( new ConsoleApplication() )->renderThrowable( $e, $output ); |
||
557 | } |
||
558 | |||
559 | /** |
||
560 | * Determine if the given exception is an HTTP exception. |
||
561 | * |
||
562 | * @param \Throwable $e The throwable exception. |
||
563 | * |
||
564 | * @return bool |
||
565 | */ |
||
566 | protected function is_http_exception( Throwable $e ) { |
||
568 | } |
||
569 | } |
||
570 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths