Handler::render()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
c 0
b 0
f 0
nc 6
nop 2
dl 0
loc 23
rs 8.5906
1
<?php
2
3
namespace PHPHub\Exceptions;
4
5
use Exception;
6
use Illuminate\Database\Eloquent\ModelNotFoundException;
7
use Symfony\Component\HttpKernel\Exception\HttpException;
8
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
10
use Whoops\Handler\JsonResponseHandler;
11
use Whoops\Handler\PrettyPageHandler;
12
use Whoops\Run as Whoops;
13
14
class Handler extends ExceptionHandler
15
{
16
    /**
17
     * A list of the exception types that should not be reported.
18
     *
19
     * @var array
20
     */
21
    protected $dontReport = [
22
        HttpException::class,
23
        ModelNotFoundException::class,
24
    ];
25
26
    /**
27
     * Report or log an exception.
28
     *
29
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
30
     *
31
     * @param \Exception $e
32
     */
33
    public function report(Exception $e)
34
    {
35
        return parent::report($e);
36
    }
37
38
    /**
39
     * Render an exception into an HTTP response.
40
     *
41
     * @param \Illuminate\Http\Request $request
42
     * @param \Exception               $e
43
     *
44
     * @return \Illuminate\Http\Response
45
     */
46
    public function render($request, Exception $e)
47
    {
48
        if ($e instanceof ModelNotFoundException) {
49
            $e = new NotFoundHttpException($e->getMessage(), $e);
50
        }
51
52
        if (config('app.debug')) {
53
            $whoops = new Whoops();
54
55
            if ($request->ajax() || str_contains($request->header('Accept'), 'json')) {
0 ignored issues
show
Bug introduced by
It seems like $request->header('Accept') targeting Illuminate\Http\Request::header() can also be of type array; however, str_contains() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
56
                $whoops->pushHandler(new JsonResponseHandler());
0 ignored issues
show
Documentation introduced by
new \Whoops\Handler\JsonResponseHandler() is of type object<Whoops\Handler\JsonResponseHandler>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
            } else {
58
                $whoops->pushHandler(new PrettyPageHandler());
0 ignored issues
show
Documentation introduced by
new \Whoops\Handler\PrettyPageHandler() is of type object<Whoops\Handler\PrettyPageHandler>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
            }
60
61
            return response($whoops->handleException($e),
62
                $e->getStatusCode(),
63
                $e->getHeaders()
64
            );
65
        }
66
67
        return parent::render($request, $e);
68
    }
69
}
70