Format::humanReadableSize()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Spatie\DbSnapshots\Helpers;
4
5
class Format
6
{
7
    public static function humanReadableSize(int $sizeInBytes): string
8
    {
9
        $units = ['B', 'KB', 'MB', 'GB', 'TB'];
10
11
        if ($sizeInBytes === 0) {
12
            return '0 '.$units[1];
13
        }
14
        for ($i = 0; $sizeInBytes > 1024; $i++) {
15
            $sizeInBytes /= 1024;
16
        }
17
18
        return round($sizeInBytes, 2).' '.$units[$i];
19
    }
20
}
21