Passed
Push — master ( 3da5b1...202928 )
by Peter
04:02
created

ExceptionRenderer::subRender()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 71
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 54
c 1
b 0
f 0
nc 14
nop 1
dl 0
loc 71
rs 8.3814

How to fix   Long Method   

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 AbterPhp\Framework\Debug\Exceptions\Handlers\Whoops;
4
5
use Exception;
6
use Opulence\Framework\Debug\Exceptions\Handlers\Http;
7
use Opulence\Http\HttpException;
8
use Throwable;
9
use Whoops\RunInterface;
10
11
/**
12
 * @SuppressWarnings(PHPMD)
13
 */
14
class ExceptionRenderer extends Http\ExceptionRenderer implements Http\IExceptionRenderer
15
{
16
    /** @var RunInterface */
17
    protected $run;
18
19
    /**
20
     * WhoopsRenderer constructor.
21
     *
22
     * @param RunInterface $run
23
     */
24
    public function __construct(RunInterface $run, bool $inDevelopmentEnvironment = false)
25
    {
26
        $this->run = $run;
27
28
        parent::__construct($inDevelopmentEnvironment);
29
    }
30
31
    /**
32
     * @return RunInterface
33
     */
34
    public function getRun()
35
    {
36
        return $this->run;
37
    }
38
39
    /**
40
     * Renders an exception
41
     *
42
     * @param Throwable|Exception $ex The thrown exception
43
     */
44
    public function render($ex)
45
    {
46
        if (!$this->inDevelopmentEnvironment) {
47
            $this->run->writeToOutput(false);
48
49
            $this->run->unregister();
50
51
            return $this->subRender($ex);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->subRender($ex) targeting AbterPhp\Framework\Debug...onRenderer::subRender() 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...
52
        }
53
54
        $this->run->handleException($ex);
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function subRender($ex)
61
    {
62
        // Add support for HTTP library without having to necessarily depend on it
63
        if (get_class($ex) === HttpException::class) {
64
            /** @var HttpException $ex */
65
            $statusCode = $ex->getStatusCode();
66
            $headers    = $ex->getHeaders();
67
        } else {
68
            $statusCode = 500;
69
            $headers    = [];
70
        }
71
72
        // Always get the content, even if headers are sent, so that we can unit test this
73
        $content = $ex->getMessage();
74
75
        if (!headers_sent()) {
76
            header("HTTP/1.1 $statusCode", true, $statusCode);
77
78
            switch ($this->getRequestFormat()) {
79
                case 'json':
80
                    $headers['Content-Type'] = 'application/json';
81
                    break;
82
                default:
83
                    $content                 = sprintf(
84
                        '<!DOCTYPE html>
85
<html>
86
    <head>
87
        <meta name="viewport" content="initial-scale=1"/>
88
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
89
            integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
90
            crossorigin="anonymous">
91
    </head>
92
    <body>
93
        <main class="main">
94
            <div class="container">
95
                <div class="row">
96
                    <div class="col-sm">
97
                        <div class="alert alert-danger" role="alert">
98
                            <h4 class="alert-heading">Exception: %s</h4>
99
                            <p>%s</p>
100
                            <hr>
101
                            <p class="mb-0">
102
                                <a href="#" class="btn btn-danger btn-lg active" role="button" aria-pressed="true"
103
                                onclick="location.reload(); return false;">Try again</a>
104
                                <a href="#" class="btn btn-primary btn-lg active" role="button" aria-pressed="true"
105
                                onclick="window.history.back(); return false;">Back to previous page</a>
106
                            </p>
107
                        </div>
108
                    </div>
109
                </div>
110
            </div>
111
        </main>
112
    </body>
113
</html>',
114
                        get_class($ex),
115
                        $ex->getMessage()
116
                    );
117
                    $headers['Content-Type'] = 'text/html';
118
            }
119
120
            foreach ($headers as $name => $values) {
121
                $values = (array)$values;
122
123
                foreach ($values as $value) {
124
                    header("$name:$value", false, $statusCode);
125
                }
126
            }
127
128
            echo $content;
129
            // To prevent any potential output buffering, let's flush
130
            flush();
131
        }
132
    }
133
}
134