• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aplus-framework / database / 15792021985

08 Jun 2025 02:19AM UTC coverage: 99.023% (+0.003%) from 99.02%
15792021985

push

github

natanfelles
Merge branch 'development'

7 of 7 new or added lines in 1 file covered. (100.0%)

2432 of 2456 relevant lines covered (99.02%)

10.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Debug/DatabaseCollector.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of Aplus Framework Database Library.
4
 *
5
 * (c) Natan Felles <natanfelles@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Framework\Database\Debug;
11

12
use Framework\Database\Database;
13
use Framework\Debug\Collector;
14
use Framework\Debug\Debugger;
15

16
/**
17
 * Class DatabaseCollector.
18
 *
19
 * @package database
20
 */
21
class DatabaseCollector extends Collector
22
{
23
    protected Database $database;
24

25
    public function setDatabase(Database $database) : static
26
    {
27
        $this->database = $database;
7✔
28
        return $this;
7✔
29
    }
30

31
    public function getServerInfo() : string
32
    {
33
        return $this->database->getConnection()->server_info;
5✔
34
    }
35

36
    public function getActivities() : array
37
    {
38
        $activities = [];
1✔
39
        foreach ($this->getData() as $index => $data) {
1✔
40
            $activities[] = [
1✔
41
                'collector' => $this->getName(),
1✔
42
                'class' => static::class,
1✔
43
                'description' => 'Run statement ' . ($index + 1),
1✔
44
                'start' => $data['start'],
1✔
45
                'end' => $data['end'],
1✔
46
            ];
1✔
47
        }
48
        return $activities;
1✔
49
    }
50

51
    public function getContents() : string
52
    {
53
        \ob_start();
6✔
54
        if (!isset($this->database)) {
6✔
55
            echo '<p>This collector has not been added to a Database instance.</p>';
1✔
56
            return \ob_get_clean(); // @phpstan-ignore-line
1✔
57
        }
58
        echo $this->showHeader();
5✔
59
        if (!$this->hasData()) {
5✔
60
            echo '<p>Did not run statements.</p>';
3✔
61
            return \ob_get_clean(); // @phpstan-ignore-line
3✔
62
        }
63
        $count = \count($this->getData()); ?>
2✔
64
        <p>Ran <?= $count ?> statement<?= $count === 1 ? '' : 's' ?>
2✔
65
            in <?= $this->getStatementsTime() ?> ms:
2✔
66
        </p>
67
        <table>
68
            <thead>
69
            <tr>
70
                <th>#</th>
71
                <th title="Milliseconds">Time</th>
72
                <th>Statement</th>
73
                <th title="Affected rows or Rows in set">Rows</th>
74
            </tr>
75
            </thead>
76
            <tbody>
77
            <?php foreach ($this->getData() as $index => $item): ?>
2✔
78
                <tr>
2✔
79
                    <td><?= $index + 1 ?></td>
2✔
80
                    <td><?= Debugger::roundSecondsToMilliseconds($item['end'] - $item['start']) ?></td>
2✔
81
                    <td>
82
                        <pre><code class="language-sql"><?=
2✔
83
                                \htmlentities($item['statement'])
2✔
84
                ?></code></pre>
2✔
85
                    </td>
86
                    <td<?= isset($item['description'])
2✔
87
                        ? ' title="' . \htmlentities($item['description']) . '"'
1✔
88
                        : ''?>><?= \htmlentities((string) $item['rows']) ?></td>
2✔
89
                </tr>
90
            <?php endforeach ?>
91
            </tbody>
2✔
92
        </table>
2✔
93
        <?php
2✔
94
        return \ob_get_clean(); // @phpstan-ignore-line
2✔
95
    }
96

97
    protected function getStatementsTime() : float
98
    {
99
        $time = .0;
2✔
100
        foreach ($this->getData() as $data) {
2✔
101
            $total = $data['end'] - $data['start'];
2✔
102
            $time += $total;
2✔
103
        }
104
        return Debugger::roundSecondsToMilliseconds($time);
2✔
105
    }
106

107
    protected function showHeader() : string
108
    {
109
        $config = $this->database->getConfig();
5✔
110
        \ob_start();
5✔
111
        ?>
112
        <p title="<?= 'Connected to ' . \htmlentities($this->getHostInfo()) ?>">
5✔
113
            <strong>Host:</strong> <?= $config['host'] ?? 'localhost' ?>
5✔
114
        </p>
5✔
115
        <?php
116
        if (\str_contains($this->getHostInfo(), 'TCP/IP')) {
5✔
117
            if (isset($config['port'])) {
4✔
118
                ?>
119
                <p><strong>Port:</strong> <?= \htmlentities((string) $config['port']) ?></p>
4✔
120
                <?php
121
            }
122
        } elseif (isset($config['socket'])) { ?>
1✔
123
            <p><strong>Socket:</strong> <?= \htmlentities($config['socket']) ?></p>
1✔
124
            <?php
125
        }
126
        ?>
127
        <p><strong>Server Info:</strong> <?= \htmlentities($this->getServerInfo()) ?></p>
5✔
128
        <?php
129
        return \ob_get_clean(); // @phpstan-ignore-line
5✔
130
    }
131

132
    protected function getHostInfo() : string
133
    {
134
        return $this->database->getConnection()->host_info;
3✔
135
    }
136
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc