Conditions | 14 |
Paths | 3 |
Total Lines | 127 |
Code Lines | 103 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 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 |
||
147 | public static function dumpException(\Throwable $exception): void |
||
148 | { |
||
149 | if (self::isCli()) { |
||
150 | echo $exception; |
||
151 | exit; |
||
152 | } |
||
153 | |||
154 | self::setSyntaxHighlighting(); |
||
155 | |||
156 | $file = $exception->getFile(); |
||
157 | $line = $exception->getLine(); |
||
158 | $message = $exception->getMessage(); |
||
159 | $trace = $exception->getTrace(); |
||
160 | $traceString = $exception->getTraceAsString(); |
||
161 | $name = get_class($exception); |
||
162 | $filename = basename($file); |
||
163 | $lines = null; |
||
164 | |||
165 | if (file_exists($file)) { |
||
166 | $lines = file($file); |
||
167 | } |
||
168 | |||
169 | $accentColor = self::$accentColor; |
||
170 | $contrastColor = self::$contrastColor; |
||
171 | |||
172 | $style = ":root{--accent-color:{$accentColor};--contrast-color:{$contrastColor}}*,::after,::before{box-sizing:border-box}body{background:#fff;font-family:-apple-system,'Fira Sans',Ubuntu,Helvetica,Arial,sans-serif;font-size:16px;line-height:1.5;margin:0}h1,h2,h3,h4,h5,h6{margin:0}h1,h2{color:var(--accent-color)}h1{font-size:32px}h2{font-size:28px}h3{color:#fff}.container{width:85vw;max-width:1200px;min-height:100vh;background:#fff;padding:7vh 3vw 10vh 3vw;margin:0 auto;overflow:hidden}.message{background:var(--accent-color);color:#fff;padding:2em 1em;margin:0 0 3em 0;}.code{overflow-y:scroll;font-family:'Fira Code','Ubuntu Mono',Courier,monospace;font-size:14px;margin:0 0 3em 0;-ms-overflow-style: none;scrollbar-width: none}.code::-webkit-scrollbar{display:none}pre{white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word}ul{padding:2em 1em;margin:1em 0;background:var(--contrast-color)}ul li{white-space:pre;list-style-type:none;font-family:monospace}ul li span.line{display:inline-block;color:#fff;text-align:right;padding:4px 8px;user-select:none}ul li.exception-line span.line{color:var(--accent-color);font-weight:bold}ul li.exception-line span.line+code>span>span:not(:first-child){padding-bottom:3px;border-bottom:2px solid var(--accent-color)}table{width:100%;border-collapse:collapse;border-spacing:0}table th{background:var(--contrast-color);color:#fff;text-align:left;padding-top:12px;padding-bottom:12px}table td,table th{border-bottom:1px solid rgba(0,0,0,0.15);padding:6px}table tr:nth-child(even){background-color:rgba(0,0,0,0.05)}table td.number{text-align:left}table td.line{text-align:left}table td.class,table td.function{font-family:'Fira Code','Ubuntu Mono',Courier,monospace;font-size:14px;font-weight:700}table td.arguments span{display:inline-block;background:rgba(0,0,0,.15);color:var(--accent-color);font-style:italic;padding:2px 4px;margin:0 4px 0 0;border-radius:4px}"; |
||
173 | |||
174 | (new HTML(false)) |
||
175 | ->node('<!DOCTYPE html>') |
||
176 | ->open('html', ['lang' => 'en']) |
||
177 | ->open('head') |
||
178 | ->title('Oops, something went wrong') |
||
179 | ->style($style) |
||
180 | ->close() |
||
181 | ->open('body') |
||
182 | ->open('div', ['class' => 'container']) |
||
183 | ->h1("Uncaught {$name}") |
||
184 | ->p("An <b>{$name}</b> was thrown on line {$line} of file {$filename} which prevented further execution of the code.") |
||
185 | ->open('div', ['class' => 'message']) |
||
186 | ->h3($name) |
||
187 | ->p($message) |
||
188 | ->close() |
||
189 | ->h2('Thrown in:') |
||
190 | ->execute(function (HTML $html) use ($file, $line, $lines) { |
||
191 | if (isset($lines)) { |
||
192 | $html->p($file); |
||
193 | $html->open('ul', ['class' => 'code']); |
||
194 | for ($i = $line - 3; $i < $line + 4; $i++) { |
||
195 | if ($i > 0 && $i < count($lines)) { |
||
196 | $highlightedCode = highlight_string('<?php ' . $lines[$i], true); |
||
197 | $highlightedCode = preg_replace( |
||
198 | ['/\n/', '/<br ?\/?>/', '/<\?php /'], |
||
199 | ['', '', ''], |
||
200 | $highlightedCode |
||
201 | ); |
||
202 | if ($i == $line - 1) { |
||
203 | $arrow = str_pad('>', strlen("{$i}"), '=', STR_PAD_LEFT); |
||
204 | $html |
||
205 | ->open('li', ['class' => 'exception-line']) |
||
206 | ->span($arrow, ['class' => 'line']) |
||
207 | ->node($highlightedCode) |
||
208 | ->close(); |
||
209 | } else { |
||
210 | $number = strval($i + 1); |
||
211 | $html |
||
212 | ->open('li') |
||
213 | ->span($number, ['class' => 'line']) |
||
214 | ->node($highlightedCode) |
||
215 | ->close(); |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | $html->close(); |
||
220 | } |
||
221 | }) |
||
222 | ->h2('Stack trace:') |
||
223 | ->execute(function (HTML $html) use ($trace, $traceString) { |
||
224 | if (count($trace)) { |
||
225 | $html->p('<i>Hover on fields with * to reveal more info.</i>'); |
||
226 | $html->open('table', ['class' => 'trace']) |
||
227 | ->open('thead') |
||
228 | ->open('tr') |
||
229 | ->th('No.') |
||
230 | ->th('File *') |
||
231 | ->th('Line') |
||
232 | ->th('Class') |
||
233 | ->th('Function') |
||
234 | ->th('Arguments *') |
||
235 | ->close() |
||
236 | ->close() |
||
237 | ->open('tbody') |
||
238 | ->execute(function (HTML $html) use ($trace) { |
||
239 | foreach ($trace as $i => $trace) { |
||
240 | $count = (int)$i + 1; |
||
241 | $html |
||
242 | ->open('tr', ['class' => $count % 2 == 0 ? 'even' : 'odd']) |
||
243 | ->td(strval($count), ['class' => 'number']) |
||
244 | ->td(isset($trace['file']) ? basename($trace['file']) : '', ['class' => 'file', 'title' => $trace['file'] ?? false]) |
||
245 | ->td(strval($trace['line'] ?? ''), ['class' => 'line']) |
||
246 | ->td(strval($trace['class'] ?? ''), ['class' => 'class']) |
||
247 | ->td(strval($trace['function'] ?? ''), ['class' => 'function']) |
||
248 | ->open('td', ['class' => 'arguments']) |
||
249 | ->execute(function (HTML $html) use ($trace) { |
||
250 | if (isset($trace['args'])) { |
||
251 | foreach ($trace['args'] as $i => $arg) { |
||
252 | $html->span(gettype($arg), ['title' => print_r($arg, true)]); |
||
253 | } |
||
254 | } else { |
||
255 | $html->node('NULL'); |
||
256 | } |
||
257 | }) |
||
258 | ->close() |
||
259 | ->close(); |
||
260 | } |
||
261 | }) |
||
262 | ->close() |
||
263 | ->close(); |
||
264 | } else { |
||
265 | $html->pre($traceString); |
||
266 | } |
||
267 | }) |
||
268 | ->close() |
||
269 | ->close() |
||
270 | ->close() |
||
271 | ->echo(); |
||
272 | |||
273 | exit; |
||
274 | } |
||
332 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.