|
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
|
|
|
}; |
|
20
|
|
|
use Innmind\Stream\Readable; |
|
21
|
|
|
|
|
22
|
|
|
final class FrameReader |
|
23
|
|
|
{ |
|
24
|
22 |
|
public function __invoke(Readable $stream, Protocol $protocol): Frame |
|
25
|
|
|
{ |
|
26
|
22 |
|
$octet = $stream->read(1); |
|
27
|
|
|
|
|
28
|
|
|
try { |
|
29
|
22 |
|
$type = Type::fromInt( |
|
|
|
|
|
|
30
|
22 |
|
UnsignedOctet::fromString($octet)->original()->value() |
|
31
|
|
|
); |
|
32
|
2 |
|
} catch (UnknownFrameType $e) { |
|
33
|
2 |
|
throw new NoFrameDetected($octet->append((string) $stream->read())); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
21 |
|
$channel = new Channel( |
|
37
|
21 |
|
UnsignedShortInteger::fromString($stream->read(2))->original()->value() |
|
38
|
|
|
); |
|
39
|
|
|
$payload = $stream |
|
40
|
21 |
|
->read(UnsignedLongInteger::fromString($stream->read(4))->original()->value()) |
|
41
|
21 |
|
->toEncoding('ASCII'); |
|
42
|
|
|
|
|
43
|
21 |
|
if ($payload->length() < 4) { |
|
44
|
1 |
|
throw new PayloadTooShort; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
20 |
|
$end = $stream->read(1)->toEncoding('ASCII'); |
|
48
|
|
|
|
|
49
|
20 |
|
if ($end->length() !== 1) { |
|
50
|
2 |
|
throw new ReceivedFrameNotDelimitedCorrectly; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
18 |
|
$end = UnsignedOctet::fromString($end)->original()->value(); |
|
54
|
|
|
|
|
55
|
18 |
|
if ($end !== 0xCE) { |
|
56
|
|
|
throw new ReceivedFrameNotDelimitedCorrectly; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
18 |
|
$method = $payload->substring(0, 4); |
|
60
|
18 |
|
$method = new Method( |
|
61
|
18 |
|
UnsignedShortInteger::fromString($method->substring(0, 2))->original()->value(), |
|
62
|
18 |
|
UnsignedShortInteger::fromString($method->substring(2, 4))->original()->value() |
|
63
|
|
|
); |
|
64
|
18 |
|
$arguments = $payload->substring(4); |
|
65
|
|
|
|
|
66
|
18 |
|
return Frame::command( |
|
67
|
18 |
|
$channel, |
|
68
|
18 |
|
$method, |
|
69
|
18 |
|
...$protocol->read($method, $arguments) |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.