Passed
Push — master ( fc052b...13a97b )
by Camilo
02:52
created

ReadableContent::readVariableHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT\Internals;
6
7
use unreal4u\MQTT\Exceptions\InvalidResponseType;
8
use unreal4u\MQTT\Utilities;
9
10
/**
11
 * Trait ReadableContent
12
 * @package unreal4u\MQTT\Internals
13
 */
14
trait ReadableContent
15
{
16
    /**
17
     * Length of variable header
18
     * @var int
19
     */
20
    protected $variableHeaderSize = 0;
21
22 3
    final public function populate(string $rawMQTTHeaders): ReadableContentInterface
23
    {
24
        //var_dump(base64_encode($rawMQTTHeaders)); // For now: make it a bit easier to create unit tests
25
        $this
26 3
            ->checkControlPacketValue(\ord($rawMQTTHeaders[0]) >> 4)
27 2
            ->fillObject($rawMQTTHeaders);
28
29 2
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type unreal4u\MQTT\Internals\ReadableContent which is incompatible with the type-hinted return unreal4u\MQTT\Internals\ReadableContentInterface.
Loading history...
30
    }
31
32 3
    final public function checkControlPacketValue(int $controlPacketValue): ReadableContentInterface
33
    {
34
        // Check whether the first byte corresponds to the expected control packet value
35 3
        if (static::CONTROL_PACKET_VALUE !== $controlPacketValue) {
0 ignored issues
show
Bug introduced by
The constant unreal4u\MQTT\Internals\...t::CONTROL_PACKET_VALUE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
36 1
            throw new InvalidResponseType(sprintf(
37 1
                'Value of received value does not correspond to response (Expected: %d, Actual: %d)',
38 1
                static::CONTROL_PACKET_VALUE,
39 1
                $controlPacketValue
40
            ));
41
        }
42
43 2
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type unreal4u\MQTT\Internals\ReadableContent which is incompatible with the type-hinted return unreal4u\MQTT\Internals\ReadableContentInterface.
Loading history...
44
    }
45
46
    /**
47
     * Extracts the packet identifier from the raw headers
48
     *
49
     * @param string $rawMQTTHeaders
50
     * @return int
51
     * @throws \OutOfRangeException
52
     */
53 1
    private function extractPacketIdentifier(string $rawMQTTHeaders): int
54
    {
55 1
        return Utilities::convertBinaryStringToNumber($rawMQTTHeaders{2} . $rawMQTTHeaders{3});
56
    }
57
58
    /**
59
     * Any class can overwrite the default behaviour
60
     * @param string $rawMQTTHeaders
61
     * @return ReadableContentInterface
62
     */
63 1
    public function fillObject(string $rawMQTTHeaders): ReadableContentInterface
0 ignored issues
show
Unused Code introduced by
The parameter $rawMQTTHeaders is not used and could be removed. ( Ignorable by Annotation )

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

63
    public function fillObject(/** @scrutinizer ignore-unused */ string $rawMQTTHeaders): ReadableContentInterface

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    {
65 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type unreal4u\MQTT\Internals\ReadableContent which is incompatible with the type-hinted return unreal4u\MQTT\Internals\ReadableContentInterface.
Loading history...
66
    }
67
68
    /**
69
     * Any class can overwrite the default behaviour
70
     * @param ClientInterface $client
71
     * @param WritableContentInterface $originalRequest
72
     * @return bool
73
     */
74
    public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool
0 ignored issues
show
Unused Code introduced by
The parameter $client is not used and could be removed. ( Ignorable by Annotation )

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

74
    public function performSpecialActions(/** @scrutinizer ignore-unused */ ClientInterface $client, WritableContentInterface $originalRequest): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $originalRequest is not used and could be removed. ( Ignorable by Annotation )

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

74
    public function performSpecialActions(ClientInterface $client, /** @scrutinizer ignore-unused */ WritableContentInterface $originalRequest): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76
        return false;
77
    }
78
}
79