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

aplus-framework / debug / 12309578219

07 Dec 2024 11:27PM UTC coverage: 97.273%. Remained the same
12309578219

push

github

natanfelles
Add height attribute to debugbar icon

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

535 of 550 relevant lines covered (97.27%)

2.73 hits per line

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

100.0
/src/Debugger.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of Aplus Framework Debug 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\Debug;
11

12
use Framework\Helpers\Isolation;
13
use InvalidArgumentException;
14

15
/**
16
 * Class Debugger.
17
 *
18
 * @package debug
19
 */
20
class Debugger
21
{
22
    /**
23
     * @var array<string,Collection>
24
     */
25
    protected array $collections = [];
26
    /**
27
     * @var array<string,mixed>
28
     */
29
    protected array $options = [];
30
    protected string $debugbarView = __DIR__ . '/Views/debugbar/debugbar.php';
31
    protected bool $debugbarEnabled = true;
32

33
    public function addCollection(Collection $collection) : static
34
    {
35
        $this->collections[$collection->getName()] = $collection;
4✔
36
        return $this;
4✔
37
    }
38

39
    /**
40
     * @return array<string,Collection>
41
     */
42
    public function getCollections() : array
43
    {
44
        return $this->collections;
6✔
45
    }
46

47
    public function getCollection(string $name) : ?Collection
48
    {
49
        return $this->getCollections()[$name] ?? null;
4✔
50
    }
51

52
    public function addCollector(Collector $collector, string $collectionName) : static
53
    {
54
        $collection = $this->getCollection($collectionName);
3✔
55
        if ($collection === null) {
3✔
56
            $collection = new Collection($collectionName);
3✔
57
            $this->addCollection($collection);
3✔
58
        }
59
        $collection->addCollector($collector);
3✔
60
        return $this;
3✔
61
    }
62

63
    /**
64
     * @param array<string,mixed> $options
65
     *
66
     * @return static
67
     */
68
    public function setOptions(array $options) : static
69
    {
70
        $this->options = $options;
1✔
71
        return $this;
1✔
72
    }
73

74
    /**
75
     * @return array<string,mixed>
76
     */
77
    public function getOptions() : array
78
    {
79
        return $this->options;
4✔
80
    }
81

82
    /**
83
     * @return array<string,mixed>
84
     */
85
    public function getActivities() : array
86
    {
87
        $collected = [];
4✔
88
        foreach ($this->getCollections() as $collection) {
4✔
89
            foreach ($collection->getActivities() as $activities) {
2✔
90
                $collected = [...$collected, ...$activities];
1✔
91
            }
92
        }
93
        $min = .0;
4✔
94
        $max = .0;
4✔
95
        if ($collected) {
4✔
96
            \usort($collected, static function ($c1, $c2) {
1✔
97
                return $c1['start'] <=> $c2['start'];
1✔
98
            });
1✔
99
            $min = \min(\array_column($collected, 'start'));
1✔
100
            $max = \max(\array_column($collected, 'end'));
1✔
101
            foreach ($collected as &$activity) {
1✔
102
                $this->addActivityValues($activity, $min, $max);
1✔
103
            }
104
        }
105
        return [
4✔
106
            'min' => $min,
4✔
107
            'max' => $max,
4✔
108
            'total' => $max - $min,
4✔
109
            'collected' => $collected,
4✔
110
        ];
4✔
111
    }
112

113
    /**
114
     * @param array<string,mixed> $activity
115
     * @param float $min
116
     * @param float $max
117
     */
118
    protected function addActivityValues(array &$activity, float $min, float $max) : void
119
    {
120
        $total = $max - $min;
1✔
121
        $activity['total'] = $activity['end'] - $activity['start'];
1✔
122
        $activity['left'] = \round(($activity['start'] - $min) * 100 / $total, 3);
1✔
123
        $activity['width'] = \round($activity['total'] * 100 / $total, 3);
1✔
124
    }
125

126
    public function setDebugbarView(string $file) : static
127
    {
128
        $realpath = \realpath($file);
1✔
129
        if (!$realpath || !\is_file($realpath)) {
1✔
130
            throw new InvalidArgumentException(
1✔
131
                'Invalid debugbar view file: ' . $file
1✔
132
            );
1✔
133
        }
134
        $this->debugbarView = $realpath;
1✔
135
        return $this;
1✔
136
    }
137

138
    public function getDebugbarView() : string
139
    {
140
        return $this->debugbarView;
5✔
141
    }
142

143
    public function renderDebugbar() : string
144
    {
145
        if (!$this->isDebugbarEnabled()) {
4✔
146
            return '';
1✔
147
        }
148
        \ob_start();
4✔
149
        Isolation::require($this->getDebugbarView(), [
4✔
150
            'collections' => $this->getCollections(),
4✔
151
            'activities' => $this->getActivities(),
4✔
152
            'options' => $this->getOptions(),
4✔
153
        ]);
4✔
154
        return \ob_get_clean(); // @phpstan-ignore-line
4✔
155
    }
156

157
    /**
158
     * Tells if debugbar rendering is enabled.
159
     *
160
     * @since 4.2
161
     *
162
     * @return bool
163
     */
164
    public function isDebugbarEnabled() : bool
165
    {
166
        return $this->debugbarEnabled;
4✔
167
    }
168

169
    /**
170
     * Enables debugbar rendering.
171
     *
172
     * @since 4.2
173
     *
174
     * @return static
175
     */
176
    public function enableDebugbar() : static
177
    {
178
        $this->debugbarEnabled = true;
1✔
179
        return $this;
1✔
180
    }
181

182
    /**
183
     * Disables debugbar rendering.
184
     *
185
     * @since 4.2
186
     *
187
     * @return static
188
     */
189
    public function disableDebugbar() : static
190
    {
191
        $this->debugbarEnabled = false;
1✔
192
        return $this;
1✔
193
    }
194

195
    public static function makeSafeName(string $name) : string
196
    {
197
        return \strtr(\trim(\strip_tags(\strtolower($name))), [
4✔
198
            'ยท' => '-',
4✔
199
            ':' => '-',
4✔
200
            '(' => '-',
4✔
201
            ')' => '-',
4✔
202
            '/' => '-',
4✔
203
            '\\' => '-',
4✔
204
            ' ' => '-',
4✔
205
        ]);
4✔
206
    }
207

208
    public static function convertSize(float | int $size) : string
209
    {
210
        if (empty($size)) {
1✔
211
            return '0 B';
1✔
212
        }
213
        $unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
1✔
214
        $index = \floor(\log($size, 1024));
1✔
215
        return \round($size / (1024 ** $index), 3) . ' ' . $unit[$index];
1✔
216
    }
217

218
    public static function makeDebugValue(mixed $value) : string
219
    {
220
        $type = \get_debug_type($value);
1✔
221
        return (string) match ($type) {
1✔
222
            'array' => 'array',
1✔
223
            'bool' => $value ? 'true' : 'false',
1✔
224
            'float', 'int' => $value,
1✔
225
            'null' => 'null',
1✔
226
            'string' => "'" . \strtr($value, ["'" => "\\'"]) . "'",
1✔
227
            default => 'instanceof ' . $type,
1✔
228
        };
1✔
229
    }
230

231
    /**
232
     * Remove dots and zeros from the end of the version.
233
     *
234
     * @param string $version
235
     *
236
     * @return string
237
     */
238
    public static function roundVersion(string $version) : string
239
    {
240
        if (\str_ends_with($version, '.0')) {
5✔
241
            $version = \substr($version, 0, -2);
1✔
242
            return static::roundVersion($version);
1✔
243
        }
244
        return $version;
5✔
245
    }
246
}
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

© 2025 Coveralls, Inc