Completed
Push — master ( 00ed35...63fbb3 )
by Oscar
10:20
created

Whoops::whoops()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Middleware;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Whoops\Run;
9
use Whoops\Handler\PrettyPageHandler;
10
use Whoops\Handler\PlainTextHandler;
11
use Whoops\Handler\JsonResponseHandler;
12
13
/**
14
 * Middleware to use whoops as error handler.
15
 */
16
class Whoops
17
{
18
    /**
19
     * @var Run|null To handle errors using whoops
20
     */
21
    private $whoops;
22
23
    /**
24
     * @var bool Whether catch errors or not
25
     */
26
    private $catchErrors = true;
27
28
    /**
29
     * Set the whoops instance.
30
     *
31
     * @param Run|null $whoops
32
     */
33
    public function __construct(Run $whoops = null)
34
    {
35
        $this->whoops = $whoops;
36
    }
37
38
    /**
39
     * Whether catch errors or not.
40
     *
41
     * @param bool $catchErrors
42
     *
43
     * @return self
44
     */
45
    public function catchErrors($catchErrors = true)
46
    {
47
        $this->catchErrors = (bool) $catchErrors;
48
49
        return $this;
50
    }
51
52
    /**
53
     * Execute the middleware.
54
     *
55
     * @param ServerRequestInterface $request
56
     * @param ResponseInterface      $response
57
     * @param callable               $next
58
     *
59
     * @return ResponseInterface
60
     */
61
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
62
    {
63
        $whoops = $this->getWhoopsInstance($request);
64
65
        //Catch errors means register whoops globally
66
        if ($this->catchErrors) {
67
            $whoops->register();
68
        }
69
70
        try {
71
            $response = $next($request, $response);
72
        } catch (\Exception $exception) {
73
            $method = Run::EXCEPTION_HANDLER;
74
75
            $whoops->allowQuit(false);
76
            $whoops->writeToOutput(false);
77
78
            $body = Middleware::createStream();
79
            $body->write($whoops->$method($exception));
80
81
            $response = $response->withStatus(500)->withBody($body);
82
        }
83
84
        $whoops->unregister();
85
86
        return $response;
87
    }
88
89
    /**
90
     * Returns the whoops instance or create one.
91
     * 
92
     * @param ServerRequestInterface $request
93
     *
94
     * @return Run
95
     */
96
    private function getWhoopsInstance(ServerRequestInterface $request)
97
    {
98
        if ($this->whoops) {
99
            return $this->whoops;
100
        }
101
102
        $whoops = new Run();
103
104
        //Is ajax?
105
        if (strtolower($request->getHeaderLine('X-Requested-With')) === 'xmlhttprequest') {
106
            $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...
107
        } else {
108
            $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...
109
        }
110
111
        //Command line
112
        $whoops->pushHandler(new PlainTextHandler());
0 ignored issues
show
Documentation introduced by
new \Whoops\Handler\PlainTextHandler() is of type object<Whoops\Handler\PlainTextHandler>, 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...
113
114
        return $whoops;
115
    }
116
}
117