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.

HumanFilesize::humanFilesize()   A
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 24
rs 9.3888
1
<?php
2
3
namespace ProtoneMedia\LaravelMixins\String;
4
5
class HumanFilesize
6
{
7
    /**
8
     * Returns a function containing the macro.
9
     *
10
     * @return callable
11
     */
12
    public function humanFilesize(): callable
13
    {
14
        /**
15
         * Formats the $value into a human readable filesize.
16
         */
17
        return function ($value, $precision = 1): string {
18
            if ($value >= 1000000000000) {
19
                $value = round($value / (1024 * 1024 * 1024 * 1024), $precision);
20
                $unit  = 'TB';
21
            } elseif ($value >= 1000000000) {
22
                $value = round($value / (1024 * 1024 * 1024), $precision);
23
                $unit  = 'GB';
24
            } elseif ($value >= 1000000) {
25
                $value = round($value / (1024 * 1024), $precision);
26
                $unit  = 'MB';
27
            } elseif ($value >= 1000) {
28
                $value = round($value / (1024), $precision);
29
                $unit  = 'KB';
30
            } else {
31
                $unit = 'Bytes';
32
                return number_format($value) . ' ' . $unit;
33
            }
34
35
            return number_format($value, $precision) . ' ' . $unit;
36
        };
37
    }
38
}
39