| Conditions | 6 |
| Paths | 14 |
| Total Lines | 70 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 79 | public function devRender($ex) |
||
| 80 | { |
||
| 81 | // Add support for HTTP library without having to necessarily depend on it |
||
| 82 | if ($ex instanceof HttpException) { |
||
| 83 | $statusCode = $ex->getStatusCode(); |
||
| 84 | $headers = $ex->getHeaders(); |
||
| 85 | } else { |
||
| 86 | $statusCode = 500; |
||
| 87 | $headers = []; |
||
| 88 | } |
||
| 89 | |||
| 90 | // Always get the content, even if headers are sent, so that we can unit test this |
||
| 91 | $content = $ex->getMessage(); |
||
| 92 | |||
| 93 | if (!headers_sent()) { |
||
| 94 | header("HTTP/1.1 $statusCode", true, $statusCode); |
||
| 95 | |||
| 96 | switch ($this->getRequestFormat()) { |
||
| 97 | case 'json': |
||
| 98 | $headers['Content-Type'] = 'application/json'; |
||
| 99 | break; |
||
| 100 | default: |
||
| 101 | $content = sprintf( |
||
| 102 | '<!DOCTYPE html> |
||
| 103 | <html> |
||
| 104 | <head> |
||
| 105 | <meta name="viewport" content="initial-scale=1"/> |
||
| 106 | <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" |
||
| 107 | integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" |
||
| 108 | crossorigin="anonymous"> |
||
| 109 | </head> |
||
| 110 | <body> |
||
| 111 | <main class="main"> |
||
| 112 | <div class="container"> |
||
| 113 | <div class="row"> |
||
| 114 | <div class="col-sm"> |
||
| 115 | <div class="alert alert-danger" role="alert"> |
||
| 116 | <h4 class="alert-heading">Exception: %s</h4> |
||
| 117 | <p>%s</p> |
||
| 118 | <hr> |
||
| 119 | <p class="mb-0"> |
||
| 120 | <a href="#" class="btn btn-danger btn-lg active" role="button" aria-pressed="true" |
||
| 121 | onclick="location.reload(); return false;">Try again</a> |
||
| 122 | <a href="#" class="btn btn-primary btn-lg active" role="button" aria-pressed="true" |
||
| 123 | onclick="window.history.back(); return false;">Back to previous page</a> |
||
| 124 | </p> |
||
| 125 | </div> |
||
| 126 | </div> |
||
| 127 | </div> |
||
| 128 | </div> |
||
| 129 | </main> |
||
| 130 | </body> |
||
| 131 | </html>', |
||
| 132 | get_class($ex), |
||
| 133 | $ex->getMessage() |
||
| 134 | ); |
||
| 135 | $headers['Content-Type'] = 'text/html'; |
||
| 136 | } |
||
| 137 | |||
| 138 | foreach ($headers as $name => $values) { |
||
| 139 | $values = (array)$values; |
||
| 140 | |||
| 141 | foreach ($values as $value) { |
||
| 142 | header("$name:$value", false, $statusCode); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | echo $content; |
||
| 147 | // To prevent any potential output buffering, let's flush |
||
| 148 | flush(); |
||
| 149 | } |
||
| 152 |