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
Pull Request — develop (#1)
by Baptiste
01:41
created

Table::__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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\{
7
    Transport\Frame\Value,
8
    Exception\UnboundedTextCannotBeWrapped,
9
};
10
use Innmind\Math\Algebra\Integer;
11
use Innmind\Stream\Readable;
12
use Innmind\Immutable\{
13
    Str,
14
    Sequence as Seq,
15
    MapInterface,
16
    Map,
17
};
18
19
final class Table implements Value
20
{
21
    private $value;
22
    private $original;
23
24
    /**
25
     * @param MapInterface<string, Value> $map
26
     */
27 66
    public function __construct(MapInterface $map)
28
    {
29
        if (
30 66
            (string) $map->keyType() !== 'string' ||
31 66
            (string) $map->valueType() !== Value::class
32
        ) {
33 1
            throw new \TypeError(sprintf(
34 1
                'Argument 1 must be of type MapInterface<string, %s>',
35 1
                Value::class
36
            ));
37
        }
38
39 65
        $texts = $map->filter(static function(string $key, Value $value): bool {
40 60
            return $value instanceof Text;
41 65
        });
42
43 65
        if ($texts->size() > 0) {
44 1
            throw new UnboundedTextCannotBeWrapped;
45
        }
46
47
        $data = $map
48 64
            ->reduce(
49 64
                new Seq,
50 64
                static function(Seq $sequence, string $key, Value $value): Seq {
51
                    return $sequence
52 59
                        ->add(new ShortString(new Str($key)))
53 59
                        ->add(Symbols::symbol(get_class($value)))
54 59
                        ->add($value);
55 64
                }
56
            )
57 64
            ->join('')
58 64
            ->toEncoding('ASCII');
59
60 64
        $this->value = (string) new UnsignedLongInteger(
61 64
            new Integer($data->length())
62
        );
63 64
        $this->value .= $data;
64 64
        $this->original = $map;
65 64
    }
66
67 52
    public static function fromStream(Readable $stream): Value
68
    {
69 52
        $length = UnsignedLongInteger::fromStream($stream)->original();
70 52
        $position = $stream->position()->toInt();
71 52
        $boundary = $position + $length->value();
72
73 52
        $map = new Map('string', Value::class);
74
75 52
        while ($position < $boundary) {
76 49
            $key = ShortString::fromStream($stream)->original();
77 49
            $class = Symbols::class((string) $stream->read(1));
78
79 49
            $map = $map->put((string) $key, [$class, 'fromStream']($stream));
80
81 49
            $position = $stream->position()->toInt();
82
        }
83
84 52
        return new self($map);
85
    }
86
87
    /**
88
     * @return MapInterface<string, Value>
89
     */
90 13
    public function original(): MapInterface
91
    {
92 13
        return $this->original;
93
    }
94
95 63
    public function __toString(): string
96
    {
97 63
        return $this->value;
98
    }
99
}
100