BytesFormatter::formatBytes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools;
6
7
use function floor;
8
use function log;
9
use function pow;
10
use function round;
11
12
/**
13
 * The BytesFormatter class is responsible for converting a bytes integer to a more human readable string.
14
 * This class is used to format the memory used for display purposes when executing migrations.
15
 *
16
 * @internal
17
 */
18
final class BytesFormatter
19
{
20 13
    public static function formatBytes(float $size, int $precision = 2) : string
21
    {
22 13
        $base     = log($size, 1024);
23 13
        $suffixes = ['', 'K', 'M', 'G', 'T'];
24
25 13
        return round(pow(1024, $base - floor($base)), $precision) . $suffixes[(int) floor($base)];
26
    }
27
}
28