Formatter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 35
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A date() 0 11 3
A bytes() 0 10 2
1
<?php
2
3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CacheTool\Util;
13
14
class Formatter
15
{
16
    /**
17
     * @param  integer  $bytes
18
     * @param  integer $precision
19
     * @return string
20
     */
21 3
    public static function bytes($bytes, $precision = 2)
22
    {
23 3
        $units = ['b', 'KiB', 'MiB', 'GiB', 'TiB'];
24
25 3
        $bytes = max($bytes, 0);
26 3
        $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
27 3
        $pow = min($pow, count($units) - 1);
28 3
        $bytes /= (1 << (10 * $pow));
29
30 3
        return round($bytes, $precision) . ' ' . $units[$pow];
31
    }
32
33
    /**
34
     * @param  mixed  $date
35
     * @param  string $format
36
     * @return string
37
     */
38 2
    public static function date($date, $format = null)
39
    {
40 2
        if (false === $date instanceof \DateTime) {
41 2
            if ($format !== null) {
42 2
                $date = \DateTime::createFromFormat($format, $date);
43
            } else {
44
                $date = new \DateTime($date);
45
            }
46
        }
47
48 2
        return $date->format(\DateTime::RFC2822);
49
    }
50
}
51