Completed
Push — master ( 59b3e2...6dd6b9 )
by Andrii
15:38
created

DebugPanel::calculateTimings()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 19
nc 10
nop 0
1
<?php
2
/**
3
 * Tools to use API as ActiveRecord for Yii2
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart
6
 * @package   yii2-hiart
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart\debug;
12
13
use hiqdev\hiart\Command;
14
use Yii;
15
use yii\base\InvalidConfigException;
16
use yii\base\ViewContextInterface;
17
use yii\log\Logger;
18
19
/**
20
 * Debugger panel that collects and displays HiArt queries performed.
21
 */
22
class DebugPanel extends \yii\debug\Panel implements ViewContextInterface
23
{
24
    public function init()
25
    {
26
        $this->actions['hiart-query'] = [
27
            'class' => DebugAction::class,
28
            'panel' => $this,
29
        ];
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getName()
36
    {
37
        return 'HiArt';
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getSummary()
44
    {
45
        $timings = $this->getTimings();
46
        $total = 0;
47
        foreach ($timings as $timing) {
48
            $total += $timing[3];
49
        }
50
51
        return $this->render('summary', [
52
            'url'   => $this->getUrl(),
53
            'count' => count($timings),
54
            'total' => number_format($total * 1000) . ' ms',
55
        ]);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getDetail()
62
    {
63
        return $this->render('detail', [
64
            'timings' => Timing::buildAll($this),
65
        ]);
66
    }
67
68
    private $_timings;
69
70
    public function getTimings()
71
    {
72
        if ($this->_timings === null) {
73
            $this->_timings = $this->calculateTimings();
74
        }
75
76
        return $this->_timings;
77
    }
78
79
    public function calculateTimings()
80
    {
81
        $messages = $this->data['messages'];
82
        $timings = [];
83
        $stack = [];
84
        foreach ($messages as $i => $log) {
85
            list($token, $level, $category, $timestamp) = $log;
0 ignored issues
show
Unused Code introduced by
The assignment to $category is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
86
            $log[5] = $i;
87
            if ($level === Logger::LEVEL_PROFILE_BEGIN) {
88
                $stack[] = $log;
89
            } elseif ($level === Logger::LEVEL_PROFILE_END) {
90
                $last = array_pop($stack);
91
                if ($last !== null && $last[0] === $token) {
92
                    $timings[$last[5]] = [count($stack), $token, $last[3], $timestamp - $last[3], $last[4]];
93
                }
94
            }
95
        }
96
97
        $now = microtime(true);
98
        while (($last = array_pop($stack)) !== null) {
99
            $delta = $now - $last[3];
100
            $timings[$last[5]] = [count($stack), $last[0], $last[2], $delta, $last[4]];
101
        }
102
        ksort($timings);
103
104
        return $timings;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function save()
111
    {
112
        $target = $this->module->logTarget;
113
        $messages = $target->filterMessages($target->messages, Logger::LEVEL_PROFILE, [Command::getProfileCategory()]);
114
115
        return ['messages' => $messages];
116
    }
117
118
    protected $_viewPath;
119
120
    public function setViewPath($value)
121
    {
122
        $this->_viewPath = $value;
123
    }
124
125
    public function getViewPath()
126
    {
127
        if ($this->_viewPath === null) {
128
            $this->_viewPath = dirname(__DIR__) . '/views/debug';
129
        }
130
        return $this->_viewPath;
131
    }
132
133
    public function render($file, $data)
134
    {
135
        return Yii::$app->view->render($file, $data, $this);
136
    }
137
}
138