Whoops::getWhoopsInstance()   C
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 47
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 5.1578
c 0
b 0
f 0
cc 10
eloc 30
nc 10
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Utils;
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
use Whoops\Handler\XmlResponseHandler;
13
14
/**
15
 * Middleware to use whoops as error handler.
16
 */
17
class Whoops
18
{
19
    use Utils\StreamTrait;
20
21
    /**
22
     * @var Run|null The provided instance of Whoops
23
     */
24
    private $whoops;
25
26
    /**
27
     * @var bool Whether catch errors or not
28
     */
29
    private $catchErrors = true;
30
31
    /**
32
     * Set the whoops instance.
33
     *
34
     * @param Run|null $whoops
35
     */
36
    public function __construct(Run $whoops = null)
37
    {
38
        $this->whoops = $whoops;
39
    }
40
41
    /**
42
     * Whether catch errors or not.
43
     *
44
     * @param bool $catchErrors
45
     *
46
     * @return self
47
     */
48
    public function catchErrors($catchErrors = true)
49
    {
50
        $this->catchErrors = (bool) $catchErrors;
51
52
        return $this;
53
    }
54
55
    /**
56
     * Execute the middleware.
57
     *
58
     * @param ServerRequestInterface $request
59
     * @param ResponseInterface      $response
60
     * @param callable               $next
61
     *
62
     * @return ResponseInterface
63
     */
64
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
65
    {
66
        ob_start();
67
        $level = ob_get_level();
68
69
        $method = Run::EXCEPTION_HANDLER;
70
        $whoops = $this->getWhoopsInstance($request);
71
72
        $whoops->allowQuit(false);
73
        $whoops->writeToOutput(false);
74
        $whoops->sendHttpCode(false);
75
76
        //Catch errors means register whoops globally
77
        if ($this->catchErrors) {
78
            $whoops->register();
79
        }
80
81
        try {
82
            $response = $next($request, $response);
83
        } catch (\Throwable $exception) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
84
            $body = self::createStream($response->getBody());
85
            $body->write($whoops->$method($exception));
86
            $response = $response->withStatus(500)->withBody($body);
87
        } catch (\Exception $exception) {
88
            $body = self::createStream($response->getBody());
89
            $body->write($whoops->$method($exception));
90
            $response = $response->withStatus(500)->withBody($body);
91
        } finally {
92
            Utils\Helpers::getOutput($level);
93
        }
94
95
        if ($this->catchErrors) {
96
            $whoops->unregister();
97
        }
98
99
        return $response;
100
    }
101
102
    /**
103
     * Returns the whoops instance or create one.
104
     *
105
     * @param ServerRequestInterface $request
106
     *
107
     * @return Run
108
     */
109
    private function getWhoopsInstance(ServerRequestInterface $request)
110
    {
111
        if ($this->whoops) {
112
            return $this->whoops;
113
        }
114
115
        $whoops = new Run();
116
117
        if (php_sapi_name() === 'cli') {
118
            $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...
119
120
            return $whoops;
121
        }
122
123
        $format = FormatNegotiator::getFormat($request);
124
125
        switch ($format) {
126
            case 'json':
127
                $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...
128
                break;
129
130
            case 'html':
131
                $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...
132
                break;
133
134
            case 'xml':
135
                $whoops->pushHandler(new XmlResponseHandler());
0 ignored issues
show
Documentation introduced by
new \Whoops\Handler\XmlResponseHandler() is of type object<Whoops\Handler\XmlResponseHandler>, 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...
136
                break;
137
138
            case 'txt':
139
            case 'css':
140
            case 'js':
141
                $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...
142
                break;
143
144
            default:
145
                if (empty($format)) {
146
                    $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...
147
                } else {
148
                    $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...
149
                }
150
151
                break;
152
        }
153
154
        return $whoops;
155
    }
156
}
157