Passed
Push — master ( 540fca...898f33 )
by Chris
02:35
created

PacketDecoder::decodePacket()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 1
dl 0
loc 26
ccs 0
cts 18
cp 0
crap 12
rs 8.8571
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace DaveRandom\LibLifxLan\Decoding;
4
5
use DaveRandom\LibLifxLan\Decoding\Exceptions\DecodingException;
6
use DaveRandom\LibLifxLan\Decoding\Exceptions\InsufficientDataException;
7
use DaveRandom\LibLifxLan\Header\Header;
8
use DaveRandom\LibLifxLan\Packet;
9
10
final class PacketDecoder
11
{
12
    private const HEADER_OFFSET = 0;
13
    private const MESSAGE_OFFSET = Header::WIRE_SIZE;
14
15
    private $headerDecoder;
16
    private $messageDecoder;
17
18
    public function __construct(HeaderDecoder $headerDecoder = null, MessageDecoder $messageDecoder = null)
19
    {
20
        $this->headerDecoder = $headerDecoder ?? new HeaderDecoder;
21
        $this->messageDecoder = $messageDecoder ?? new MessageDecoder;
22
    }
23
24
    /**
25
     * @param string $buffer
26
     * @return Packet
27
     * @throws DecodingException
28
     */
29
    public function decodePacket(string $buffer): Packet
30
    {
31
        $dataLength = \strlen($buffer);
32
33
        if ($dataLength < Header::WIRE_SIZE) {
34
            throw new InsufficientDataException(
35
                "Data length {$dataLength} less than minimum packet size " . Header::WIRE_SIZE
36
            );
37
        }
38
39
        $length = \unpack('vlength', $buffer)['length'];
0 ignored issues
show
Bug introduced by
The call to unpack() has too few arguments starting with offset. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        $length = /** @scrutinizer ignore-call */ \unpack('vlength', $buffer)['length'];

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
40
41
        if ($length !== $dataLength) {
42
            throw new InsufficientDataException(
43
                "Packet length is stated to be {$length} bytes, buffer is {$dataLength} bytes"
44
            );
45
        }
46
47
        $header = $this->headerDecoder->decodeHeader($buffer, self::HEADER_OFFSET);
48
        $payload = $this->messageDecoder->decodeMessage(
49
            $header->getProtocolHeader()->getType(),
50
            $buffer,
51
            self::MESSAGE_OFFSET
52
        );
53
54
        return new Packet($header, $payload);
55
    }
56
}
57