Passed
Push — master ( bcb1ec...700d1f )
by Paul
02:53
created

Console   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
eloc 69
dl 0
loc 105
rs 10
c 1
b 0
f 0

5 Methods

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