Handler   D
last analyzed

Complexity

Total Complexity 59

Size/Duplication

Total Lines 539
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 160
dl 0
loc 539
rs 4.08
c 1
b 0
f 0
wmc 59

26 Methods

Rating   Name   Duplication   Size   Complexity  
A context() 0 11 2
A get_http_exception_view() 0 3 1
A render_exception_with_symfony() 0 5 1
A __construct() 0 3 1
A convert_validation_exception_to_response() 0 9 3
A invalid() 0 5 1
A prepare_response() 0 13 4
A convert_exception_to_response() 0 6 3
A shouldnt_report() 0 9 1
A is_http_exception() 0 3 1
A prepare_json_response() 0 7 3
A register_error_view_paths() 0 11 1
A render_for_console() 0 3 1
A prepare_exception() 0 13 5
A convert_exception_to_array() 0 14 3
A invalid_json() 0 8 1
A should_report() 0 3 1
A exception_context() 0 3 1
A render_exception_with_whoops() 0 12 1
B render() 0 22 8
A unauthenticated() 0 5 2
A render_exception_content() 0 8 4
A to_illuminate_response() 0 17 2
A report() 0 26 4
A whoops_handler() 0 6 2
A render_http_exception() 0 18 2

How to fix   Complexity   

Complex Class

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
2
/**
3
 * The file handle the errors.
4
 *
5
 * @link       https://github.com/maab16
6
 * @since      1.0.0
7
 */
8
9
namespace WPB\Exceptions;
10
11
use Exception;
12
use Illuminate\Auth\Access\AuthorizationException;
0 ignored issues
show
Bug introduced by
The type Illuminate\Auth\Access\AuthorizationException was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Illuminate\Auth\AuthenticationException;
0 ignored issues
show
Bug introduced by
The type Illuminate\Auth\AuthenticationException was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Illuminate\Contracts\Container\BindingResolutionException;
15
use Illuminate\Contracts\Container\Container;
16
use Illuminate\Contracts\Support\Responsable;
17
use Illuminate\Database\Eloquent\ModelNotFoundException;
18
use Illuminate\Http\Exceptions\HttpResponseException;
19
use Illuminate\Http\RedirectResponse;
20
use Illuminate\Http\Response;
21
use Illuminate\Routing\Router;
22
use Illuminate\Session\TokenMismatchException;
23
use Illuminate\Support\Arr;
24
use Illuminate\Support\Facades\Auth;
25
use Illuminate\Support\Facades\View;
26
use Illuminate\Support\ViewErrorBag;
27
use Illuminate\Validation\ValidationException;
0 ignored issues
show
Bug introduced by
The type Illuminate\Validation\ValidationException was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use Psr\Log\LoggerInterface;
29
use Symfony\Component\Console\Application as ConsoleApplication;
30
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
31
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
32
use Symfony\Component\HttpFoundation\RedirectResponse as SymfonyRedirectResponse;
33
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
34
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
35
use Symfony\Component\HttpKernel\Exception\HttpException;
36
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
37
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
38
use Throwable;
39
use Whoops\Handler\HandlerInterface;
0 ignored issues
show
Bug introduced by
The type Whoops\Handler\HandlerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
use Whoops\Run as Whoops;
0 ignored issues
show
Bug introduced by
The type Whoops\Run was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
41
use WPB\Contracts\ExceptionHandler as ExceptionHandlerContract;
42
43
/**
44
 * The exception handler.
45
 *
46
 * @since      1.0.0
47
 *
48
 * @author     Md Abu Ahsan basir <[email protected]>
49
 */
