Passed
Push — master ( 98a22c...456092 )
by Bernardette
02:12
created

TracyBar::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Rostenkowski\Doctrine\Debugger;
4
5
6
use Doctrine\DBAL\Logging\SQLLogger;
7
use Nette\Utils\Html;
8
use const PHP_EOL;
0 ignored issues
show
Bug introduced by
The type PHP_EOL 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...
9
use Tracy\Dumper;
10
use Tracy\Helpers;
11
use Tracy\IBarPanel;
12
use const DEBUG_BACKTRACE_IGNORE_ARGS;
0 ignored issues
show
Bug introduced by
The type DEBUG_BACKTRACE_IGNORE_ARGS 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...
13
use function count;
14
use function debug_backtrace;
0 ignored issues
show
Bug introduced by
The type debug_backtrace 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...
15
use function dirname;
0 ignored issues
show
Bug introduced by
The type dirname 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...
16
use function strlen;
0 ignored issues
show
Bug introduced by
The type strlen 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 function substr;
0 ignored issues
show
Bug introduced by
The type substr 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...
18
use function var_dump;
0 ignored issues
show
Bug introduced by
The type var_dump 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...
19
20
class TracyBar implements SQLLogger, IBarPanel
21
{
22
23
	/**
24
	 * @var string
25
	 */
26
	private $appDir;
27
28
	/**
29
	 * @var float
30
	 */
31
	private $start;
32
33
	/**
34
	 * @var int
35
	 */
36
	private $current;
37
38
	/**
39
	 * @var array
40
	 */
41
	private $queries = [];
42
43
	private $totalTime;
44
45
	/**
46
	 * debugger panel max width
47
	 *
48
	 * @var string
49
	 */
50
	private $width = '960px';
51
52
	/**
53
	 * debugger panel max height
54
	 *
55
	 * @var string
56
	 */
57
	private $height = '720px';
58
59
60
	public function __construct(string $appDir)
61
	{
62
		$this->appDir = $appDir;
63
	}
64
65
66
	public function getHeight(): string
67
	{
68
		return $this->height;
69
	}
70
71
72
	public function setHeight(string $height)
73
	{
74
		$this->height = $height;
75
	}
76
77
78
	public function getPanel()
79
	{
80
		$totalQueries = count($this->queries);
81
		$color = $totalQueries ? 'green' : '#555555';
82
		$timeCaption = $this->formatTime($this->getTotalTime());
83
		$panel = $this->getTemplate('panel');
84
		$row = $this->getTemplate('query');
85
		$buffer = '';
86
		$colorizer = new SimpleQueryColorizer();
87
		foreach ($this->queries as $query) {
88
89
			$link = Helpers::editorUri($query['file'], $query['line']);
90
			$linkText = '…/' . substr($query['file'], strlen(dirname($this->appDir)) + 1) . ':' . $query['line'];
91
			$a = Html::el('a')->setAttribute('href', $link)->setText($linkText);
92
			$buffer .= sprintf($row,
93
				$this->formatTime($query['dur']),
94
				$colorizer->colorize($query['sql'], true),
95
				$this->dump($query['params']),
96
				(string) $a
97
			);
98
		}
99
100
		return sprintf($panel, $color, $timeCaption, $totalQueries, $this->width, $this->height, $buffer);
101
	}
102
103
104
	public function getTab()
105
	{
106
		$count = count($this->queries);
107
		$color = $count ? 'green' : '#555555';
108
		$time = $this->formatTime($this->getTotalTime());
109
		$template = $this->getTemplate('tab');
110
111
		return sprintf($template, $color, $time, $count);
112
	}
113
114
115
	private function formatTime($microseconds)
116
	{
117
		return number_format($microseconds * 1000, 1, '.', ' ') . ' ms';
118
	}
119
120
121
	private function getTotalTime()
122
	{
123
		if ($this->totalTime === NULL) {
124
			foreach ($this->queries as $query) {
125
				$this->totalTime += $query['dur'];
126
			}
127
		}
128
129
		return $this->totalTime;
130
	}
131
132
133
	private function getTemplate($name)
134
	{
135
		return file_get_contents(__DIR__ . "/templates/$name.html");
136
	}
137
138
139
	private function dump($params)
140
	{
141
		if ($params) {
142
143
			return Dumper::toHtml($params, [Dumper::COLLAPSE => 1]);
144
		}
145
146
		return '';
147
	}
148
149
150
	public function getWidth(): string
151
	{
152
		return $this->width;
153
	}
154
155
156
	public function setWidth(string $width)
157
	{
158
		$this->width = $width;
159
	}
160
161
162
	public function startQuery($sql, array $params = NULL, array $types = NULL)
163
	{
164
		$this->start = (float) microtime(true);
165
166
		$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
167
168
		foreach ($trace as $i => $item) {
169
			if (substr($trace[$i]['file'], 0, strlen($this->appDir)) === $this->appDir) {
170
				break;
171
			}
172
		}
173
174
		$this->queries[++$this->current] = [
175
			'sql'    => trim($sql, " \t\n\r\0\x0B\""),
176
			'params' => $params,
177
			'types'  => $types,
178
			'dur'    => 0,
179
			'file'   => $trace[$i]['file'],
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $i seems to be defined by a foreach iteration on line 168. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
180
			'line'   => $trace[$i]['line'],
181
		];
182
	}
183
184
185
	public function stopQuery()
186
	{
187
		$this->queries[$this->current]['dur'] = (float) microtime(true) - $this->start;
188
	}
189
190
191
}
192