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

contributte / dev / 6906202230

17 Nov 2023 03:55PM UTC coverage: 3.81%. Remained the same
6906202230

push

github

f3l1x
Versions: open v0.6.x

4 of 105 relevant lines covered (3.81%)

0.04 hits per line

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

2.9
/src/Dev.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\Dev;
4

5
use Nette\Utils\Json;
6
use RuntimeException;
7
use Tracy\Debugger;
8
use Tracy\Helpers;
9
use Traversable;
10

11
class Dev
12
{
13

14
        final private function __construct()
15
        {
16
                // Static class - cannot be instantiated.
17
        }
18

19
        /**
20
         * Dump;
21
         */
22
        public static function d(): void
23
        {
24
                foreach (func_get_args() as $var) {
×
25
                        dump($var);
×
26
                }
27
        }
28

29
        /**
30
         * Dump; die;
31
         */
32
        public static function dd(): void
33
        {
34
                self::d(...func_get_args());
×
35
                die;
×
36
        }
37

38
        /**
39
         * echo;die
40
         *
41
         * @param scalar $value
42
         */
43
        public static function ed(mixed $value): void
44
        {
45
                echo $value;
×
46
                die;
×
47
        }
48

49
        /**
50
         * Foreach dump;
51
         *
52
         * @param array<mixed|mixed[]> $values
53
         */
54
        public static function fd(array $values): void
55
        {
56
                foreach ($values as $value) {
×
57
                        if ($value instanceof Traversable) {
×
58
                                $value = iterator_to_array($value);
×
59
                        }
60

61
                        dump($value);
×
62
                        echo "<hr style='border:0px;border-top:1px solid #DDD;height:0px;'>";
×
63
                }
64
        }
65

66
        /**
67
         * Foreach dump;die;
68
         *
69
         * @param mixed[] $values
70
         */
71
        public static function fdd(array $values): void
72
        {
73
                self::fd($values);
×
74
                die;
×
75
        }
76

77
        /**
78
         * Table dump;
79
         *
80
         * @param mixed[] $values
81
         */
82
        public static function td(array $values): void
83
        {
84
                echo "<table border=1 style='border-color:#DDD;border-collapse:collapse; font-family:Courier New; color:#222; font-size:13px' cellspacing=0 cellpadding=5>";
×
85
                $th = false;
×
86
                foreach ($values as $value) {
×
87
                        if (!$th) {
×
88
                                echo '<tr>';
×
89
                                foreach ((array) $value as $key2 => $value2) {
×
90
                                        echo '<th>' . $key2 . '</th>';
×
91
                                }
92

93
                                echo '</tr>';
×
94
                        }
95

96
                        $th = true;
×
97

98
                        echo '<tr>';
×
99
                        foreach ((array) $value as $key2 => $value2) {
×
100
                                echo '<td>' . $value2 . '</td>';
×
101
                        }
102

103
                        echo '</tr>';
×
104
                }
105

106
                echo '</table>';
×
107
        }
108

109
        /**
110
         * Table dump;die;
111
         *
112
         * @param mixed[] $values
113
         */
114
        public static function tdd(array $values): void
115
        {
116
                self::td($values);
×
117
                die;
×
118
        }
119

120
        /**
121
         * Bar dump shortcut.
122
         */
123
        public static function bd(mixed $var, ?string $title = null): mixed
124
        {
125
                $trace = debug_backtrace();
×
126
                $traceTitle = (isset($trace[1]['class']) ? htmlspecialchars($trace[1]['class']) . '->' : null) .
×
127
                        htmlspecialchars($trace[1]['function']) . '():';
×
128

129
                if ($title === null) {
×
130
                        foreach (func_get_args() as $arg) {
×
131
                                Debugger::barDump($arg, $traceTitle);
×
132
                        }
133

134
                        return $var;
×
135
                }
136

137
                return Debugger::barDump($var, $title);
×
138
        }
139

140
        /**
141
         * Function prints from where were method/function called
142
         */
143
        public static function wc(int $level = 1, bool $return = false, bool $fullTrace = false): mixed
144
        {
145
                $o = fn ($t) => (isset($t->class) ? htmlspecialchars($t->class) . '->' : null) . htmlspecialchars($t->function) . '()';
×
146
                $f = fn ($t) => isset($t->file) ? '(' . Helpers::editorLink($t->file, $t->line ?? null) . ')' : null;
×
147

148
                $trace = debug_backtrace();
×
149
                $target = (object) $trace[$level];
×
150
                $caller = (object) $trace[$level + 1];
×
151
                $message = null;
×
152

153
                if ($fullTrace) {
×
154
                        array_shift($trace);
×
155
                        foreach ($trace as $call) {
×
156
                                $message .= $o((object) $call) . " \n";
×
157
                        }
158
                } else {
159
                        $message = $o($target) . ' called from ' . $o($caller) . $f($caller);
×
160
                }
161

162
                if ($return) {
×
163
                        return strip_tags((string) $message);
×
164
                }
165

166
                echo "<pre class='nette-dump'>" . nl2br((string) $message) . '</pre>';
×
167

168
                return null;
×
169
        }
170

171
        /**
172
         * Function prints from where were method/function called
173
         */
174
        public static function fwc(int $level = 3, bool $return = false): void
175
        {
176
                self::wc($level, $return, true);
×
177
        }
178

179
        /**
180
         * Convert script into shortcut; exit;
181
         */
182
        public static function ss(string $code): void
183
        {
184
                $array = [
×
185
                        "\t" => "\\t",
186
                        "\n" => "\\n",
187
                ];
188

189
                echo strtr($code, $array);
×
190
                exit;
×
191
        }
192

193
        /**
194
         * Show debug bar
195
         */
196
        public static function e(): void
197
        {
198
                throw new RuntimeException('debug');
×
199
        }
200

201
        /**
202
         * Log message (alias for log)
203
         */
204
        public static function l(string $message): void
205
        {
206
                self::log($message);
×
207
        }
208

209
        /**
210
         * Log message
211
         */
212
        public static function log(string $message): void
213
        {
214
                $message = array_map(fn ($message) => !is_scalar($message) ? Json::encode($message) : $message, func_get_args());
×
215

216
                Debugger::log(implode(', ', $message));
×
217
        }
218

219
        /**
220
         * Show debug bar and dump $arg
221
         */
222
        public static function erd(): void
223
        {
224
                $e = new RuntimeException();
×
225
                fd(func_get_args());
×
226
                echo '<hr />';
×
227
                fd($e->getTrace());
×
228
                echo '<hr />';
×
229
        }
230

231
        /**
232
         * PHP workaround for direct usage of created class
233
         *
234
         * <code>
235
         * // echo (new Person)->name; // does not work in PHP
236
         * echo c(new Person)->name;
237
         * </code>
238
         */
239
        public static function c(object $instance): object
240
        {
241
                return $instance;
×
242
        }
243

244
        /**
245
         * PHP workaround for direct usage of cloned instances
246
         *
247
         * <code>
248
         * echo cl($startTime)->modify('+1 day')->format('Y-m-d');
249
         * </code>
250
         */
251
        public static function cl(object $instance): object
252
        {
253
                return clone $instance;
×
254
        }
255

256
        /**
257
         * PHP callback workaround
258
         *
259
         * @return array{object, string}
260
         */
261
        public static function callback(object $obj, string $method): array
1✔
262
        {
263
                return [$obj, $method];
1✔
264
        }
265

266
}
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