Passed
Push — master ( c1f248...257530 )
by Camilo
01:51
created

ReadableContent::performSpecialActions()   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
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT\Internals;
6
7
use unreal4u\MQTT\Client;
8
use unreal4u\MQTT\Exceptions\InvalidResponseType;
9
10
trait ReadableContent
11
{
12
    /**
13
     * Length of variable header
14
     * @var int
15
     */
16
    protected $variableHeaderSize = 0;
17
18 2
    final public function populate(string $rawMQTTHeaders): ReadableContentInterface
19
    {
20
        //var_dump(base64_encode($rawMQTTHeaders)); // For now: make it a bit easier to create unit tests
21
        $this
22 2
            ->checkControlPacketValue(\ord($rawMQTTHeaders[0]) >> 4)
23 2
            ->fillObject($rawMQTTHeaders);
24
25 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...
26
    }
27
28 2
    final public function checkControlPacketValue(int $controlPacketValue): ReadableContentInterface
29
    {
30
        // Check whether the first byte corresponds to the expected control packet value
31 2
        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...
32
            throw new InvalidResponseType(sprintf(
33
                'Value of received value does not correspond to response (Expected: %d, Actual: %d)',
34
                static::CONTROL_PACKET_VALUE,
35
                $controlPacketValue
36
            ));
37
        }
38
39 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...
40
    }
41
42
    /**
43
     * Extracts the packet identifier from the raw headers
44
     *
45
     * @param string $rawMQTTHeaders
46
     * @return int
47
     */
48 1
    private function extractPacketIdentifier(string $rawMQTTHeaders): int
49
    {
50
        // Fastest conversion? Turn the bytes around instead of trying to arm a number and passing it along
51 1
        return \ord($rawMQTTHeaders{3} . $rawMQTTHeaders{2});
52
    }
53
54
    /**
55
     * Returns the number of bytes we'll have to read out from
56
     * @return int
57
     */
58
    final public function readVariableHeader(): int
59
    {
60
        // TODO
61
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return integer. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
62
63
    /**
64
     * Any class can overwrite the default behaviour
65
     * @param string $rawMQTTHeaders
66
     * @return ReadableContentInterface
67
     */
68 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

68
    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...
69
    {
70 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...
71
    }
72
73
    /**
74
     * Any class can overwrite the default behaviour
75
     * @param Client $client
76
     * @param WritableContentInterface $originalRequest
77
     * @return bool
78
     */
79
    public function performSpecialActions(Client $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

79
    public function performSpecialActions(/** @scrutinizer ignore-unused */ Client $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

79
    public function performSpecialActions(Client $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...
80
    {
81
        return false;
82
    }
83
}
84