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.
Completed
Push — develop ( 78c84d...9bdc70 )
by Baptiste
04:48
created

Bytes::fromUnit()   C

Complexity

Conditions 14
Paths 14

Size

Total Lines 45
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 14

Importance

Changes 0
Metric Value
dl 0
loc 45
c 0
b 0
f 0
ccs 31
cts 31
cp 1
rs 5.0864
cc 14
eloc 32
nc 14
nop 2
crap 14

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Server\Status\Server\Memory;
5
6
use Innmind\Server\Status\Exception\{
7
    BytesCannotBeNegative,
8
    UnknownBytesFormat
9
};
10
use Innmind\Immutable\Str;
11
12
final class Bytes
13
{
14
    public const BYTES = 1024;
15
    public const KILOBYTES = 1024 ** 2;
16
    public const MEGABYTES = 1024 ** 3;
17
    public const GIGABYTES = 1024 ** 4;
18
    public const TERABYTES = 1024 ** 5;
19
    public const PETABYTES = 1024 ** 6;
20
21
    private $value;
22
    private $string;
23
24 35
    public function __construct(int $value)
25
    {
26 35
        if ($value < 0) {
27 1
            throw new BytesCannotBeNegative;
28
        }
29
30 34
        $this->value = $value;
31
32
        switch (true) {
33 34
            case $value < self::BYTES:
34 14
                $this->string = $value.'B';
35 14
                break;
36
37 24
            case $value < self::KILOBYTES:
38 4
                $this->string = sprintf(
39 4
                    '%sKB',
40 4
                    round($value/self::BYTES, 3)
41
                );
42 4
                break;
43
44 20
            case $value < self::MEGABYTES:
45 8
                $this->string = sprintf(
46 8
                    '%sMB',
47 8
                    round($value/self::KILOBYTES, 3)
48
                );
49 8
                break;
50
51 16
            case $value < self::GIGABYTES:
52 8
                $this->string = sprintf(
53 8
                    '%sGB',
54 8
                    round($value/self::MEGABYTES, 3)
55
                );
56 8
                break;
57
58 8
            case $value < self::TERABYTES:
59 4
                $this->string = sprintf(
60 4
                    '%sTB',
61 4
                    round($value/self::GIGABYTES, 3)
62
                );
63 4
                break;
64
65 4
            case $value < self::PETABYTES:
66 4
                $this->string = sprintf(
67 4
                    '%sPB',
68 4
                    round($value/self::TERABYTES, 3)
69
                );
70 4
                break;
71
        }
72 34
    }
73
74 13
    public function toInt(): int
75
    {
76 13
        return $this->value;
77
    }
78
79 25
    public function __toString(): string
80
    {
81 25
        return $this->string;
82
    }
83
84 19
    public static function fromString(string $bytes): self
85
    {
86 19
        if ($bytes === (string) (int) $bytes) {
87 3
            return new self((int) $bytes);
88
        }
89
90 18
        $bytes = new Str($bytes);
91
92 18
        if ($bytes->length() < 2) {
93 2
            throw new UnknownBytesFormat;
94
        }
95
96
        try {
97 16
            return self::fromUnit(
98 16
                $bytes->substring(0, -1),
99 16
                $bytes->substring(-1)
100
            );
101 8
        } catch (UnknownBytesFormat $e) {
102 8
            return self::fromUnit(
103 8
                $bytes->substring(0, -2),
104 8
                $bytes->substring(-2)
105
            );
106
        }
107
    }
108
109 16
    private static function fromUnit(Str $bytes, Str $unit): self
110
    {
111 16
        if ($bytes->length() === 0) {
112 1
            throw new UnknownBytesFormat;
113
        }
114
115 16
        switch ((string) $unit) {
116 16
            case 'B':
117 15
            case 'Bi':
118 2
                $multiplier = 1;
119 2
                break;
120
121 15
            case 'K':
122 14
            case 'Ki':
123 2
                $multiplier = Bytes::BYTES;
124 2
                break;
125
126 14
            case 'M':
127 13
            case 'Mi':
128 4
                $multiplier = Bytes::KILOBYTES;
129 4
                break;
130
131 13
            case 'G':
132 10
            case 'Gi':
133 4
                $multiplier = Bytes::MEGABYTES;
134 4
                break;
135
136 10
            case 'T':
137 9
            case 'Ti':
138 2
                $multiplier = Bytes::GIGABYTES;
139 2
                break;
140
141 9
            case 'P':
142 8
            case 'Pi':
143 2
                $multiplier = Bytes::TERABYTES;
144 2
                break;
145
146
            default:
147 8
                throw new UnknownBytesFormat;
148
        }
149
150 14
        return new self(
151 14
            (int) (((float) (string) $bytes) * $multiplier)
152
        );
153
    }
154
}
155