FormatTrait::formatBytes()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 16
rs 9.9332
cc 3
nc 4
nop 2
1
<?php
2
namespace Health\Checks\Traits;
3
4
/**
5
 * Formatting operations
6
 */
7
trait FormatTrait
8
{
9
10
    /**
11
     * Format bytes to kb, mb, gb, tb
12
     *
13
     * @param mixed $size
14
     * @param integer $precision
15
     * @return string
16
     */
17
    public function formatBytes($size, $precision = 2)
18
    {
19
        $size = (int) $size;
20
        $base = $size > 0 ? log($size) / log(1024) : 0;
21
22
        $suffixes = [
23
            ' bytes',
24
            ' KB',
25
            ' MB',
26
            ' TB',
27
            ' GB'
28
        ];
29
30
        $value = $size > 0 ? round(pow(1024, $base - floor($base)), $precision) : 0;
31
32
        return $value . $suffixes[floor($base)];
33
    }
34
}
35