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 — develop ( d433ef...8d7806 )
by Baptiste
02:53
created

Bytes::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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