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.
Passed
Push — master ( 4fa160...5892ba )
by Baptiste
02:44 queued 10s
created

Bytes::of()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 21
ccs 13
cts 13
cp 1
rs 9.8333
cc 4
nc 4
nop 1
crap 4
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 105
    public function __construct(int $value)
25
    {
26 105
        if ($value < 0) {
27 3
            throw new BytesCannotBeNegative;
28
        }
29
30 102
        $this->value = $value;
31
32
        switch (true) {
33 102
            case $value < self::BYTES:
34 42
                $this->string = $value.'B';
35 42
                break;
36
37 72
            case $value < self::KILOBYTES:
38 12
                $this->string = sprintf(
39 12
                    '%sKB',
40 12
                    round($value/self::BYTES, 3)
41
                );
42 12
                break;
43
44 60
            case $value < self::MEGABYTES:
45 24
                $this->string = sprintf(
46 24
                    '%sMB',
47 24
                    round($value/self::KILOBYTES, 3)
48
                );
49 24
                break;
50
51 48
            case $value < self::GIGABYTES:
52 24
                $this->string = sprintf(
53 24
                    '%sGB',
54 24
                    round($value/self::MEGABYTES, 3)
55
                );
56 24
                break;
57
58 24
            case $value < self::TERABYTES:
59 12
                $this->string = sprintf(
60 12
                    '%sTB',
61 12
                    round($value/self::GIGABYTES, 3)
62
                );
63 12
                break;
64
65 12
            case $value < self::PETABYTES:
66 12
                $this->string = sprintf(
67 12
                    '%sPB',
68 12
                    round($value/self::TERABYTES, 3)
69
                );
70 12
                break;
71
        }
72 102
    }
73
74 39
    public function toInt(): int
75
    {
76 39
        return $this->value;
77
    }
78
79 75
    public function __toString(): string
80
    {
81 75
        return $this->string;
82
    }
83
84 57
    public static function of(string $bytes): self
85
    {
86 57
        if ($bytes === (string) (int) $bytes) {
87 9
            return new self((int) $bytes);
88
        }
89
90 54
        $bytes = new Str($bytes);
91
92 54
        if ($bytes->length() < 2) {
93 6
            throw new UnknownBytesFormat;
94
        }
95
96
        try {
97 48
            return self::fromUnit(
98 48
                $bytes->substring(0, -1),
99 48
                $bytes->substring(-1)
100
            );
101 24
        } catch (UnknownBytesFormat $e) {
102 24
            return self::fromUnit(
103 24
                $bytes->substring(0, -2),
104 24
                $bytes->substring(-2)
105
            );
106
        }
107
    }
108
109
    /**
110
     * @deprecated
111
     * @see self::of()
112
     */
113 51
    public static function fromString(string $bytes): self
114
    {
115 51
        return self::of($bytes);
116
    }
117
118 48
    private static function fromUnit(Str $bytes, Str $unit): self
119
    {
120 48
        if ($bytes->length() === 0) {
121 3
            throw new UnknownBytesFormat;
122
        }
123
124 48
        switch ((string) $unit) {
125 48
            case 'B':
126 45
            case 'Bi':
127 6
                $multiplier = 1;
128 6
                break;
129
130 45
            case 'K':
131 42
            case 'Ki':
132 6
                $multiplier = Bytes::BYTES;
133 6
                break;
134
135 42
            case 'M':
136 39
            case 'Mi':
137 12
                $multiplier = Bytes::KILOBYTES;
138 12
                break;
139
140 39
            case 'G':
141 30
            case 'Gi':
142 12
                $multiplier = Bytes::MEGABYTES;
143 12
                break;
144
145 30
            case 'T':
146 27
            case 'Ti':
147 6
                $multiplier = Bytes::GIGABYTES;
148 6
                break;
149
150 27
            case 'P':
151 24
            case 'Pi':
152 6
                $multiplier = Bytes::TERABYTES;
153 6
                break;
154
155
            default:
156 24
                throw new UnknownBytesFormat;
157
        }
158
159 42
        return new self(
160 42
            (int) (((float) (string) $bytes) * $multiplier)
161
        );
162
    }
163
}
164