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.
Completed
Push — develop ( ee213a...13fd92 )
by Baptiste
01:48
created

FrameReader::__invoke()   C

Complexity

Conditions 11
Paths 9

Size

Total Lines 82
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 46
CRAP Score 11.0086

Importance

Changes 2
Bugs 1 Features 1
Metric Value
dl 0
loc 82
ccs 46
cts 48
cp 0.9583
rs 5.2653
c 2
b 1
f 1
cc 11
eloc 54
nc 9
nop 2
crap 11.0086

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
23
final class FrameReader
24
{
25 25
    public function __invoke(Readable $stream, Protocol $protocol): Frame
26
    {
27 25
        $octet = $stream->read(1);
28
29
        try {
30 25
            $type = Type::fromInt(
31 25
                UnsignedOctet::fromString($octet)->original()->value()
32
            );
33 2
        } catch (UnknownFrameType $e) {
34 2
            throw new NoFrameDetected($octet->append((string) $stream->read()));
35
        }
36
37 24
        $channel = new Channel(
38 24
            UnsignedShortInteger::fromString($stream->read(2))->original()->value()
39
        );
40
        $payload = $stream
41 24
            ->read(UnsignedLongInteger::fromString($stream->read(4))->original()->value())
42 24
            ->toEncoding('ASCII');
43
44
        if (
45
            (
46 24
                $type === Type::method() ||
47 24
                $type === Type::header()
48
            ) &&
49 24
            $payload->length() < 4
50
        ) {
51 1
            throw new PayloadTooShort;
52
        }
53
54 23
        $end = $stream->read(1)->toEncoding('ASCII');
55
56 23
        if ($end->length() !== 1) {
57 2
            throw new ReceivedFrameNotDelimitedCorrectly;
58
        }
59
60 21
        $end = UnsignedOctet::fromString($end)->original()->value();
61
62 21
        if ($end !== 0xCE) {
63
            throw new ReceivedFrameNotDelimitedCorrectly;
64
        }
65
66
        switch ($type) {
67 21
            case Type::method():
68 18
                $method = $payload->substring(0, 4);
69 18
                $method = new Method(
70 18
                    UnsignedShortInteger::fromString($method->substring(0, 2))
71 18
                        ->original()
72 18
                        ->value(),
73 18
                    UnsignedShortInteger::fromString($method->substring(2, 4))
74 18
                        ->original()
75 18
                        ->value()
76
                );
77 18
                $arguments = $payload->substring(4);
78
79 18
                return Frame::command(
80 18
                    $channel,
81 18
                    $method,
82 18
                    ...$protocol->read($method, $arguments)
83
                );
84
85 3
            case Type::header():
86 1
                $header = $payload->substring(0, 4); //3 and 4 are the weight
87 1
                $class = UnsignedShortInteger::fromString($header->substring(0, 2))
88 1
                    ->original()
89 1
                    ->value();
90
91 1
                return Frame::header(
92 1
                    $channel,
93 1
                    $class,
94 1
                    ...$protocol->readHeader($payload->substring(4))
95
                );
96
97 2
            case Type::body():
98 1
                return Frame::body($channel, $payload);
99
100 1
            case Type::heartbeat():
101 1
                return Frame::heartbeat();
102
103
            default:
104
                throw new LogicException; //if reached then there's an implementation error
105
        }
106
    }
107
}
108