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 ( a71c17...924269 )
by Baptiste
02:08
created

MessageReader::__invoke()   F

Complexity

Conditions 16
Paths 16384

Size

Total Lines 140
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 41.756

Importance

Changes 0
Metric Value
dl 0
loc 140
c 0
b 0
f 0
ccs 23
cts 43
cp 0.5349
rs 2
cc 16
eloc 90
nc 16384
nop 1
crap 41.756

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
    Model\Basic\Message,
8
    Model\Basic\Message\Generic,
9
    Model\Basic\Message\ContentType,
10
    Model\Basic\Message\ContentEncoding,
11
    Model\Basic\Message\DeliveryMode,
12
    Model\Basic\Message\Priority,
13
    Model\Basic\Message\CorrelationId,
14
    Model\Basic\Message\ReplyTo,
15
    Model\Basic\Message\Id,
16
    Model\Basic\Message\Type,
17
    Model\Basic\Message\UserId,
18
    Model\Basic\Message\AppId,
19
    Transport\Connection,
20
    Transport\Frame\Value
21
};
22
use Innmind\TimeContinuum\ElapsedPeriod;
23
use Innmind\Immutable\{
24
    Map,
25
    Str
26
};
27
28
final class MessageReader
29
{
30 12
    public function __invoke(Connection $connection): Message
31
    {
32 12
        $header = $connection->wait();
33
        $bodySize = $header
34 12
            ->values()
35 12
            ->first()
36 12
            ->original()
37 12
            ->value();
38
        $flagBits = $header
39 12
            ->values()
40 12
            ->get(1)
41 12
            ->original()
42 12
            ->value();
43 12
        $payload = new Str('');
44
45 12
        while ($payload->length() !== $bodySize) {
46 12
            $payload = $payload->append(
47
                (string) $connection
48 12
                    ->wait()
49 12
                    ->values()
50 12
                    ->first()
51 12
                    ->original()
52
            );
53
        }
54
55 12
        $message = new Generic($payload);
56
        $properties = $header
57 12
            ->values()
58 12
            ->drop(2);
59
60 12
        if ($flagBits & (1 << 15)) {
61
            [$topLevel, $subType] = explode(
2 ignored issues
show
Bug introduced by
The variable $topLevel does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $subType does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
62
                '/',
63
                (string) $properties->first()->original()
64
            );
65
            $message = $message->withContentType(new ContentType(
66
                $topLevel,
67
                $subType
68
            ));
69
            $properties = $properties->drop(1);
70
        }
71
72 12
        if ($flagBits & (1 << 14)) {
73
            $message = $message->withContentEncoding(new ContentEncoding(
74
                (string) $properties->first()->original()
75
            ));
76
            $properties = $properties->drop(1);
77
        }
78
79 12
        if ($flagBits & (1 << 13)) {
80
            $message = $message->withHeaders(
81
                $properties
82
                    ->first()
83
                    ->original()
84
                    ->reduce(
85
                        new Map('string', 'mixed'),
86
                        static function(Map $carry, string $key, Value $value): Map {
87
                            return $carry->put(
88
                                $key,
89
                                $value->original()
90
                            );
91
                        }
92
                    )
93
            );
94
            $properties = $properties->drop(1);
95
        }
96
97
        if ($flagBits & (1 << 12)) {
98
            $message = $message->withDeliveryMode(
99
                $properties->first()->original()->value() === DeliveryMode::persistent()->toInt() ?
100
                    DeliveryMode::persistent() : DeliveryMode::nonPersistent()
101
            );
102
            $properties = $properties->drop(1);
103
        }
104
105
        if ($flagBits & (1 << 11)) {
106
            $message = $message->withPriority(new Priority(
107
                $properties->first()->original()->value()
108
            ));
109
            $properties = $properties->drop(1);
110
        }
111
112
        if ($flagBits & (1 << 10)) {
113
            $message = $message->withCorrelationId(new CorrelationId(
114
                (string) $properties->first()->original()
115
            ));
116
            $properties = $properties->drop(1);
117
        }
118
119
        if ($flagBits & (1 << 9)) {
120
            $message = $message->withReplyTo(new ReplyTo(
121
                (string) $properties->first()->original()
122
            ));
123
            $properties = $properties->drop(1);
124
        }
125
126
        if ($flagBits & (1 << 8)) {
127
            $message = $message->withExpiration(new ElapsedPeriod(
128
                (int) (string) $properties->first()->original()
129
            ));
130
            $properties = $properties->drop(1);
131
        }
132
133
        if ($flagBits & (1 << 7)) {
134
            $message = $message->withId(new Id(
135
                (string) $properties->first()->original()
136
            ));
137
            $properties = $properties->drop(1);
138
        }
139
140
        if ($flagBits & (1 << 6)) {
141
            $message = $message->withTimestamp(
142
                $properties->first()->original()
143
            );
144
            $properties = $properties->drop(1);
145
        }
146
147
        if ($flagBits & (1 << 5)) {
148
            $message = $message->withType(new Type(
149
                (string) $properties->first()->original()
150
            ));
151
            $properties = $properties->drop(1);
152
        }
153
154
        if ($flagBits & (1 << 4)) {
155
            $message = $message->withType(new UserId(
0 ignored issues
show
Documentation introduced by
new \Innmind\AMQP\Model\...s->first()->original()) is of type object<Innmind\AMQP\Model\Basic\Message\UserId>, but the function expects a object<Innmind\AMQP\Model\Basic\Message\Type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
156
                (string) $properties->first()->original()
157
            ));
158
            $properties = $properties->drop(1);
159
        }
160
161
        if ($flagBits & (1 << 3)) {
162
            $message = $message->withType(new AppId(
0 ignored issues
show
Documentation introduced by
new \Innmind\AMQP\Model\...s->first()->original()) is of type object<Innmind\AMQP\Model\Basic\Message\AppId>, but the function expects a object<Innmind\AMQP\Model\Basic\Message\Type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
163
                (string) $properties->first()->original()
164
            ));
165
            $properties = $properties->drop(1);
0 ignored issues
show
Unused Code introduced by
$properties is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
166
        }
167
168
        return $message;
169
    }
170
}
171