GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

FormatUtils   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 0
dl 0
loc 45
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A formatBytes() 0 17 2
1
<?php
2
3
namespace WebservicesNl\Utils;
4
5
/**
6
 * Class FormatUtils.
7
 */
8
class FormatUtils
9
{
10
    /**
11
     * @var array
12
     */
13
    protected static $formats = [
14
        'decimal' => [ //  SI Prefixes (decimal)
15
            'mod' => 1000,
16
            'units' => ['B', 'kB', 'MB', 'GB', 'TB', 'PB'],
17
        ],
18
        'binary' => [ // IEC prefixes (binary)
19
            'mod' => 1024,
20
            'units' => ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'],
21
        ],
22
    ];
23
24
    /**
25
     * Return a formatted string (from bytes) in decimal or binary.
26
     *
27
     * @param int|string|float $size
28
     * @param int              $precision
29
     * @param string           $format    either binary or decimal
30
     *
31
     * @throws \InvalidArgumentException
32
     *
33
     * @return string
34
     */
35 2
    public static function formatBytes($size, $precision = 0, $format = 'decimal')
36
    {
37 2
        if (!array_key_exists($format, self::$formats)) {
38 1
            throw new \InvalidArgumentException('Not a valid format');
39
        }
40
41 1
        $format = self::$formats[$format];
42 1
        $precision = (int) $precision;
43
44
        /** @var float $base */
45 1
        $base = log((float) $size, $format['mod']);
46 1
        $key = (int) floor($base);
47
48 1
        $value = round(pow($format['mod'], $base - floor($base)), $precision);
49
50 1
        return sprintf('%.' . $precision . 'f %s', $value, $format['units'][$key]);
51
    }
52
}
53