DebugPanel::getDetail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * ActiveRecord for API
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart
6
 * @package   yii2-hiart
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart\debug;
12
13
use hiqdev\hiart\Command;
14
use Yii;
15
use yii\base\ViewContextInterface;
16
use yii\debug\Panel;
0 ignored issues
show
Bug introduced by
The type yii\debug\Panel 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...
17
use yii\helpers\ArrayHelper;
18
use yii\log\Logger;
19
20
/**
21
 * Debugger panel that collects and displays HiArt queries performed.
22
 */
23
class DebugPanel extends Panel implements ViewContextInterface
24
{
25
    public function init()
26
    {
27
        $this->actions['hiart-query'] = [
0 ignored issues
show
Bug Best Practice introduced by
The property actions does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
28
            'class' => DebugAction::class,
29
            'panel' => $this,
30
        ];
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getName()
37
    {
38
        return 'HiArt';
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getSummary()
45
    {
46
        $timings = $this->getTimings();
47
        $total = 0;
48
        foreach ($timings as $timing) {
49
            $total += $timing[3];
50
        }
51
52
        return $this->render('summary', [
53
            'url' => $this->getUrl(),
54
            'count' => count($timings),
55
            'total' => number_format($total * 1000) . ' ms',
56
        ]);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getDetail()
63
    {
64
        return $this->render('detail', [
65
            'timings' => Timing::buildAll($this),
66
        ]);
67
    }
68
69
    private $_timings;
70
71
    public function getTimings()
72
    {
73
        if ($this->_timings === null) {
74
            $this->_timings = $this->calculateTimings();
75
        }
76
77
        return $this->_timings;
78
    }
79
80
    public function calculateTimings()
81
    {
82
        $messages = $this->data['messages'];
83
        $timings = [];
84
        $stack = [];
85
        $groups = ArrayHelper::index($messages, 1, 0);
86
        foreach ($groups as $token => $logs) {
87
            if (count($logs) !== 2) {
88
                continue;
89
            }
90
            $begin = $logs[Logger::LEVEL_PROFILE_BEGIN];
91
            $end = $logs[Logger::LEVEL_PROFILE_END];
92
            $timings[$begin[5]] = [
93
                count($stack),
94
                $token,
95
                $end[3],
96
                $end[3] - $begin[3],
97
                $begin[4],
98
            ];
99
        }
100
        ksort($timings);
101
102
        return $timings;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function save()
109
    {
110
        $target = $this->module->logTarget;
111
        $messages = $target->filterMessages($target->messages, Logger::LEVEL_PROFILE, [Command::getProfileCategory()]);
112
113
        return ['messages' => $messages];
114
    }
115
116
    protected $_viewPath;
117
118
    public function setViewPath($value)
119
    {
120
        $this->_viewPath = $value;
121
    }
122
123
    public function getViewPath()
124
    {
125
        if ($this->_viewPath === null) {
126
            $this->_viewPath = dirname(__DIR__) . '/views/debug';
127
        }
128
129
        return $this->_viewPath;
130
    }
131
132
    public function render($file, $data)
133
    {
134
        return Yii::$app->view->render($file, $data, $this);
135
    }
136
}
137