Passed
Push — master ( e26094...28949d )
by Paul
02:50
created

Console::normalizeMessage()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
nc 6
nop 2
dl 0
loc 15
rs 9.6111
c 1
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\BlackBar\Modules;
4
5
use GeminiLabs\BlackBar\Application;
6
7
class Console implements 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
    /**
31
     * @var Application
32
     */
33
    protected $app;
34
    /**
35
     * @var array
36
     */
37
    protected $entries;
38
39
    public function __construct(Application $app)
40
    {
41
        $this->app = $app;
42
        $this->entries = [];
43
    }
44
45
    public function entries(): array
46
    {
47
        $entries = [];
48
        foreach ($this->entries as $entry) {
49
            $entry['name'] = ucfirst($entry['errname']);
50
            if ($entry['count'] > 1) {
51
                $entry['message'] = sprintf('(%s) %s', $entry['count'], $entry['message']);
52
            }
53
            $entries[] = $entry;
54
        }
55
        return $entries;
56
    }
57
58
    public function hasEntries(): bool
59
    {
60
        return !empty($this->entries);
61
    }
62
63
    public function id(): string
64
    {
65
        return 'glbb-console';
66
    }
67
68
    public function isVisible(): bool
69
    {
70
        return true;
71
    }
72
73
    public function label(): string
74
    {
75
        $class = '';
76
        $entryCount = count($this->entries);
77
        $errorCount = 0;
78
        $label = __('Console', 'blackbar');
79
        foreach ($this->entries as $entry) {
80
            if (in_array($entry['errno'], [E_WARNING])) {
81
                $class = 'glbb-warning';
82
            }
83
            if (in_array($entry['errno'], [E_ERROR])) {
84
                ++$errorCount;
85
            }
86
        }
87
        if ($errorCount > 0) {
88
            $class = 'glbb-error';
89
            $label = sprintf('%s (%d, %d!)', $label, $entryCount, $errorCount);
90
        } elseif ($entryCount > 0) {
91
            $label = sprintf('%s (%d)', $label, $entryCount);
92
        }
93
        return sprintf('<span class="%s">%s</span>', $class, $label);
94
    }
95
96
    public function render(): void
97
    {
98
        $this->app->render('panels/console', ['console' => $this]);
99
    }
100
101
    public function store(string $message, string $errno = '', string $location = ''): void
102
    {
103
        if (is_numeric($errno)) {
104
            // entry likely stored by set_error_handler()
105
            $errname = 'Unknown';
106
            if (array_key_exists((int) $errno, static::ERROR_CODES)) {
107
                $errname = static::ERROR_CODES[$errno];
108
            }
109
        } else {
110
            // entry likely stored by filter hook
111
            $errname = 'Debug';
112
            if (array_key_exists($errno, static::MAPPED_ERROR_CODES)) {
113
                $errname = $errno;
114
                $errno = static::MAPPED_ERROR_CODES[$errno];
115
            }
116
        }
117
        $errname = strtolower($errname);
118
        $hash = md5($errno.$errname.$message.$location);
119
        if (array_key_exists($hash, $this->entries)) {
120
            ++$this->entries[$hash]['count'];
121
        } else {
122
            $this->entries[$hash] = [
123
                'count' => 0,
124
                'errname' => $errname,
125
                'errno' => (int) $errno,
126
                'message' => $this->normalizeMessage($message, $location),
127
            ];
128
        };
129
    }
130
131
    protected function normalizeMessage($message, string $location): string
132
    {
133
        if ($message instanceof \DateTime) {
134
            $message = $message->format('Y-m-d H:i:s');
135
        } elseif (is_object($message) || is_array($message)) {
136
            $message = print_r(json_decode(json_encode($message)), true);
137
        } else {
138
            $message = esc_html(trim((string) $message));
139
        }
140
        $location = trim($location);
141
        if (!empty($location)) {
142
            $location = str_replace([WP_CONTENT_DIR, ABSPATH], '', $location);
143
            $location = sprintf('[%s] ', $location);
144
        }
145
        return $location.$message;
0 ignored issues
show
Bug introduced by
Are you sure $message of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

145
        return $location./** @scrutinizer ignore-type */ $message;
Loading history...
146
    }
147
}
148