Conditions | 10 |
Paths | 64 |
Total Lines | 40 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
29 | function serverEcho(int $statusCode, string $msg, float $time, bool $fromHandler = false): void |
||
30 | { |
||
31 | $isXhr = (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) |
||
32 | && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') ? '[XHR]' : ''; |
||
33 | $method = isset($_SERVER['REQUEST_METHOD']) ? |
||
34 | strtoupper($_SERVER['REQUEST_METHOD']) : ''; |
||
35 | $statusColor = match (true) { |
||
36 | $statusCode >= 200 && $statusCode < 300 => '32', |
||
37 | $statusCode >= 300 && $statusCode < 400 => '34', |
||
38 | $statusCode >= 400 && $statusCode < 500 => '33', |
||
39 | $statusCode >= 500 => '31', |
||
40 | default => '37', |
||
41 | }; |
||
42 | $duration = sprintf('%.5f', round($time, 5)); |
||
43 | $columns = (int)getenv('CONIA_TERMINAL_COLUMNS'); |
||
44 | |||
45 | list($usec, $sec) = explode(' ', microtime()); |
||
46 | $usec = str_replace('0.', '.', $usec); |
||
47 | $timestamp = date('H:i:s', (int)$sec) . substr($usec, 0, 3); |
||
48 | $url = urldecode($msg); |
||
49 | |||
50 | $leftSide = |
||
51 | // timestamp |
||
52 | "\033[0;37m{$timestamp}\033[0m " . |
||
53 | // status code |
||
54 | "\033[0;{$statusColor}m{$statusCode}\033[0m " . |
||
55 | // request method |
||
56 | "{$method} " . |
||
57 | // request uri |
||
58 | "\033[0;{$statusColor}m{$url}\033[0m"; |
||
59 | $rightSide = |
||
60 | // from error handler |
||
61 | ($fromHandler ? "\033[0;36m[EXC]\033[0m" : '') . |
||
62 | // xhr indicator |
||
63 | "\033[0;36m{$isXhr}\033[0m" . |
||
64 | ($fromHandler || $isXhr ? ' ' : '') . |
||
65 | // time |
||
66 | "\033[0;37m{$duration}s\033[0m"; |
||
67 | |||
68 | error_log($leftSide . getServerEchoSpacer($leftSide, $rightSide, $columns) . $rightSide); |
||
69 | } |
||
71 |