Passed
Push — master ( de3d61...be839c )
by Alec
13:42 queued 13s
created

BytesFormatter::getUnits()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Helper;
6
7
use AlecRabbit\Spinner\Helper\Contract\IBytesFormatter;
8
9
final class BytesFormatter implements IBytesFormatter
10
{
11
    private const UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB']; // 'EB' is max for int on 64-bit systems
12
13
    public function format(int $bytes): string
14
    {
15
        $i = (int)floor(log($bytes, 1024));
16
        return round($bytes / (1024 ** $i), 2) . self::getUnits($i);
17
    }
18
19
    private static function getUnits(int $i): string
20
    {
21
        return self::UNITS[$i];
22
    }
23
}
24