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.

FrameReader::__invoke()   B
last analyzed

Complexity

Conditions 10
Paths 8

Size

Total Lines 74
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 10.001

Importance

Changes 0
Metric Value
cc 10
eloc 49
nc 8
nop 2
dl 0
loc 74
ccs 43
cts 44
cp 0.9773
crap 10.001
rs 7.246
c 0
b 0
f 0

How to fix   Long Method    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\AMQP\Transport\Connection;
5
6
use Innmind\AMQP\{
7
    Transport\Protocol,
8
    Transport\Frame,
9
    Transport\Frame\Method,
10
    Transport\Frame\Type,
11
    Transport\Frame\Channel,
12
    Transport\Frame\Value\UnsignedOctet,
13
    Transport\Frame\Value\UnsignedShortInteger,
14
    Transport\Frame\Value\UnsignedLongInteger,
15
    Exception\ReceivedFrameNotDelimitedCorrectly,
16
    Exception\PayloadTooShort,
17
    Exception\UnknownFrameType,
18
    Exception\NoFrameDetected,
19
    Exception\LogicException,
20
};
21
use Innmind\Stream\Readable;
22
use Innmind\Filesystem\Stream\StringStream;
23
24
final class FrameReader
25
{
26 104
    public function __invoke(Readable $stream, Protocol $protocol): Frame
27
    {
28 104
        $octet = UnsignedOctet::fromStream($stream);
29
30
        try {
31 104
            $type = Type::fromInt($octet->original()->value());
32 4
        } catch (UnknownFrameType $e) {
33 4
            throw new NoFrameDetected(new StringStream(
34 4
                (string) $stream->read()->prepend((string) $octet)
35
            ));
36
        }
37
38 102
        $channel = new Channel(
39 102
            UnsignedShortInteger::fromStream($stream)->original()->value()
40
        );
41
        $payload = $stream
42 102
            ->read(UnsignedLongInteger::fromStream($stream)->original()->value())
43 102
            ->toEncoding('ASCII');
44
45
        if (
46
            (
47 102
                $type === Type::method() ||
48 102
                $type === Type::header()
49
            ) &&
50 102
            $payload->length() < 4
51
        ) {
52 2
            throw new PayloadTooShort;
53
        }
54
55 100
        $end = UnsignedOctet::fromStream($stream)->original()->value();
56
57 100
        if ($end !== Frame::end()) {
58 2
            throw new ReceivedFrameNotDelimitedCorrectly;
59
        }
60
61 98
        $payload = new StringStream((string) $payload);
62
63
        switch ($type) {
64 98
            case Type::method():
65 92
                $method = new Method(
66 92
                    UnsignedShortInteger::fromStream($payload)
67 92
                        ->original()
68 92
                        ->value(),
69 92
                    UnsignedShortInteger::fromStream($payload)
70 92
                        ->original()
71 92
                        ->value()
72
                );
73
74 92
                return Frame::method(
75 92
                    $channel,
76 46
                    $method,
77 92
                    ...$protocol->read($method, $payload)
78
                );
79
80 38
            case Type::header():
81 34
                $class = UnsignedShortInteger::fromStream($payload)
82 34
                    ->original()
83 34
                    ->value();
84 34
                $payload->read(2); // walk over the weight definition
85
86 34
                return Frame::header(
87 34
                    $channel,
88 17
                    $class,
89 34
                    ...$protocol->readHeader($payload)
90
                );
91
92 36
            case Type::body():
93 34
                return Frame::body($channel, $payload->read());
94
95 2
            case Type::heartbeat():
96 2
                return Frame::heartbeat();
97
98
            default:
99
                throw new LogicException; //if reached then there's an implementation error
100
        }
101
    }
102
}
103