Issues (3)

src/VPA/Logger/ConsoleLogger.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VPA\Logger;
6
7
use VPA\DI\Injectable;
8
use \Stringable;
0 ignored issues
show
The type \Stringable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
#[Injectable]
11
class ConsoleLogger extends BaseLogger
12
{
13
    private const PREPEND_COL = '| ';
14
    private const SEPARATOR_COL = ' | ';
15
    private const APPEND_COL = ' |';
16
17
    /**
18
     * Logs with an arbitrary level.
19
     *
20
     * @param mixed $level
21
     * @param string | Stringable $message
22
     * @param array $context
23
     * @return void
24
     */
25 9
    public function log(mixed $level, string|Stringable $message, array $context = []): void
26
    {
27 9
        $text = $this->interpolate($message, $context);
28
29 9
        $color = match ($level) {
30 1
            'emergency' => '0;35',
31 1
            'info' => '0;32',
32 2
            'debug' => '1;34',
33 1
            'notice' => '1;33',
34 2
            'error', 'critical' => '0;31',
35 1
            'alert' => '1;31',
36 1
            default => '1;37',
37
        };
38 9
        printf("\033[%sm%s [%s] %s\033[0m\n", $color, date('y-m-d H:i:s'), $level, $text);
39
    }
40
41 9
    protected function castValue(mixed $value): string
42
    {
43 9
        $type = gettype($value);
44
        return match ($type) {
45 9
            'array' => $this->formatArray($value),
46 9
            default => $value,
47
        };
48
    }
49
50
51 1
    private function format1DArray(array $value): string
52
    {
53 1
        $lengthFirstColumn = array_reduce(array_keys($value), function (mixed $carry,  mixed $string) {
54 1
            return max($carry, strlen((string) $string));
55
        }
56
        );
57 1
        $lengthSecondColumn = array_reduce($value, function (mixed $carry, string $string) {
58 1
            return max($carry, strlen($string));
59
        }
60
        );
61
62 1
        $lengthTotal = strlen(self::APPEND_COL) +
63
            $lengthFirstColumn +
64 1
            strlen(self::SEPARATOR_COL) +
65
            $lengthSecondColumn +
66 1
            strlen(self::APPEND_COL);
67 1
        $output = $footer = str_repeat('-', $lengthTotal);
68 1
        foreach ($value as $key => $item) {
69 1
            $output .= sprintf(
70
                "\n%s%s%s%s%s",
71
                self::PREPEND_COL,
72 1
                str_pad($key, $lengthFirstColumn),
73
                self::SEPARATOR_COL,
74 1
                str_pad($item, $lengthSecondColumn),
75
                self::APPEND_COL
76
            );
77
        }
78 1
        return "\n" . $output . "\n" . $footer . "\n";
79
    }
80
81 1
    private function getDimensional(array $array): int
82
    {
83 1
        if (is_array(reset($array))) {
84
            $return = $this->getDimensional(reset($array)) + 1;
85
        } else {
86 1
            $return = 1;
87
        }
88
89 1
        return $return;
90
    }
91
92 1
    private function formatArray(array $value): string
93
    {
94 1
        $dim = $this->getDimensional($value);
95
        return match ($dim) {
96 1
            1 => $this->format1DArray($value),
97 1
            default => "Array with dimension $dim not supported",
98
        };
99
    }
100
}
101