Byte::format()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 2
nop 2
crap 3
1
<?php
2
/**
3
 * Formatter for byte values
4
 *
5
 * PHP version 5.5
6
 *
7
 * @category   OpCacheGUI
8
 * @package    Format
9
 * @author     Pieter Hordijk <[email protected]>
10
 * @copyright  Copyright (c) 2013 Pieter Hordijk <https://github.com/PeeHaa>
11
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
12
 * @version    1.0.0
13
 */
14
namespace OpCacheGUI\Format;
15
16
/**
17
 * Formatter for byte values
18
 *
19
 * @category   OpCacheGUI
20
 * @package    Format
21
 * @author     Pieter Hordijk <[email protected]>
22
 */
23
class Byte
24
{
25
    /**
26
     * Formats size to byte units
27
     *
28
     * @param int $size     The size in bytes
29
     * @param int $decimals The number of decimals
30
     *
31
     * return $string The formatted bytes
32
     */
33 8
    public function format($size, $decimals = 2)
34
    {
35 8
        $units = ['B', 'KB', 'MB', 'GB'];
36 8
        $position = 0;
37
38
        do {
39 8
            if ($size < 1024) {
40 7
                return round($size, $decimals) . $units[$position];
41
            }
42
43 7
            $size = $size / 1024;
44 7
            $position++;
45 7
        } while ($position < count($units));
46
47 1
        return round($size, $decimals) . end($units);
48
    }
49
}
50