ParsesNumValues   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A toBytes() 0 20 4
A fromBytes() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Longman\LaravelLodash\SelfDiagnosis;
6
7
use function floor;
8
use function pow;
9
use function sprintf;
10
use function strlen;
11
use function strtolower;
12
use function trim;
13
14
trait ParsesNumValues
15
{
16
    private function toBytes(string $value): int
17
    {
18
        $value = trim($value);
19
        $last = strtolower($value[strlen($value) - 1]);
20
21
        $value = (int) $value;
22
        switch ($last) {
23
            case 'g':
24
                $value *= 1024;
25
            // no break
26
            case 'm':
27
                $value *= 1024;
28
            // no break
29
            case 'k':
30
                $value *= 1024;
31
            // no break
32
        }
33
34
        return $value;
35
    }
36
37
    private function fromBytes(int $bytes, int $decimals = 2): string
38
    {
39
        $size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
40
        $factor = (int) floor((strlen((string) $bytes) - 1) / 3);
41
42
        return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . $size[$factor];
43
    }
44
}
45