Completed
Push — master ( b2e200...1332b8 )
by Oscar
04:48
created

Whoops   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 4
Bugs 3 Features 0
Metric Value
wmc 10
c 4
b 3
f 0
lcom 2
cbo 7
dl 0
loc 117
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A whoops() 0 6 1
A catchErrors() 0 6 1
B __invoke() 0 27 3
A getWhoopsInstance() 0 20 3
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
     * Constructor.Set the whoops instance.
30
     *
31
     * @param Run $whoops
32
     */
33
    public function __construct(Run $whoops = null)
34
    {
35
        if ($whoops !== null) {
36
            $this->whoops($whoops);
37
        }
38
    }
39
40
    /**
41
     * Set an instance of Whoops.
42
     *
43
     * @param Run $whoops
44
     *
45
     * @return self
46
     */
47
    public function whoops(Run $whoops)
48
    {
49
        $this->whoops = $whoops;
50
51
        return $this;
52
    }
53
54
    /**
55
     * Whether catch errors or not.
56
     *
57
     * @param bool $catchErrors
58
     *
59
     * @return self
60
     */
61
    public function catchErrors($catchErrors = true)
62
    {
63
        $this->catchErrors = (bool) $catchErrors;
64
65
        return $this;
66
    }
67
68
    /**
69
     * Execute the middleware.
70
     *
71
     * @param ServerRequestInterface $request
72
     * @param ResponseInterface      $response
73
     * @param callable               $next
74
     *
75
     * @return ResponseInterface
76
     */
77
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
78
    {
79
        $whoops = $this->getWhoopsInstance($request);
80
81
        //Catch errors means register whoops globally
82
        if ($this->catchErrors) {
83
            $whoops->register();
84
        }
85
86
        try {
87
            $response = $next($request, $response);
88
        } catch (\Exception $exception) {
89
            $method = Run::EXCEPTION_HANDLER;
90
91
            $whoops->allowQuit(false);
92
            $whoops->writeToOutput(false);
93
94
            $body = Middleware::createStream();
95
            $body->write($whoops->$method($exception));
96
97
            $response = $response->withStatus(500)->withBody($body);
98
        }
99
100
        $whoops->unregister();
101
102
        return $response;
103
    }
104
105
    /**
106
     * Returns the whoops instance or create one.
107
     * 
108
     * @param ServerRequestInterface $request
109
     *
110
     * @return Run
111
     */
112
    private function getWhoopsInstance(ServerRequestInterface $request)
113
    {
114
        if ($this->whoops) {
115
            return $this->whoops;
116
        }
117
118
        $whoops = new Run();
119
120
        //Is ajax?
121
        if (strtolower($request->getHeaderLine('X-Requested-With')) === 'xmlhttprequest') {
122
            $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...
123
        } else {
124
            $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...
125
        }
126
127
        //Command line
128
        $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...
129
130
        return $whoops;
131
    }
132
}
133