Formats::dataSize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace BlueData\Data;
4
5
class Formats
6
{
7
    const SIZE_UNITS = ['B', 'kB', 'MB', 'GB', 'TB', 'PB'];
8
9
    /**
10
     * @param int $bytes
11
     * @return string
12
     */
13 1
    public static function dataSize($bytes)
14
    {
15 1
        $format = '%01.2f %s';
16 1
        $mod = 1000;
17 1
        $power = ($bytes > 0) ? floor(log($bytes, $mod)) : 0;
18
19 1
        return sprintf($format, $bytes / ($mod ** $power), self::SIZE_UNITS[$power]);
20
    }
21
}
22