Queries   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 44
dl 0
loc 71
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A label() 0 3 1
A sortByTime() 0 6 3
A hasEntries() 0 4 1
A entries() 0 39 5
A info() 0 9 3
1
<?php
2
3
namespace GeminiLabs\BlackBar\Modules;
4
5
class Queries extends Module
6
{
7
    public function entries(): array
8
    {
9
        global $wpdb;
10
        $entries = [];
11
        $index = 0;
12
        $search = [
13
            'FROM', 'GROUP BY', 'INNER JOIN', 'LEFT JOIN', 'LIMIT',
14
            'ON DUPLICATE KEY UPDATE', 'ORDER BY', 'OFFSET', ' SET', 'WHERE',
15
        ];
16
        $replace = array_map(function ($value) {
17
            return PHP_EOL.$value;
18
        }, $search);
19
        foreach ($wpdb->queries as $query) {
20
            $sql = preg_replace('/\s\s+/', ' ', trim($query[0]));
21
            $sql = str_replace(PHP_EOL, ' ', $sql);
22
            $sql = str_replace(['( ', ' )', ' ,'], ['(', ')', ','], $sql);
23
            $sql = str_replace($search, $replace, $sql);
24
            $parts = explode(PHP_EOL, $sql);
25
            $sql = array_reduce($parts, function ($carry, $part) {
26
                if (str_starts_with($part, 'SELECT') && strlen($part) > 100) {
27
                    $part = preg_replace('/\s*(,)\s*/', ','.PHP_EOL.'  ', $part);
28
                }
29
                if (str_starts_with($part, 'WHERE')) {
30
                    $part = str_replace('AND', PHP_EOL.'  AND', $part);
31
                }
32
                return $carry.$part.PHP_EOL;
33
            });
34
            $trace = explode(', ', $query[2]);
35
            $nanoseconds = (int) round($query[1] * 1e9);
36
            $entries[] = [
37
                'index' => $index++,
38
                'sql' => $sql,
39
                'time' => $nanoseconds,
40
                'time_formatted' => $this->formatTime($nanoseconds),
41
                'trace' => array_reverse($trace, true),
42
            ];
43
        }
44
        uasort($entries, [$this, 'sortByTime']);
45
        return $entries;
46
    }
47
48
    public function hasEntries(): bool
49
    {
50
        global $wpdb;
51
        return !empty($wpdb->queries);
52
    }
53
54
    public function info(): string
55
    {
56
        if (!defined('SAVEQUERIES') || !SAVEQUERIES) {
57
            return '';
58
        }
59
        global $wpdb;
60
        $seconds = (float) array_sum(wp_list_pluck($wpdb->queries, 1));
61
        $nanoseconds = (int) round($seconds * 1e9);
62
        return $this->formatTime($nanoseconds);
63
    }
64
65
    public function label(): string
66
    {
67
        return __('SQL', 'blackbar');
68
    }
69
70
    protected function sortByTime(array $a, array $b): int
71
    {
72
        if ($a['time'] !== $b['time']) {
73
            return ($a['time'] > $b['time']) ? -1 : 1;
74
        }
75
        return 0;
76
    }
77
}
78