Total Complexity | 45 |
Total Lines | 398 |
Duplicated Lines | 0 % |
Changes | 2 | ||
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 |
||
44 | class Handler implements ExceptionHandlerContract |
||
45 | { |
||
46 | /** |
||
47 | * The container implementation. |
||
48 | * |
||
49 | * @var \Syscodes\Contracts\Container\Container $container |
||
|
|||
50 | */ |
||
51 | protected $container; |
||
52 | |||
53 | /** |
||
54 | * A list of the exception types that should not be reported. |
||
55 | * |
||
56 | * @var array $dontReport |
||
57 | */ |
||
58 | protected $dontReport = []; |
||
59 | |||
60 | /** |
||
61 | * A list of the Core exception types that should not be reported. |
||
62 | * |
||
63 | * @var array $coreDontReport |
||
64 | */ |
||
65 | protected $coreDontReport = [ |
||
66 | HttpException::class, |
||
67 | HttpResponseException::class, |
||
68 | ModelNotFoundException::class, |
||
69 | ]; |
||
70 | |||
71 | /** |
||
72 | * The callbacks that should be used during reporting. |
||
73 | * |
||
74 | * @var array $reportCallbacks |
||
75 | */ |
||
76 | protected $reportCallbacks = []; |
||
77 | |||
78 | /** |
||
79 | * The callbacks that should be used during rendering. |
||
80 | * |
||
81 | * @var array $renderCallbacks |
||
82 | */ |
||
83 | protected $renderCallbacks = []; |
||
84 | |||
85 | /** |
||
86 | * Constructor. Create a new exception handler instance. |
||
87 | * |
||
88 | * @param \Syscodes\Components\Contracts\Container\Container $container |
||
89 | * |
||
90 | * @return void |
||
91 | */ |
||
92 | public function __construct(Container $container) |
||
93 | { |
||
94 | $this->container = $container; |
||
95 | |||
96 | $this->register(); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Register the exception handling with callbacks for the application. |
||
101 | * |
||
102 | * @return void |
||
103 | */ |
||
104 | public function register() {} |
||
105 | |||
106 | /** |
||
107 | * Register a reportable callback. |
||
108 | * |
||
109 | * @param \callable $callback |
||
110 | * |
||
111 | * @return static |
||
112 | */ |
||
113 | public function reportable(callable $callback): static |
||
114 | { |
||
115 | $this->reportCallbacks[] = $callback; |
||
116 | |||
117 | return $this; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Register a renderable callback. |
||
122 | * |
||
123 | * @param \callable $callback |
||
124 | * |
||
125 | * @return static |
||
126 | */ |
||
127 | public function renderable(callable $callback): static |
||
128 | { |
||
129 | $this->renderCallbacks[] = $callback; |
||
130 | |||
131 | return $this; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Report or log an exception. |
||
136 | * |
||
137 | * @param \Throwable $e |
||
138 | * |
||
139 | * @return mixed |
||
140 | * |
||
141 | * @throws \Exception |
||
142 | */ |
||
143 | public function report(Throwable $e) |
||
144 | { |
||
145 | if ($this->shouldntReport($e)) { |
||
146 | return; |
||
147 | } |
||
148 | |||
149 | if (method_exists($e, 'report')) { |
||
150 | return $e->report($e); |
||
151 | } |
||
152 | |||
153 | foreach ($this->reportCallbacks as $reportCallback) { |
||
154 | if ($reportCallback($e) === false) { |
||
155 | return; |
||
156 | } |
||
157 | } |
||
158 | |||
159 | try { |
||
160 | $logger = $this->container->make(LoggerInterface::class); |
||
161 | } catch (Exception $e) { |
||
162 | throw $e; |
||
163 | } |
||
164 | |||
165 | $logger->error($e->getMessage()); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Determine if the exception should be reported. |
||
170 | * |
||
171 | * @param \Throwable $e |
||
172 | * |
||
173 | * @return bool |
||
174 | */ |
||
175 | public function shouldReport(Throwable $e): bool |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Determine if the exception is in the "do not report" list. |
||
182 | * |
||
183 | * @param \Throwable $e |
||
184 | * |
||
185 | * @return bool |
||
186 | */ |
||
187 | public function shouldntReport(Throwable $e): bool |
||
188 | { |
||
189 | $dontReport = array_merge($this->dontReport, $this->coreDontReport); |
||
190 | |||
191 | foreach ($dontReport as $type) { |
||
192 | if ($e instanceof $type) { |
||
193 | return true; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | return false; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Render an exception into an HTTP response. |
||
202 | * |
||
203 | * @param \Syscodes\Components\Http\Request $request |
||
204 | * @param \Throwable $e |
||
205 | * |
||
206 | * @return \Syscodes\Components\Http\Response |
||
207 | */ |
||
208 | public function render($request, Throwable $e) |
||
209 | { |
||
210 | if (method_exists($e, 'render') && $response = $e->render($request)) { |
||
211 | return Router::toResponse($request, $response); |
||
212 | } |
||
213 | |||
214 | $e = $this->prepareException($e); |
||
215 | |||
216 | foreach ($this->renderCallbacks as $renderCallback) { |
||
217 | $response = $renderCallback($e, $request); |
||
218 | |||
219 | if ( ! is_null($response)) { |
||
220 | return $response; |
||
221 | } |
||
222 | } |
||
223 | |||
224 | foreach ($this->renderCallbacks as $renderCallback) { |
||
225 | $response = $renderCallback($e, $request); |
||
226 | |||
227 | if ( ! is_null($response)) { |
||
228 | return $response; |
||
229 | } |
||
230 | } |
||
231 | |||
232 | if ($e instanceof HttpResponseException) { |
||
233 | $e->getResponse(); |
||
234 | } |
||
235 | |||
236 | return $this->prepareResponse($request, $e); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Prepare exception for rendering. |
||
241 | * |
||
242 | * @param \Throwable $e |
||
243 | * |
||
244 | * @return \Throwable |
||
245 | */ |
||
246 | protected function prepareException(Throwable $e): Throwable |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Prepare a response for the given exception. |
||
257 | * |
||
258 | * @param \Syscodes\Components\Http\Request $request |
||
259 | * @param \Throwable $e |
||
260 | * |
||
261 | * @return \Syscodes\Components\Http\Response |
||
262 | * |
||
263 | * @uses \Syscodes\Components\Core\Http\Exceptions\HttpException |
||
264 | */ |
||
265 | protected function prepareResponse($request, Throwable $e) |
||
266 | { |
||
267 | if ( ! $this->isHttpException($e) && config('app.debug')) { |
||
268 | return $this->toSyscodesResponse($this->convertExceptionToResponse($e), $e); |
||
269 | } |
||
270 | |||
271 | // When the debug is not active, the HTTP 500 code view is throw |
||
272 | if ( ! $this->isHttpException($e)) { |
||
273 | $e = new HttpException(500, $e->getMessage()); |
||
274 | } |
||
275 | |||
276 | return $this->toSyscodesResponse($this->renderHttpException($e), $e); |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Render the given HttpException. |
||
281 | * |
||
282 | * @param \Syscodes\Components\Core\Http\Exceptions\HttpException $e |
||
283 | * |
||
284 | * @return \Syscodes\Components\Http\Response |
||
285 | */ |
||
286 | protected function renderHttpException(HttpException $e) |
||
287 | { |
||
288 | $this->registerViewErrorPaths(); |
||
289 | |||
290 | if (view()->viewExists($view = $this->getHttpExceptionView($e))) { |
||
291 | return response()->view( |
||
292 | $view, |
||
293 | ['exception' => $e], |
||
294 | $e->getStatusCode(), |
||
295 | $e->getHeaders() |
||
296 | ); |
||
297 | } |
||
298 | |||
299 | return $this->convertExceptionToResponse($e); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Register the error view paths. |
||
304 | * |
||
305 | * @return void |
||
306 | */ |
||
307 | protected function registerViewErrorPaths(): void |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Get the view used to render HTTP exceptions. |
||
314 | * |
||
315 | * @param \Syscodes\Components\Core\Http\Exceptions\HttpException $e |
||
316 | * |
||
317 | * @return string |
||
318 | */ |
||
319 | protected function getHttpExceptionView(HttpException $e): string |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Create a response for the given exception. |
||
326 | * |
||
327 | * @param \Throwable $e |
||
328 | * |
||
329 | * @return \Syscodes\Components\Http\Response |
||
330 | */ |
||
331 | protected function convertExceptionToResponse(Throwable $e) |
||
332 | { |
||
333 | return Response::render( |
||
334 | $this->renderExceptionContent($e), |
||
335 | $this->isHttpException($e) ? $e->getStatusCode() : 500, |
||
336 | $this->isHttpException($e) ? $e->getHeaders() : [] |
||
337 | ); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Gets the response content for the given exception. |
||
342 | * |
||
343 | * @param \Throwable $e |
||
344 | * |
||
345 | * @return string |
||
346 | */ |
||
347 | protected function renderExceptionContent(Throwable $e): string |
||
355 | } |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * Render an exception to a string of debug. |
||
360 | * |
||
361 | * @param \Throwable $e |
||
362 | * |
||
363 | * @return void |
||
364 | * |
||
365 | * @uses \Syscodes\Components\Contracts\Core\ExceptionRender |
||
366 | */ |
||
367 | protected function renderExceptionWithDebug(Throwable $e) |
||
368 | { |
||
369 | return app(ExceptionRender::class)->render($e); |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * Render an exception to a string using Flat Design Debug. |
||
374 | * |
||
375 | * @param \Throwable $e |
||
376 | * @param bool $debug |
||
377 | * |
||
378 | * @return string |
||
379 | */ |
||
380 | protected function renderExceptionWithFlatDesignDebug(Throwable $e, $debug) |
||
384 | ); |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Map the given exception into an Syscodes response. |
||
389 | * |
||
390 | * @param \Syscodes\Components\Http\Response $response |
||
391 | * @param \Exception $e |
||
392 | * |
||
393 | * @return \Syscodes\Components\Http\Response |
||
394 | */ |
||
395 | protected function toSyscodesResponse($response, Throwable $e) |
||
396 | { |
||
397 | if ($response instanceof RedirectResponse) { |
||
398 | $response = new RedirectResponse( |
||
399 | $response->getTargetUrl(), $response->status(), $response->headers->all() |
||
400 | ); |
||
401 | } else { |
||
402 | $response = new Response( |
||
403 | $response->content(), $response->status(), $response->headers->all() |
||
404 | ); |
||
405 | } |
||
406 | |||
407 | return $response->withException($e); |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * Render an exception to the console. |
||
412 | * |
||
413 | * @param \Syscodes\Components\Contracts\Console\Output\ConsoleOutput $output |
||
414 | * @param \Throwable $e |
||
415 | * |
||
416 | * @return void |
||
417 | */ |
||
418 | public function renderForConsole($output, Throwable $e) |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * Determine if the given exception is an HTTP exception. |
||
434 | * |
||
435 | * @param \Throwable $e |
||
436 | * |
||
437 | * @return bool |
||
438 | */ |
||
439 | protected function isHttpException(Throwable $e): bool |
||
442 | } |
||
443 | } |
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