serverEcho()   B
last analyzed

Complexity

Conditions 10
Paths 64

Size

Total Lines 40
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 40
rs 7.6666
cc 10
nc 64
nop 4

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
declare(strict_types=1);
4
5
if (!function_exists('getServerEchoSpacer')) {
6
    function getServerEchoSpacer(string $leftSide, string $rightSide, int $columns): string
7
    {
8
        $strlen = function (string $str): int {
9
            if (function_exists('mb_strlen')) {
10
                return mb_strlen($str);
11
            }
12
13
            return strlen($str);
14
        };
15
        $leftLen = $strlen(preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $leftSide));
16
        $rightLen = $strlen(preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $rightSide));
17
18
        if ($leftLen > $columns) {
19
            $leftLen = $leftLen % $columns;
20
        }
21
22
        $spacer = str_repeat('.', $columns - (($leftLen + $rightLen + 2) % $columns));
23
24
        return " \033[1;30m{$spacer}\033[0m ";
25
    }
26
}
27
28
if (!function_exists('serverEcho')) {
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
    }
70
}
71