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

aplus-framework / debug / 11867178430

24 Oct 2024 08:51PM UTC coverage: 97.243%. Remained the same
11867178430

push

github

natanfelles
Add comment about breakpoint

529 of 544 relevant lines covered (97.24%)

2.67 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

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

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

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

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

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

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

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

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

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

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

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

153
    public static function makeSafeName(string $name) : string
154
    {
155
        return \strtr(\trim(\strip_tags(\strtolower($name))), [
4✔
156
            'ยท' => '-',
4✔
157
            ':' => '-',
4✔
158
            '(' => '-',
4✔
159
            ')' => '-',
4✔
160
            '/' => '-',
4✔
161
            '\\' => '-',
4✔
162
            ' ' => '-',
4✔
163
        ]);
4✔
164
    }
165

166
    public static function convertSize(float | int $size) : string
167
    {
168
        if (empty($size)) {
1✔
169
            return '0 B';
1✔
170
        }
171
        $unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
1✔
172
        $index = \floor(\log($size, 1024));
1✔
173
        return \round($size / (1024 ** $index), 3) . ' ' . $unit[$index];
1✔
174
    }
175

176
    public static function makeDebugValue(mixed $value) : string
177
    {
178
        $type = \get_debug_type($value);
1✔
179
        return (string) match ($type) {
1✔
180
            'array' => 'array',
1✔
181
            'bool' => $value ? 'true' : 'false',
1✔
182
            'float', 'int' => $value,
1✔
183
            'null' => 'null',
1✔
184
            'string' => "'" . \strtr($value, ["'" => "\\'"]) . "'",
1✔
185
            default => 'instanceof ' . $type,
1✔
186
        };
1✔
187
    }
188

189
    /**
190
     * Remove dots and zeros from the end of the version.
191
     *
192
     * @param string $version
193
     *
194
     * @return string
195
     */
196
    public static function roundVersion(string $version) : string
197
    {
198
        if (\str_ends_with($version, '.0')) {
4✔
199
            $version = \substr($version, 0, -2);
1✔
200
            return static::roundVersion($version);
1✔
201
        }
202
        return $version;
4✔
203
    }
204
}
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