1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace SavinMikhail\DistSizeOptimizer; |
||
6 | |||
7 | use function count; |
||
8 | |||
9 | function formatBytes(int $bytes, int $precision = 2): string |
||
10 | { |
||
11 | 3 | $units = ['B', 'KB', 'MB', 'GB']; |
|
12 | 3 | $bytes = max($bytes, 0); |
|
13 | 3 | $pow = $bytes > 0 ? floor(num: log(num: $bytes, base: 1_024)) : 0; |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
14 | 3 | $pow = min($pow, count(value: $units) - 1); |
|
15 | 3 | $bytes /= (1_024 ** $pow); |
|
16 | |||
17 | 3 | return round(num: $bytes, precision: $precision) . ' ' . $units[$pow]; |
|
18 | } |
||
19 |