-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.php
More file actions
253 lines (230 loc) · 7.54 KB
/
Copy pathHelpers.php
File metadata and controls
253 lines (230 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
/**
* Helpers — Part of the MaplePHP Unitary Testing Library
*
* @package: MaplePHP\Unitary
* @author: Daniel Ronkainen
* @licence: Apache-2.0 license, Copyright © Daniel Ronkainen
* Don't delete this comment, it's part of the license.
*/
declare(strict_types=1);
namespace MaplePHP\Unitary\Support;
use ErrorException;
use Exception;
use MaplePHP\Blunder\ExceptionItem;
use MaplePHP\Blunder\Handlers\CliHandler;
use MaplePHP\DTO\Format\Str;
final class Helpers
{
/**
* Convert bytes to megabytes and return as a string with fixed precision.
*
* Note: 1 MB = 1024 * 1024 bytes
*
* @param int $memoryInByte Memory size in bytes.
* @return string Memory size in megabytes
*/
public static function byteToMegabyte(int $memoryInByte): string
{
return number_format(round($memoryInByte / 1048576, 4), 4, '.', '');
}
/**
* Round and format the duration for end-user display string with fixed precision.
*
* @param float $duration Duration in seconds.
* @return string Formatted duration (e.g. "0.123456").
*/
public static function formatDuration(float $duration): string
{
return number_format(round($duration, 6), 6, '.', '');
}
/**
* Convert a throwable into ExceptionItem
*
* @param \Throwable $exception
* @return ExceptionItem
*/
public static function getExceptionItem(\Throwable $exception): ExceptionItem
{
return new ExceptionItem($exception);
}
/**
* Get a pretty exception message from a Throwable instance
*
* @param \Throwable $exception
* @param ExceptionItem|null $exceptionItem Use ExceptionItem to get more options
* @return string
*/
public static function getExceptionMessage(\Throwable $exception, ?ExceptionItem &$exceptionItem = null): string
{
$exceptionItem = self::getExceptionItem($exception);
$cliErrorHandler = new CliHandler();
return $cliErrorHandler->getSmallErrorMessage($exceptionItem);
}
/**
* Used to stringify arguments to show in a test
*
* @param mixed $args
* @return string
*/
public static function stringifyArgs(mixed $args): string
{
$levels = 0;
$str = self::stringify($args, $levels);
if ($levels > 1) {
return "[$str]";
}
return $str;
}
/**
* Stringify an array and objects
*
* @param mixed $arg
* @param int $levels
* @return string
*/
public static function stringify(mixed $arg, int &$levels = 0): string
{
if (is_array($arg)) {
$items = array_map(function ($item) use (&$levels) {
$levels++;
return self::stringify($item, $levels);
}, $arg);
return implode(', ', $items);
}
if (is_object($arg)) {
return get_class($arg);
}
return (string)$arg;
}
/**
* Create a file instead of eval for improved debug
*
* @param string $filename
* @param string $input
* @return void
* @throws Exception
*/
public static function createFile(string $filename, string $input): void
{
$temp = getenv('UNITARY_TEMP_DIR');
$tempDir = $temp !== false ? $temp : sys_get_temp_dir();
if (!is_dir($tempDir)) {
mkdir($tempDir, 0777, true);
}
$tempFile = rtrim($tempDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $filename;
file_put_contents($tempFile, "<?php\n" . $input);
if (!is_file($tempFile)) {
throw new Exception("Unable to create file $tempFile");
}
include $tempFile;
/*
register_shutdown_function(function () use ($tempFile) {
unlink($tempFile);
});
*/
}
/**
* Processes a trace array to retrieve specific details about the file, line, and code context.
*
* @param array $trace The trace array containing details such as file and line.
* @return array An associative array with keys 'line', 'file', and 'code' representing the line number, file path, and contextual code respectively.
* @throws ErrorException
*/
public static function getTrace(array $trace): array
{
$codeLine = [
'line' => 0,
'file' => '',
'code' => 0
];
$file = (string)($trace['file'] ?? '');
if (is_file($file)) {
$line = (int)($trace['line'] ?? 0);
$lines = file($file);
$code = "";
if ($lines !== false) {
$code = trim($lines[$line - 1] ?? '');
if (str_starts_with($code, '->')) {
$code = substr($code, 2);
}
$code = self::excerpt($code);
}
$codeLine['line'] = $line;
$codeLine['file'] = $file;
$codeLine['code'] = $code;
}
return $codeLine;
}
/**
* Create a checksum that is this array
*
* @param array $array
* @return string
* @throws \JsonException
*/
public static function md5Array(array $array): string
{
$normalized = json_encode($array, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
if($normalized === false) {
return '';
}
return md5($normalized);
}
/**
* Generates an excerpt from the given string with a specified maximum length.
*
* @param string $value The input string to be excerpted.
* @param int $length The maximum length of the excerpt. Defaults to 80.
* @return string The resulting excerpted string.
* @throws ErrorException
*/
final public static function excerpt(string $value, int $length = 80): string
{
$format = new Str($value);
return (string)$format->excerpt($length)->get();
}
/**
* Used to get a readable value (Move to utility)
*
* @param mixed|null $value
* @param bool $minify
* @return string
* @throws ErrorException
*/
public static function stringifyDataTypes(mixed $value = null, bool $minify = false): string
{
if (is_bool($value)) {
return '"' . ($value ? "true" : "false") . '"' . ($minify ? "" : " (type: bool)");
}
if (is_int($value)) {
return '"' . self::excerpt((string)$value) . '"' . ($minify ? "" : " (type: int)");
}
if (is_float($value)) {
return '"' . self::excerpt((string)$value) . '"' . ($minify ? "" : " (type: float)");
}
if (is_string($value)) {
return '"' . self::excerpt($value) . '"' . ($minify ? "" : " (type: string)");
}
if (is_array($value)) {
$json = json_encode($value);
if ($json === false) {
return "(unknown type)";
}
return '"' . self::excerpt($json) . '"' . ($minify ? "" : " (type: array)");
}
if (is_callable($value)) {
return '"' . self::excerpt(get_class((object)$value)) . '"' . ($minify ? "" : " (type: callable)");
}
if (is_object($value)) {
return '"' . self::excerpt(get_class($value)) . '"' . ($minify ? "" : " (type: object)");
}
if ($value === null) {
return '"null"'. ($minify ? '' : ' (type: null)');
}
if (is_resource($value)) {
return '"' . self::excerpt(get_resource_type($value)) . '"' . ($minify ? "" : " (type: resource)");
}
return "(unknown type)";
}
}