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 ( 778995...e0837d )
by Baptiste
03:36
created

Bits::fromStream()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 1
dl 0
loc 24
ccs 17
cts 17
cp 1
crap 1
rs 9.7
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\AMQP\Transport\Frame\Value;
5
6
use Innmind\AMQP\Transport\Frame\Value;
7
use Innmind\Stream\Readable;
8
use Innmind\Immutable\{
9
    StreamInterface,
10
    Stream,
11
    Str,
12
    Sequence as Seq,
13
};
14
15
final class Bits implements Value
16
{
17
    private $value;
18
    private $original;
19
20 184
    public function __construct(bool $first, bool ...$bits)
21
    {
22 184
        $this->original = Stream::of('bool', $first, ...$bits);
23 184
    }
24
25 116
    public static function fromStream(Readable $stream): Value
26
    {
27 116
        return new self(
28
            ...$stream
29 116
                ->read(1)
30 116
                ->toEncoding('ASCII')
31 116
                ->chunk()
32 116
                ->reduce(
33 116
                    new Seq,
34
                    static function(Seq $bits, Str $bit): Seq {
35 116
                        return (new Str(\decbin(\ord((string) $bit))))
36 116
                            ->chunk()
37 116
                            ->reduce(
38 116
                                $bits,
39
                                static function(Seq $bits, Str $bit): Seq {
40 116
                                    return $bits->add((int) (string) $bit);
41 116
                                }
42
                            );
43 116
                    }
44
                )
45
                ->map(static function(int $bit): bool {
46 116
                    return (bool) $bit;
47 116
                })
48 116
                ->reverse()
49
        );
50
    }
51
52
    /**
53
     * @return StreamInterface<bool>
54
     */
55 88
    public function original(): StreamInterface
56
    {
57 88
        return $this->original;
58
    }
59
60 182
    public function __toString(): string
61
    {
62 182
        if (\is_null($this->value)) {
63 182
            $value = 0;
64
65 182
            foreach ($this->original as $i => $bit) {
66 182
                $bit = (int) $bit;
67 182
                $value |= $bit << $i;
68
            }
69
70 182
            $this->value = \chr($value);
71
        }
72
73 182
        return $this->value;
74
    }
75
}
76