Skip to content

Commit 1fa5c86

Browse files
committed
Refactor Database Collector display
1 parent cb0b371 commit 1fa5c86

File tree

3 files changed

+73
-19
lines changed

3 files changed

+73
-19
lines changed

system/Debug/Toolbar/Collectors/Database.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static function collect(Query $query)
8989
'query' => $query,
9090
'string' => $queryString,
9191
'duplicate' => in_array($queryString, array_column(static::$queries, 'string', null), true),
92-
'trace' => debug_backtrace(),
92+
'trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),
9393
];
9494
}
9595
}
@@ -134,23 +134,24 @@ public function display(): array
134134
$data['queries'] = array_map(static function (array $query) {
135135
$isDuplicate = $query['duplicate'] === true;
136136

137-
// Find the first line that doesn't include `system` in the backtrace
138-
$line = [];
139-
140-
foreach ($query['trace'] as &$traceLine) {
141-
// Clean up the file paths
142-
$traceLine['file'] = str_ireplace(APPPATH, 'APPPATH/', $traceLine['file']);
143-
$traceLine['file'] = str_ireplace(SYSTEMPATH, 'SYSTEMPATH/', $traceLine['file']);
144-
if (defined('VENDORPATH')) {
145-
// VENDORPATH is not defined unless `vendor/autoload.php` exists
146-
$traceLine['file'] = str_ireplace(VENDORPATH, 'VENDORPATH/', $traceLine['file']);
137+
foreach ($query['trace'] as &$line) {
138+
if (isset($line['file'])) {
139+
$line['file'] = clean_path($line['file']) . ':' . $line['line'];
140+
unset($line['line']);
141+
} else {
142+
$line['file'] = '[internal function]';
147143
}
148-
$traceLine['file'] = str_ireplace(ROOTPATH, 'ROOTPATH/', $traceLine['file']);
144+
}
145+
146+
// Find the first line that doesn't include `system` in the backtrace
147+
$firstNonSystemLine = '';
148+
149+
foreach ($query['trace'] as $line) {
150+
if (strpos($line['file'], 'SYSTEMPATH') === false) {
151+
$firstNonSystemLine = $line['file'];
149152

150-
if (strpos($traceLine['file'], 'SYSTEMPATH') !== false) {
151-
continue;
153+
break;
152154
}
153-
$line = empty($line) ? $traceLine : $line;
154155
}
155156

156157
return [
@@ -159,8 +160,7 @@ public function display(): array
159160
'duration' => ((float) $query['query']->getDuration(5) * 1000) . ' ms',
160161
'sql' => $query['query']->debugToolbarDisplay(),
161162
'trace' => $query['trace'],
162-
'trace-file' => str_replace(ROOTPATH, '/', $line['file'] ?? ''),
163-
'trace-line' => $line['line'] ?? '',
163+
'trace-file' => $firstNonSystemLine,
164164
'qid' => md5($query['query'] . microtime()),
165165
];
166166
}, static::$queries);

system/Debug/Toolbar/Views/_database.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
<tr class="{class}" title="{hover}" data-toggle="{qid}-trace">
1111
<td class="narrow">{duration}</td>
1212
<td>{! sql !}</td>
13-
<td style="text-align: right">{trace-file}:<strong>{trace-line}</strong></td>
13+
<td style="text-align: right">{trace-file}</td>
1414
</tr>
1515
<tr class="muted" id="{qid}-trace" style="display:none">
1616
<td></td>
1717
<td colspan="2">
1818
{trace}
19-
{file}:<strong>{line}</strong><br/>
19+
{file}<br/>
2020
{/trace}
2121
</td>
2222
</tr>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Debug\Toolbar\Collectors;
13+
14+
use CodeIgniter\Database\Query;
15+
use CodeIgniter\Test\CIUnitTestCase;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
18+
/**
19+
* @internal
20+
*/
21+
final class DatabaseTest extends CIUnitTestCase
22+
{
23+
public function testDisplay(): void
24+
{
25+
/** @var MockObject&Query $query */
26+
$query = $this->getMockBuilder(Query::class)
27+
->disableOriginalConstructor()
28+
->getMock();
29+
30+
// set mock returns
31+
$query->method('getQuery')->willReturn('SHOW TABLES;');
32+
$query->method('debugToolbarDisplay')->willReturn('<strong>SHOW</strong> TABLES;');
33+
$query->method('getDuration')->with(5)->willReturn('1.23456');
34+
35+
Database::collect($query); // <== $query will be called here
36+
$collector = new Database();
37+
38+
$queries = $collector->display()['queries'];
39+
40+
$this->assertSame('1234.56 ms', $queries[0]['duration']);
41+
$this->assertSame('<strong>SHOW</strong> TABLES;', $queries[0]['sql']);
42+
$this->assertSame(clean_path(__FILE__) . ':35', $queries[0]['trace-file']);
43+
44+
foreach ($queries[0]['trace'] as $trace) {
45+
// since we merged file & line together
46+
$this->assertArrayNotHasKey('line', $trace);
47+
$this->assertArrayHasKey('file', $trace);
48+
49+
// since we dropped object & args in the backtrace for performance
50+
$this->assertArrayNotHasKey('object', $trace);
51+
$this->assertArrayNotHasKey('args', $trace);
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)