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 |
||
137 | public static function dumpException(\Throwable $exception): void |
||
138 | { |
||
139 | if (self::isCli()) { |
||
140 | echo $exception; |
||
141 | exit; |
||
142 | } |
||
143 | |||
144 | self::setSyntaxHighlighting(); |
||
145 | |||
146 | $file = $exception->getFile(); |
||
147 | $line = $exception->getLine(); |
||
148 | $message = $exception->getMessage(); |
||
149 | $trace = $exception->getTrace(); |
||
150 | $traceString = $exception->getTraceAsString(); |
||
151 | $name = get_class($exception); |
||
152 | $filename = basename($file); |
||
153 | $lines = null; |
||
154 | |||
155 | if (file_exists($file)) { |
||
156 | $lines = file($file); |
||
157 | } |
||
158 | |||
159 | $accentColor = self::$accentColor; |
||
160 | $contrastColor = self::$contrastColor; |
||
161 | |||
162 | $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}"; |
||
163 | |||
164 | (new HTML(false)) |
||
165 | ->node('<!DOCTYPE html>') |
||
166 | ->open('html', ['lang' => 'en']) |
||
167 | ->open('head') |
||
168 | ->title('Oops, something went wrong') |
||
169 | ->style($style) |
||
170 | ->close() |
||
171 | ->open('body') |
||
172 | ->open('div', ['class' => 'container']) |
||
173 | ->h1("Uncaught {$name}") |
||
174 | ->p("An <b>{$name}</b> was thrown on line {$line} of file {$filename} which prevented further execution of the code.") |
||
175 | ->open('div', ['class' => 'message']) |
||
176 | ->h3($name) |
||
177 | ->p($message) |
||
178 | ->close() |
||
179 | ->h2('Thrown in:') |
||
180 | ->execute(function (HTML $html) use ($file, $line, $lines) { |
||
181 | if (isset($lines)) { |
||
182 | $html->p($file); |
||
183 | $html->open('ul', ['class' => 'code']); |
||
184 | for ($i = $line - 3; $i < $line + 4; $i++) { |
||
185 | if ($i > 0 && $i < count($lines)) { |
||
186 | $highlightedCode = highlight_string('<?php ' . $lines[$i], true); |
||
187 | $highlightedCode = preg_replace( |
||
188 | ['/\n/', '/<br ?\/?>/', '/<\?php /'], |
||
189 | ['', '', ''], |
||
190 | $highlightedCode |
||
191 | ); |
||
192 | if ($i == $line - 1) { |
||
193 | $arrow = str_pad('>', strlen("{$i}"), '=', STR_PAD_LEFT); |
||
194 | $html |
||
195 | ->open('li', ['class' => 'exception-line']) |
||
196 | ->span($arrow, ['class' => 'line']) |
||
197 | ->node($highlightedCode) |
||
198 | ->close(); |
||
199 | } else { |
||
200 | $number = strval($i + 1); |
||
201 | $html |
||
202 | ->open('li') |
||
203 | ->span($number, ['class' => 'line']) |
||
204 | ->node($highlightedCode) |
||
205 | ->close(); |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 | $html->close(); |
||
210 | } |
||
211 | }) |
||
212 | ->h2('Stack trace:') |
||
213 | ->execute(function (HTML $html) use ($trace, $traceString) { |
||
214 | if (count($trace)) { |
||
215 | $html->p('<i>Hover on fields with * to reveal more info.</i>'); |
||
216 | $html->open('table', ['class' => 'trace']) |
||
217 | ->open('thead') |
||
218 | ->open('tr') |
||
219 | ->th('No.') |
||
220 | ->th('File *') |
||
221 | ->th('Line') |
||
222 | ->th('Class') |
||
223 | ->th('Function') |
||
224 | ->th('Arguments *') |
||
225 | ->close() |
||
226 | ->close() |
||
227 | ->open('tbody') |
||
228 | ->execute(function (HTML $html) use ($trace) { |
||
229 | foreach ($trace as $i => $trace) { |
||
230 | $count = (int)$i + 1; |
||
231 | $html |
||
232 | ->open('tr', ['class' => $count % 2 == 0 ? 'even' : 'odd']) |
||
233 | ->td(strval($count), ['class' => 'number']) |
||
234 | ->td(isset($trace['file']) ? basename($trace['file']) : '', ['class' => 'file', 'title' => $trace['file'] ?? false]) |
||
235 | ->td(strval($trace['line'] ?? ''), ['class' => 'line']) |
||
236 | ->td(strval($trace['class'] ?? ''), ['class' => 'class']) |
||
237 | ->td(strval($trace['function'] ?? ''), ['class' => 'function']) |
||
238 | ->open('td', ['class' => 'arguments']) |
||
239 | ->execute(function (HTML $html) use ($trace) { |
||
240 | if (isset($trace['args'])) { |
||
241 | foreach ($trace['args'] as $i => $arg) { |
||
242 | $html->span(gettype($arg), ['title' => print_r($arg, true)]); |
||
243 | } |
||
244 | } else { |
||
245 | $html->node('NULL'); |
||
246 | } |
||
247 | }) |
||
248 | ->close() |
||
249 | ->close(); |
||
250 | } |
||
251 | }) |
||
252 | ->close() |
||
253 | ->close(); |
||
254 | } else { |
||
255 | $html->pre($traceString); |
||
256 | } |
||
257 | }) |
||
258 | ->close() |
||
259 | ->close() |
||
260 | ->close() |
||
261 | ->echo(); |
||
262 | |||
263 | exit; |
||
264 | } |
||
322 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.