Conditions | 10 |
Paths | 12 |
Total Lines | 50 |
Code Lines | 35 |
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 |
||
22 | function dump($var, $label = null, bool $traceback = false, bool $die = true) |
||
23 | { |
||
24 | $MESSAGE_CLI = "\n>>> %s[type] %s\n[info] %s\n[line] \x1b[1m%s\x1b[0m\n\x1b[2m%s\x1b[0m"; |
||
25 | $MESSAGE_HTM = '<span style="clear:both; color:black; font-size:11px;">[<b>%s</b>] %s (%s) %s</span>'; |
||
26 | |||
27 | list($position, $backtrace) = (function(array $backtrace) { |
||
28 | if (isset($backtrace[1]) && '__invoke' === $backtrace[1]['function']) { |
||
29 | $p = $backtrace[1]['class'] . $backtrace[1]['type'] . $backtrace[1]['function'] . ':' . $backtrace[0]['line']; |
||
30 | } else if ('dump' === $backtrace[0]['function'] && \count($backtrace) > 1) { |
||
31 | $p = $backtrace[1]['class'] ?? $backtrace[0]['file'] ?? __FILE__; |
||
32 | $p .= $backtrace[1]['type'] ?? ' *'; |
||
33 | $p .= $backtrace[1]['function'] . ':' . $backtrace[0]['line'] ?? $backtrace[1]['line']; |
||
34 | } else { |
||
35 | $p = $backtrace[0]['class'] ?? $backtrace[0]['file']; |
||
36 | $p .= $backtrace[0]['type'] ?? ':' . $backtrace[0]['line']; |
||
37 | $p .= ' ' . $backtrace[0]['function'] . '()'; |
||
38 | } |
||
39 | |||
40 | // backtrace |
||
41 | $b = \array_map(function($l) { |
||
42 | $MESSAGE_BACKTRACE = " File \"\x1b[36;2m%s\x1b[0m\", line \x1b[36;2m%d\x1b[0m\n from %s\x1b[1m%s()\x1b[0m"; |
||
43 | return sprintf($MESSAGE_BACKTRACE, $l['file'] ?? '?', $l['line'] ?? -1, (($l['class'] ?? '') ? $l['class'] . '::' : ''), $l['function']); |
||
44 | }, \array_slice(\array_reverse($backtrace), 0, -1)); |
||
45 | |||
46 | return [$p, \join(PHP_EOL, $b)]; |
||
47 | |||
48 | })(\debug_backtrace()); |
||
49 | |||
50 | $backtrace = ($traceback ? 'Traceback (most recent call last):' . PHP_EOL . $backtrace . PHP_EOL . \str_repeat('-', 80) : \date(DATE_COOKIE)) . PHP_EOL; |
||
51 | |||
52 | if ('cli' === \php_sapi_name()) { |
||
53 | $output = \sprintf($MESSAGE_CLI, $backtrace, \gettype($var), \var_export($label, true), $position, \print_r($var, true)); |
||
|
|||
54 | } elseif (\strtoupper($_SERVER['HTTP_X_REQUESTED_WITH'] ?? '') == 'XMLHTTPREQUEST') { |
||
55 | $output = \json_encode($var, JSON_UNESCAPED_SLASHES); |
||
56 | } else { |
||
57 | $format = function($var) { |
||
58 | \ob_start(); |
||
59 | \var_dump($var); |
||
60 | $dump = \ob_get_contents(); |
||
61 | $dump = \preg_replace('/<small>\/.*<\/small>/', '\\1', $dump); |
||
62 | \ob_end_clean(); |
||
63 | |||
64 | return $dump; |
||
65 | }; |
||
66 | |||
67 | $output = \sprintf($MESSAGE_HTM, $position, \gettype($var), \var_export($label, true), $format($var)); |
||
68 | } |
||
69 | |||
70 | $die and die($output); |
||
71 | print $output; |
||
72 | } |
||
73 |