Issues (15)

src/Whoops/WhoopsWrapper.php (3 issues)

1
<?php
2
3
namespace ByJG\RestServer\Whoops;
4
5
use ByJG\RestServer\Exception\ClientShowException;
6
use ByJG\RestServer\HttpResponse;
7
use ByJG\RestServer\OutputProcessor\BaseOutputProcessor;
8
use ReflectionMethod;
9
use Whoops\Exception\Inspector;
10
use Whoops\Handler\Handler;
11
use Whoops\Inspector\InspectorInterface;
12
use Whoops\RunInterface;
13
14
class WhoopsWrapper extends Handler
15
{
16
    /** @var Handler */
17
    protected $effectiveHandler = null;
18
19
    /** @var BaseOutputProcessor */
20
    protected $outputProcessor;
21
22
    /** @var HttpResponse */
23
    protected $response;
24
25 1
    public function __construct()
26
    {
27 1
        $this->effectiveHandler = new PlainResponseErrorHandler();
28
    }
29
30
    /**
31
     * Set the effective handler
32
     *
33
     * @param Handler $handler
34
     * @return void
35
     */
36 12
    public function setHandler(Handler $handler)
37
    {
38 12
        $this->effectiveHandler = $handler;
39
    }
40
41 12
    public function setOutputProcessor(BaseOutputProcessor $processor, HttpResponse $response)
42
    {
43 12
        $this->outputProcessor = $processor;
44 12
        $this->response = $response;
45
    }
46
47
    /* *******************************************************
48
     *
49
     * HandlerInterface
50
     *
51
     ********************************************************* */
52
53
     /**
54
     * @return int|null A handler may return nothing, or a Handler::HANDLE_* constant
55
     */
56
    public function handle()
57
    {
58
        $r = new ReflectionMethod($this->effectiveHandler, 'getException');
59
        $r->setAccessible(true); // That's necessary because error handler doesn't expose `getException`
60
        $exception = $r->invoke($this->effectiveHandler);
61
        if ($exception instanceof ClientShowException) {
62
            $exception->setResponse($this->response);
63
            $exception->handleHeader();
64
        } else {
65
            $this->response->setResponseCode(500, 'Internal Error');
66
        }
67
68
        if (!empty($this->outputProcessor)) {
69
            $this->response->emptyResponse();
70
            $this->outputProcessor->writeHeader($this->response);
71
        }
72
        return $this->effectiveHandler->handle();
73
    }
74
75
    /**
76
     * @param  RunInterface  $run
77
     * @return void
78
     */
79
    public function setRun(RunInterface $run)
80
    {
81
        return $this->effectiveHandler->setRun($run);
0 ignored issues
show
Are you sure the usage of $this->effectiveHandler->setRun($run) targeting Whoops\Handler\Handler::setRun() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
82
    }
83
84
    /**
85
     * @param  \Throwable $exception
86
     * @return void
87
     */
88
    public function setException($exception)
89
    {
90
        return $this->effectiveHandler->setException($exception);
0 ignored issues
show
Are you sure the usage of $this->effectiveHandler->setException($exception) targeting Whoops\Handler\Handler::setException() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
91
    }
92
93
    /**
94
     * @param  Inspector $inspector
95
     * @return void
96
     */
97
    public function setInspector(InspectorInterface $inspector)
98
    {
99
        return $this->effectiveHandler->setInspector($inspector);
0 ignored issues
show
Are you sure the usage of $this->effectiveHandler->setInspector($inspector) targeting Whoops\Handler\Handler::setInspector() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
100
    }
101
}
102