WhoopsExceptionHandler::convertExceptionToWhoops()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 6
nc 4
nop 1
1
<?php
2
namespace CheatCodes\LaravelWhoops;
3
4
use App\Exceptions\Handler;
5
use Exception;
6
use Request;
7
use Whoops\Run as Whoops;
8
use Whoops\Handler\PrettyPageHandler;
9
use Whoops\Handler\JsonResponseHandler;
10
11
class WhoopsExceptionHandler extends Handler
12
{
13
    /**
14
     * Get the Whoops Handler instance to use
15
     *
16
     * @return \Whoops\Handler\Handler
17
     */
18
    protected function getWhoopsHandler()
19
    {
20
        $ajaxResponse = (Request::ajax() || Request::wantsJson());
21
22
        return $ajaxResponse ? new JsonResponseHandler() : new PrettyPageHandler();
23
    }
24
25
    /**
26
     * Create a Whoops response for the given exception
27
     *
28
     * @param Exception $e
29
     * @return \Illuminate\Http\Response
30
     */
31
    protected function convertExceptionToWhoops(Exception $e)
32
    {
33
        $whoops = new Whoops();
34
        $whoops->pushHandler($this->getWhoopsHandler());
35
36
        $statusCode = method_exists($e, 'getStatusCode') ? $e->getStatusCode() : 500;
37
        $headers = method_exists($e, 'getHeaders') ? $e->getHeaders() : [];
38
39
        return response()->make($whoops->handleException($e), $statusCode, $headers);
0 ignored issues
show
Documentation introduced by
$e is of type object<Exception>, but the function expects a object<Throwable>.

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...
40
    }
41
42
    /**
43
     * Create the default or a Whoops response for the given exception
44
     *
45
     * @param Exception $e
46
     * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
47
     */
48
    protected function convertExceptionToResponse(Exception $e)
49
    {
50
        return config('app.debug')
51
            ? $this->convertExceptionToWhoops($e)
52
            : parent::convertExceptionToResponse($e);
53
    }
54
}
55