Passed
Pull Request — master (#11)
by Joao
01:52
created

WhoopsWrapper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 28%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 86
ccs 7
cts 25
cp 0.28
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setException() 0 3 1
A setOutputProcessor() 0 4 1
A setInspector() 0 3 1
A setRun() 0 3 1
A handle() 0 17 3
A setHandler() 0 3 1
A __construct() 0 3 1
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\RunInterface;
12
13
class WhoopsWrapper extends Handler
14
{
15
    /** @var Handler */
16
    protected $effectiveHandler = null;
17
18
    /** @var BaseOutputProcessor */
19
    protected $outputProcessor;
20
21
    /** @var HttpResponse */
22
    protected $response;
23
24 1
    public function __construct()
25
    {
26 1
        $this->effectiveHandler = new PlainResponseErrorHandler();
27
    }
28
29
    /**
30
     * Set the effective handler
31
     *
32
     * @param Handler $handler
33
     * @return void
34
     */
35 12
    public function setHandler(Handler $handler)
36
    {
37 12
        $this->effectiveHandler = $handler;
38
    }
39
40 12
    public function setOutputProcessor(BaseOutputProcessor $processor, HttpResponse $response)
41
    {
42 12
        $this->outputProcessor = $processor;
43 12
        $this->response = $response;
44
    }
45
46
    /* *******************************************************
47
     *
48
     * HandlerInterface
49
     *
50
     ********************************************************* */
51
52
     /**
53
     * @return int|null A handler may return nothing, or a Handler::HANDLE_* constant
54
     */
55
    public function handle()
56
    {
57
        $r = new ReflectionMethod($this->effectiveHandler, 'getException');
58
        $r->setAccessible(true); // That's necessary because error handler doesn't expose `getException`
59
        $exception = $r->invoke($this->effectiveHandler);
60
        if ($exception instanceof ClientShowException) {
61
            $exception->setResponse($this->response);
62
            $exception->handleHeader();
63
        } else {
64
            $this->response->setResponseCode(500, 'Internal Error');
65
        }
66
67
        if (!empty($this->outputProcessor)) {
68
            $this->response->emptyResponse();
69
            $this->outputProcessor->writeHeader($this->response);
70
        }
71
        return $this->effectiveHandler->handle();
72
    }
73
74
    /**
75
     * @param  RunInterface  $run
76
     * @return void
77
     */
78
    public function setRun(RunInterface $run)
79
    {
80
        return $this->effectiveHandler->setRun($run);
0 ignored issues
show
Bug introduced by
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...
81
    }
82
83
    /**
84
     * @param  \Throwable $exception
85
     * @return void
86
     */
87
    public function setException($exception)
88
    {
89
        return $this->effectiveHandler->setException($exception);
0 ignored issues
show
Bug introduced by
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...
90
    }
91
92
    /**
93
     * @param  Inspector $inspector
94
     * @return void
95
     */
96
    public function setInspector(Inspector $inspector)
97
    {
98
        return $this->effectiveHandler->setInspector($inspector);
0 ignored issues
show
Bug introduced by
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...
99
    }
100
}
101