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']; |
|
|
|
|
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
|
|
|
|
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.