50
class Handler implements ExceptionHandlerContract
51
{
52
    /**
53
     * The container implementation.
54
     *
55
     * @var \Illuminate\Contracts\Container\Container
56
     */
57
    protected $container;
58
59
    /**
60
     * A list of the exception types that are not reported.
61
     *
62
     * @var array
63
     */
64
    protected $dont_report = [];
65
66
    /**
67
     * A list of the internal exception types that should not be reported.
68
     *
69
     * @var array
70
     */
71
    protected $internal_dont_report = [
72
        AuthenticationException::class,
73
        AuthorizationException::class,
74
        HttpException::class,
75
        HttpResponseException::class,
76
        ModelNotFoundException::class,
77
        SuspiciousOperationException::class,
78
        TokenMismatchException::class,
79
        ValidationException::class,
80
    ];
81
82
    /**
83
     * A list of the inputs that are never flashed for validation exceptions.
84
     *
85
     * @var array
86
     */
87
    protected $dont_flash = [
88
        'password',
89
        'password_confirmation',
90
    ];
91
92
    /**
93
     * Create a new exception handler instance.
94
     *
95
     * @param \Illuminate\Contracts\Container\Container $container The app container.
96
     *
97
     * @return void
98
     */
99
    public function __construct(Container $container)
100
    {
101
        $this->container = $container;
102
    }
103
104
    /**
105
     * Report or log an exception.
106
     *
107
     * @param \Throwable $e The throwable exception.
108
     *
109
     * @throws \Exception Throw the exception.
110
     *
111
     * @return void
112
     */
113
    public function report(Throwable $e)
114
    {
115
        if ($this->shouldnt_report($e)) {
116
            return;
117
        }
118
119
        $report_callable = [$e, 'report'];
120
121
        if (is_callable($report_callable)) {
122
            $this->container->call($report_callable);
123
124
            return;
125
        }
126
127
        try {
128
            $logger = $this->container->make(LoggerInterface::class);
129
        } catch (Exception $ex) {
130
            throw $e;
131
        }
132
133
        $logger->error(
134
            $e->getMessage(),
135
            array_merge(
136
                $this->exception_context($e),
137
                $this->context(),
138
                ['exception' => $e]
139
            )
140
        );
141
    }
142
143
    /**
144
     * Determine if the exception should be reported.
145
     *
146
     * @param \Throwable $e The throwable exception.
147
     *
148
     * @return bool
149
     */
150
    public function should_report(Throwable $e)
151
    {
152
        return !$this->shouldnt_report($e);
153
    }
154
155
    /**
156
     * Determine if the exception is in the "do not report" list.
157
     *
158
     * @param \Throwable $e The throwable exception.
159
     *
160
     * @return bool
161
     */
162
    protected function shouldnt_report(Throwable $e)
163
    {
164
        $dont_report = array_merge($this->dont_report, $this->internal_dont_report);
165
166
        return !is_null(
167
            Arr::first(
168
                $dont_report,
169
                function ($type) use ($e) {
170
                    return $e instanceof $type;
171
                }
172
            )
173
        );
174
    }
175
176
    /**
177
     * Get the default exception context variables for logging.
178
     *
179
     * @param \Throwable $e The throwable exception.
180
     *
181
     * @return array
182
     */
183
    protected function exception_context(Throwable $e)
0 ignored issues
show
Unused Code introduced by
The parameter $e is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

183
    protected function exception_context(/** @scrutinizer ignore-unused */ Throwable $e)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
184
    {
185
        return [];
186
    }
187
188
    /**
189
     * Get the default context variables for logging.
190
     *
191
     * @return array
192
     */
193
    protected function context()
194
    {
195
        try {
196
            return array_filter(
197
                [
198
                    'userId' => Auth::id(),
199
                    // 'email' => optional(Auth::user())->email,
200
                ]
201
            );
202
        } catch (Throwable $e) {
203
            return [];
204
        }
205
    }
206
207
    /**
208
     * Render an exception into an HTTP response.
209
     *
210
     * @param \Illuminate\Http\Request $request The app request.
211
     * @param \Throwable               $e       The throwable exception.
212
     *
213
     * @throws \Throwable Throw the exception.
214
     *
215
     * @return \Symfony\Component\HttpFoundation\Response
216
     */
217
    public function render($request, Throwable $e)
218
    {
219
        $response = $e->render($request);
0 ignored issues
show
Bug introduced by
The method render() does not exist on Throwable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

219
        /** @scrutinizer ignore-call */ 
220
        $response = $e->render($request);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
220
        if (method_exists($e, 'render') && $response) {
221
            return Router::toResponse($request, $response);
222
        } elseif ($e instanceof Responsable) {
223
            return $e->toResponse($request);
224
        }
225
226
        $e = $this->prepare_exception($e);
227
228
        if ($e instanceof HttpResponseException) {
229
            return $e->getResponse();
230
        } elseif ($e instanceof AuthenticationException) {
231
            return $this->unauthenticated($request, $e);
232
        } elseif ($e instanceof ValidationException) {
233
            return $this->convert_validation_exception_to_response($e, $request);
234
        }
235
236
        return $request->expectsJson()
237
                    ? $this->prepare_json_response($request, $e)
238
                    : $this->prepare_response($request, $e);
239
    }
240
241
    /**
242
     * Prepare exception for rendering.
243
     *
244
     * @param \Throwable $e The throwable exception.
245
     *
246
     * @return \Throwable
247
     */
248
    protected function prepare_exception(Throwable $e)
249
    {
250
        if ($e instanceof ModelNotFoundException) {
251
            $e = new NotFoundHttpException($e->getMessage(), $e);
252
        } elseif ($e instanceof AuthorizationException) {
253
            $e = new AccessDeniedHttpException($e->getMessage(), $e);
254
        } elseif ($e instanceof TokenMismatchException) {
255
            $e = new HttpException(419, $e->getMessage(), $e);
256
        } elseif ($e instanceof SuspiciousOperationException) {
257
            $e = new NotFoundHttpException('Bad hostname provided.', $e);
258
        }
259
260
        return $e;
261
    }
262
263
    /**
264
     * Convert an authentication exception into a response.
265
     *
266
     * @param \Illuminate\Http\Request                 $request   The app request.
267
     * @param \Illuminate\Auth\AuthenticationException $exception The authenticated exception.
268
     *
269
     * @return \Symfony\Component\HttpFoundation\Response
270
     */
271
    protected function unauthenticated($request, AuthenticationException $exception)
272
    {
273
        return $request->expectsJson()
274
                    ? response()->json(['message' => $exception->getMessage()], 401)
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

274
                    ? /** @scrutinizer ignore-call */ response()->json(['message' => $exception->getMessage()], 401)
Loading history...
275
                    : redirect()->guest($exception->redirectTo() ?? route('login'));
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

275
                    : redirect()->guest($exception->redirectTo() ?? /** @scrutinizer ignore-call */ route('login'));
Loading history...
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

275
                    : /** @scrutinizer ignore-call */ redirect()->guest($exception->redirectTo() ?? route('login'));
Loading history...
276
    }
277
278
    /**
279
     * Create a response object from the given validation exception.
280
     *
281
     * @param \Illuminate\Validation\ValidationException $e       The validation exception.
282
     * @param \Illuminate\Http\Request                   $request The app request.
283
     *
284
     * @return \Symfony\Component\HttpFoundation\Response
285
     */
286
    protected function convert_validation_exception_to_response(ValidationException $e, $request)
287
    {
288
        if ($e->response) {
289
            return $e->response;
290
        }
291
292
        return $request->expectsJson()
293
                    ? $this->invalid_json($request, $e)
294
                    : $this->invalid($request, $e);
295
    }
296
297
    /**
298
     * Convert a validation exception into a response.
299
     *
300
     * @param \Illuminate\Http\Request                   $request   The app request.
301
     * @param \Illuminate\Validation\ValidationException $exception The validation exception.
302
     *
303
     * @return \Illuminate\Http\Response
304
     */
305
    protected function invalid($request, ValidationException $exception)
306
    {
307
        return redirect($exception->redirectTo ?? url()->previous())
0 ignored issues
show
Bug introduced by
The function url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

307
        return redirect($exception->redirectTo ?? /** @scrutinizer ignore-call */ url()->previous())
Loading history...
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

307
        return /** @scrutinizer ignore-call */ redirect($exception->redirectTo ?? url()->previous())
Loading history...
308
                    ->withInput(Arr::except($request->input(), $this->dont_flash))
309
                    ->withErrors($exception->errors(), $exception->errorBag);
310
    }
311
312
    /**
313
     * Convert a validation exception into a JSON response.
314
     *
315
     * @param \Illuminate\Http\Request                   $request   The app request.
316
     * @param \Illuminate\Validation\ValidationException $exception The validation exception.
317
     *
318
     * @return \Illuminate\Http\JsonResponse
319
     */
320
    protected function invalid_json($request, ValidationException $exception)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

320
    protected function invalid_json(/** @scrutinizer ignore-unused */ $request, ValidationException $exception)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
321
    {
322
        return response()->json(
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

322
        return /** @scrutinizer ignore-call */ response()->json(
Loading history...
323
            [
324
                'message' => $exception->getMessage(),
325
                'errors'  => $exception->errors(),
326
            ],
327
            $exception->status
328
        );
329
    }
330
331
    /**
332
     * Prepare a response for the given exception.
333
     *
334
     * @param \Illuminate\Http\Request $request The app request.
335
     * @param \Throwable               $e       The throwable exception.
336
     *
337
     * @return \Symfony\Component\HttpFoundation\Response
338
     */
339
    protected function prepare_response($request, Throwable $e)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

339
    protected function prepare_response(/** @scrutinizer ignore-unused */ $request, Throwable $e)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
340
    {
341
        if (!$this->is_http_exception($e) && $this->container['config']['app.debug']) {
342
            return $this->to_illuminate_response($this->convert_exception_to_response($e), $e);
343
        }
344
345
        if (!$this->is_http_exception($e)) {
346
            $e = new HttpException(500, $e->getMessage());
347
        }
348
349
        return $this->to_illuminate_response(
350
            $this->render_http_exception($e),
351
            $e
352
        );
353
    }
354
355
    /**
356
     * Create a Symfony response for the given exception.
357
     *
358
     * @param \Throwable $e The throwable exception.
359
     *
360
     * @return \Symfony\Component\HttpFoundation\Response
361
     */
362
    protected function convert_exception_to_response(Throwable $e)
363
    {
364
        return SymfonyResponse::create(
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\HttpFo...tion\Response::create() has been deprecated: since Symfony 5.1, use __construct() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

364
        return /** @scrutinizer ignore-deprecated */ SymfonyResponse::create(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
365
            $this->render_exception_content($e),
366
            $this->is_http_exception($e) ? $e->getStatusCode() : 500,
0 ignored issues
show
Bug introduced by
The method getStatusCode() does not exist on Throwable. It seems like you code against a sub-type of Throwable such as Symfony\Component\HttpKe...\HttpExceptionInterface or Symfony\Component\HttpKe...Exception\HttpException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

366
            $this->is_http_exception($e) ? $e->/** @scrutinizer ignore-call */ getStatusCode() : 500,
Loading history...
367
            $this->is_http_exception($e) ? $e->getHeaders() : []
0 ignored issues
show
Bug introduced by
The method getHeaders() does not exist on Throwable. It seems like you code against a sub-type of Throwable such as Symfony\Component\HttpKe...\HttpExceptionInterface or Symfony\Component\HttpKe...Exception\HttpException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

367
            $this->is_http_exception($e) ? $e->/** @scrutinizer ignore-call */ getHeaders() : []
Loading history...
368
        );
369
    }
370
371
    /**
372
     * Get the response content for the given exception.
373
     *
374
     * @param \Throwable $e The throwable exception.
375
     *
376
     * @return string
377
     */
378
    protected function render_exception_content(Throwable $e)
379
    {
380
        try {
381
            return $this->container['config']['app.debug'] && class_exists(Whoops::class)
382
                        ? $this->render_exception_with_whoops($e)
383
                        : $this->render_exception_with_symfony($e, $this->container['config']['app.debug']);
384
        } catch (Exception $e) {
385
            return $this->render_exception_with_symfony($e, $this->container['config']['app.debug']);
386
        }
387
    }
388
389
    /**
390
     * Render an exception to a string using "Whoops".
391
     *
392
     * @param \Throwable $e The throwable exception.
393
     *
394
     * @return string
395
     */
396
    protected function render_exception_with_whoops(Throwable $e)
397
    {
398
        return tap(
399
            new Whoops(),
400
            function ($whoops) {
401
                $whoops->appendHandler($this->whoops_handler());
402
403
                $whoops->writeToOutput(false);
404
405
                $whoops->allowQuit(false);
406
            }
407
        )->handleException($e);
408
    }
409
410
    /**
411
     * Get the Whoops handler for the application.
412
     *
413
     * @return \Whoops\Handler\Handler
0 ignored issues
show
Bug introduced by
The type Whoops\Handler\Handler was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
414
     */
415
    protected function whoops_handler()
416
    {
417
        try {
418
            return $this->container(HandlerInterface::class);
0 ignored issues
show
Bug introduced by
The method container() does not exist on WPB\Exceptions\Handler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

418
            return $this->/** @scrutinizer ignore-call */ container(HandlerInterface::class);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
419
        } catch (BindingResolutionException $e) {
420
            return ( new WhoopsHandler() )->forDebug();
0 ignored issues
show
Bug introduced by
The type WPB\Exceptions\WhoopsHandler was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
421
        }
422
    }
423
424
    /**
425
     * Render an exception to a string using Symfony.
426
     *
427
     * @param \Throwable $e     The throwable exception.
428
     * @param bool       $debug Enable or disable debug.
429
     *
430
     * @return string
431
     */
432
    protected function render_exception_with_symfony(Throwable $e, $debug)
433
    {
434
        $renderer = new HtmlErrorRenderer($debug);
435
436
        return $renderer->getBody($renderer->render($e));
437
    }
438
439
    /**
440
     * Render the given HttpException.
441
     *
442
     * @param \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $e The http exception.
443
     *
444
     * @return \Symfony\Component\HttpFoundation\Response
445
     */
446
    protected function render_http_exception(HttpExceptionInterface $e)
447
    {
448
        $this->register_error_view_paths();
449
        $view = $this->get_http_exception_view($e);
450
451
        if (view()->exists($view)) {
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

451
        if (/** @scrutinizer ignore-call */ view()->exists($view)) {
Loading history...
452
            return response()->view(
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

452
            return /** @scrutinizer ignore-call */ response()->view(
Loading history...
453
                $view,
454
                [
455
                    'errors'    => new ViewErrorBag(),
456
                    'exception' => $e,
457
                ],
458
                $e->getStatusCode(),
459
                $e->getHeaders()
460
            );
461
        }
462
463
        return $this->convert_exception_to_response($e);
464
    }
465
466
    /**
467
     * Register the error template hint paths.
468
     *
469
     * @return void
470
     */
471
    protected function register_error_view_paths()
472
    {
473
        $paths = collect($this->container['config']['view.paths']);
474
475
        View::replaceNamespace(
476
            'errors',
477
            $paths->map(
478
                function ($path) {
479
                    return "{$path}/errors";
480
                }
481
            )->push(__DIR__.'/views')->all()
482
        );
483
    }
484
485
    /**
486
     * Get the view used to render HTTP exceptions.
487
     *
488
     * @param \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $e The http exceptions.
489
     *
490
     * @return string
491
     */
492
    protected function get_http_exception_view(HttpExceptionInterface $e)
493
    {
494
        return "errors::{$e->getStatusCode()}";
495
    }
496
497
    /**
498
     * Map the given exception into an Illuminate response.
499
     *
500
     * @param \Symfony\Component\HttpFoundation\Response $response The app http response.
501
     * @param \Throwable                                 $e        The throwable exception.
502
     *
503
     * @return \Illuminate\Http\Response
504
     */
505
    protected function to_illuminate_response($response, Throwable $e)
506
    {
507
        if ($response instanceof SymfonyRedirectResponse) {
508
            $response = new RedirectResponse(
509
                $response->getTargetUrl(),
510
                $response->getStatusCode(),
511
                $response->headers->all()
512
            );
513
        } else {
514
            $response = new Response(
515
                $response->getContent(),
516
                $response->getStatusCode(),
517
                $response->headers->all()
518
            );
519
        }
520
521
        return $response->withException($e);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response->withException($e) also could return the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
522
    }
523
524
    /**
525
     * Prepare a JSON response for the given exception.
526
     *
527
     * @param \Illuminate\Http\Request $request The app http request.
528
     * @param \Throwable               $e       The throwable exception.
529
     *
530
     * @return \Illuminate\Http\JsonResponse
531
     */
532
    protected function prepare_json_response($request, Throwable $e)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

532
    protected function prepare_json_response(/** @scrutinizer ignore-unused */ $request, Throwable $e)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
533
    {
534
        return new Json_response(
0 ignored issues
show
Bug introduced by
The type WPB\Exceptions\Json_response was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
535
            $this->convert_exception_to_array($e),
536
            $this->is_http_exception($e) ? $e->getStatusCode() : 500,
537
            $this->is_http_exception($e) ? $e->getHeaders() : [],
538
            JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
539
        );
540
    }
541
542
    /**
543
     * Convert the given exception to an array.
544
     *
545
     * @param \Throwable $e The throwable exception.
546
     *
547
     * @return array
548
     */
549
    protected function convert_exception_to_array(Throwable $e)
550
    {
551
        return $this->container['config']['app.debug'] ? [
552
            'message'   => $e->getMessage(),
553
            'exception' => get_class($e),
554
            'file'      => $e->getFile(),
555
            'line'      => $e->getLine(),
556
            'trace'     => collect($e->getTrace())->map(
557
                function ($trace) {
558
                    return Arr::except($trace, ['args']);
559
                }
560
            )->all(),
561
        ] : [
562
            'message' => $this->is_http_exception($e) ? $e->getMessage() : 'Server Error',
563
        ];
564
    }
565
566
    /**
567
     * Render an exception to the console.
568
     *
569
     * @param \Symfony\Component\Console\Output\OutputInterface $output The symfony console output.
570
     * @param \Throwable                                        $e      The throwable exception.
571
     *
572
     * @return void
573
     */
574
    public function render_for_console($output, Throwable $e)
575
    {
576
        ( new ConsoleApplication() )->renderThrowable($e, $output);
577
    }
578
579
    /**
580
     * Determine if the given exception is an HTTP exception.
581
     *
582
     * @param \Throwable $e The throwable exception.
583
     *
584
     * @return bool
585
     */
586
    protected function is_http_exception(Throwable $e)
587
    {
588
        return $e instanceof HttpExceptionInterface;
589
    }
590
}
591