Console::label()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace GeminiLabs\BlackBar\Modules;
4
5
use GeminiLabs\BlackBar\Dump;
6
7
class Console extends Module
8
{
9
    public const ERROR_CODES = [
10
        E_ERROR => 'error', // 1
11
        E_WARNING => 'warning', // 2
12
        E_NOTICE => 'notice', // 8
13
        E_STRICT => 'strict', // 2048
14
        E_DEPRECATED => 'deprecated', // 8192
15
    ];
16
17
    public const MAPPED_ERROR_CODES = [
18
        'debug' => 0,
19
        'info' => E_NOTICE,
20
        'deprecated' => E_DEPRECATED, // 8192
21
        'error' => E_ERROR, // 1
22
        'notice' => E_NOTICE, // 8
23
        'strict' => E_STRICT, // 2048
24
        'warning' => E_WARNING, // 2
25
        'critical' => E_ERROR, // 1
26
        'alert' => E_ERROR, // 1
27
        'emergency' => E_ERROR, // 1
28
    ];
29
30
    public function classes(): string
31
    {
32
        $errno = array_unique(wp_list_pluck($this->entries, 'errno'));
33
        if (in_array(E_ERROR, $errno)) {
34
            return sprintf('%s glbb-error', $this->id());
35
        }
36
        if (in_array(E_WARNING, $errno)) {
37
            return sprintf('%s glbb-warning', $this->id());
38
        }
39
        return $this->id();
40
    }
41
42
    public function entries(): array
43
    {
44
        $entries = [];
45
        foreach ($this->entries as $entry) {
46
            $entry['name'] = ucfirst($entry['errname']);
47
            if ($entry['count'] > 1) {
48
                $entry['name'] = sprintf('%s (%s)', $entry['name'], $entry['count']);
49
            }
50
            $entries[] = $entry;
51
        }
52
        return $entries;
53
    }
54
55
    public function info(): string
56
    {
57
        $counts = array_count_values(wp_list_pluck($this->entries, 'errno'));
58
        $entryCount = count($this->entries);
59
        if (!empty($counts[E_ERROR])) {
60
            return sprintf('%d, %d!', $entryCount, $counts[E_ERROR]);
61
        }
62
        if ($entryCount > 0) {
63
            return (string) $entryCount;
64
        }
65
        return '';
66
    }
67
68
    public function label(): string
69
    {
70
        return __('Console', 'blackbar');
71
    }
72
73
    public function store($message, string $errno = '', string $location = ''): void
74
    {
75
        if (is_numeric($errno)) { // entry likely stored by set_error_handler()
76
            $errname = 'Unknown';
77
            if (array_key_exists((int) $errno, static::ERROR_CODES)) {
78
                $errname = static::ERROR_CODES[$errno];
79
            }
80
        } else { // entry likely stored by filter hook
81
            $errname = 'Debug';
82
            if (array_key_exists($errno, static::MAPPED_ERROR_CODES)) {
83
                $errname = $errno;
84
                $errno = static::MAPPED_ERROR_CODES[$errno];
85
            }
86
        }
87
        $errname = strtolower($errname);
88
        $location = sanitize_text_field($location);
89
        $message = $this->normalizeMessage($message, $location);
90
        $hash = md5($errno.$errname.$message.$location);
91
        if (array_key_exists($hash, $this->entries)) {
92
            ++$this->entries[$hash]['count'];
93
        } else {
94
            $this->entries[$hash] = [
95
                'count' => 0,
96
                'errname' => $errname,
97
                'errno' => (int) $errno,
98
                'message' => $message,
99
            ];
100
        }
101
    }
102
103
    protected function normalizeMessage($message, string $location): string
104
    {
105
        if ($message instanceof \DateTime) {
106
            $message = $message->format('Y-m-d H:i:s');
107
        } elseif (is_object($message) || is_array($message)) {
108
            $message = (new Dump())->dump($message);
109
        } else {
110
            $message = esc_html(trim((string) $message));
111
        }
112
        $location = trim($location);
113
        if (!empty($location)) {
114
            $location = str_replace([WP_CONTENT_DIR, ABSPATH], '', $location);
115
            $location = sprintf('[%s]', $location);
116
        }
117
        return trim(sprintf('%s %s', $location, $message));
118
    }
119
}
120