Passed
Push — master ( c0886a...23eada )
by Chris
03:14
created

MessageDecoder::decodeMessage()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 2
dl 0
loc 21
ccs 0
cts 18
cp 0
crap 20
rs 9.0534
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace DaveRandom\LibLifxLan\Decoding;
4
5
use DaveRandom\LibLifxLan\DataTypes as DeviceDataTypes;
6
use DaveRandom\LibLifxLan\DataTypes\Light as LightDataTypes;
7
use DaveRandom\LibLifxLan\Decoding\Exceptions\DecodingException;
8
use DaveRandom\LibLifxLan\Decoding\Exceptions\InvalidMessagePayloadLengthException;
9
use DaveRandom\LibLifxLan\Decoding\Exceptions\MalformedMessagePayloadException;
10
use DaveRandom\LibLifxLan\Exceptions\InvalidValueException;
11
use DaveRandom\LibLifxLan\Messages\Device\Commands as DeviceCommands;
12
use DaveRandom\LibLifxLan\Messages\Device\Requests as DeviceRequests;
13
use DaveRandom\LibLifxLan\Messages\Device\Responses as DeviceResponses;
14
use DaveRandom\LibLifxLan\Messages\Light\Commands as LightCommmands;
15
use DaveRandom\LibLifxLan\Messages\Light\Requests as LightRequests;
16
use DaveRandom\LibLifxLan\Messages\Light\Responses as LightResponses;
17
use DaveRandom\LibLifxLan\Messages\Message;
18
use DaveRandom\LibLifxLan\Messages\UnknownMessage;
19
use Ramsey\Uuid\UuidFactory;
20
use Ramsey\Uuid\UuidFactoryInterface;
21
22
final class MessageDecoder
23
{
24
    private const HSBK_FORMAT = 'vhue/vsaturation/vbrightness/vtemperature';
25
26
    /**
27
     * @uses decodeAcknowledgement
28
     * @uses decodeEchoRequest
29
     * @uses decodeEchoResponse
30
     * @uses decodeGetGroup
31
     * @uses decodeSetGroup
32
     * @uses decodeStateGroup
33
     * @uses decodeGetHostFirmware
34
     * @uses decodeStateHostFirmware
35
     * @uses decodeGetHostInfo
36
     * @uses decodeStateHostInfo
37
     * @uses decodeGetInfo
38
     * @uses decodeStateInfo
39
     * @uses decodeGetLabel
40
     * @uses decodeSetLabel
41
     * @uses decodeStateLabel
42
     * @uses decodeGetLocation
43
     * @uses decodeSetLocation
44
     * @uses decodeStateLocation
45
     * @uses decodeGetDevicePower
46
     * @uses decodeSetDevicePower
47
     * @uses decodeStateDevicePower
48
     * @uses decodeGetService
49
     * @uses decodeStateService
50
     * @uses decodeGetVersion
51
     * @uses decodeStateVersion
52
     * @uses decodeGetWifiFirmware
53
     * @uses decodeStateWifiFirmware
54
     * @uses decodeGetWifiInfo
55
     * @uses decodeStateWifiInfo
56
     * @uses decodeGet
57
     * @uses decodeSetColor
58
     * @uses decodeSetWaveform
59
     * @uses decodeSetWaveformOptional
60
     * @uses decodeState
61
     * @uses decodeGetInfrared
62
     * @uses decodeSetInfrared
63
     * @uses decodeStateInfrared
64
     * @uses decodeGetLightPower
65
     * @uses decodeSetLightPower
66
     * @uses decodeStateLightPower
67
     */
68
    private const MESSAGE_INFO = [
69
        // Device command messages
70
        DeviceCommands\SetGroup::MESSAGE_TYPE_ID => [DeviceCommands\SetGroup::WIRE_SIZE, 'SetGroup'],
71
        DeviceCommands\SetLabel::MESSAGE_TYPE_ID => [DeviceCommands\SetLabel::WIRE_SIZE, 'SetLabel'],
72
        DeviceCommands\SetLocation::MESSAGE_TYPE_ID => [DeviceCommands\SetLocation::WIRE_SIZE, 'SetLocation'],
73
        DeviceCommands\SetPower::MESSAGE_TYPE_ID => [DeviceCommands\SetPower::WIRE_SIZE, 'SetDevicePower'],
74
75
        // Device request messages
76
        DeviceRequests\EchoRequest::MESSAGE_TYPE_ID => [DeviceRequests\EchoRequest::WIRE_SIZE, 'EchoRequest'],
77
        DeviceRequests\GetGroup::MESSAGE_TYPE_ID => [DeviceRequests\GetGroup::WIRE_SIZE, 'GetGroup'],
78
        DeviceRequests\GetHostFirmware::MESSAGE_TYPE_ID => [DeviceRequests\GetHostFirmware::WIRE_SIZE, 'GetHostFirmware'],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
79
        DeviceRequests\GetHostInfo::MESSAGE_TYPE_ID => [DeviceRequests\GetHostInfo::WIRE_SIZE, 'GetHostInfo'],
80
        DeviceRequests\GetInfo::MESSAGE_TYPE_ID => [DeviceRequests\GetInfo::WIRE_SIZE, 'GetInfo'],
81
        DeviceRequests\GetLabel::MESSAGE_TYPE_ID => [DeviceRequests\GetLabel::WIRE_SIZE, 'GetLabel'],
82
        DeviceRequests\GetLocation::MESSAGE_TYPE_ID => [DeviceRequests\GetLocation::WIRE_SIZE, 'GetLocation'],
83
        DeviceRequests\GetPower::MESSAGE_TYPE_ID => [DeviceRequests\GetPower::WIRE_SIZE, 'GetDevicePower'],
84
        DeviceRequests\GetService::MESSAGE_TYPE_ID => [DeviceRequests\GetService::WIRE_SIZE, 'GetService'],
85
        DeviceRequests\GetVersion::MESSAGE_TYPE_ID => [DeviceRequests\GetVersion::WIRE_SIZE, 'GetVersion'],
86
        DeviceRequests\GetWifiFirmware::MESSAGE_TYPE_ID => [DeviceRequests\GetWifiFirmware::WIRE_SIZE, 'GetWifiFirmware'],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
87
        DeviceRequests\GetWifiInfo::MESSAGE_TYPE_ID => [DeviceRequests\GetWifiInfo::WIRE_SIZE, 'GetWifiInfo'],
88
89
        // Device response messages
90
        DeviceResponses\Acknowledgement::MESSAGE_TYPE_ID => [DeviceResponses\Acknowledgement::WIRE_SIZE, 'Acknowledgement'],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
91
        DeviceResponses\EchoResponse::MESSAGE_TYPE_ID => [DeviceResponses\EchoResponse::WIRE_SIZE, 'EchoResponse'],
92
        DeviceResponses\StateGroup::MESSAGE_TYPE_ID => [DeviceResponses\StateGroup::WIRE_SIZE, 'StateGroup'],
93
        DeviceResponses\StateHostFirmware::MESSAGE_TYPE_ID => [DeviceResponses\StateHostFirmware::WIRE_SIZE, 'StateHostFirmware'],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
94
        DeviceResponses\StateHostInfo::MESSAGE_TYPE_ID => [DeviceResponses\StateHostInfo::WIRE_SIZE, 'StateHostInfo'],
95
        DeviceResponses\StateInfo::MESSAGE_TYPE_ID => [DeviceResponses\StateInfo::WIRE_SIZE, 'StateInfo'],
96
        DeviceResponses\StateLabel::MESSAGE_TYPE_ID => [DeviceResponses\StateLabel::WIRE_SIZE, 'StateLabel'],
97
        DeviceResponses\StateLocation::MESSAGE_TYPE_ID => [DeviceResponses\StateLocation::WIRE_SIZE, 'StateLocation'],
98
        DeviceResponses\StatePower::MESSAGE_TYPE_ID => [DeviceResponses\StatePower::WIRE_SIZE, 'StateDevicePower'],
99
        DeviceResponses\StateService::MESSAGE_TYPE_ID => [DeviceResponses\StateService::WIRE_SIZE, 'StateService'],
100
        DeviceResponses\StateVersion::MESSAGE_TYPE_ID => [DeviceResponses\StateVersion::WIRE_SIZE, 'StateVersion'],
101
        DeviceResponses\StateWifiFirmware::MESSAGE_TYPE_ID => [DeviceResponses\StateWifiFirmware::WIRE_SIZE, 'StateWifiFirmware'],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
102
        DeviceResponses\StateWifiInfo::MESSAGE_TYPE_ID => [DeviceResponses\StateWifiInfo::WIRE_SIZE, 'StateWifiInfo'],
103
104
        // Light command messages
105
        LightCommmands\SetColor::MESSAGE_TYPE_ID => [LightCommmands\SetColor::WIRE_SIZE, 'SetColor'],
106
        LightCommmands\SetInfrared::MESSAGE_TYPE_ID => [LightCommmands\SetInfrared::WIRE_SIZE, 'SetInfrared'],
107
        LightCommmands\SetPower::MESSAGE_TYPE_ID => [LightCommmands\SetPower::WIRE_SIZE, 'SetLightPower'],
108
        LightCommmands\SetWaveform::MESSAGE_TYPE_ID => [LightCommmands\SetWaveform::WIRE_SIZE, 'SetWaveform'],
109
        LightCommmands\SetWaveformOptional::MESSAGE_TYPE_ID => [LightCommmands\SetWaveformOptional::WIRE_SIZE, 'SetWaveformOptional'],
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
110
111
        // Light request messages
112
        LightRequests\Get::MESSAGE_TYPE_ID => [LightRequests\Get::WIRE_SIZE, 'Get'],
113
        LightRequests\GetInfrared::MESSAGE_TYPE_ID => [LightRequests\GetInfrared::WIRE_SIZE, 'GetInfrared'],
114
        LightRequests\GetPower::MESSAGE_TYPE_ID => [LightRequests\GetPower::WIRE_SIZE, 'GetLightPower'],
115
116
        // Light response messages
117
        LightResponses\State::MESSAGE_TYPE_ID => [LightResponses\State::WIRE_SIZE, 'State'],
118
        LightResponses\StateInfrared::MESSAGE_TYPE_ID => [LightResponses\StateInfrared::WIRE_SIZE, 'StateInfrared'],
119
        LightResponses\StatePower::MESSAGE_TYPE_ID => [LightResponses\StatePower::WIRE_SIZE, 'StateLightPower'],
120
    ];
121
122
    private $uuidFactory;
123
124
    private function unsignedShortToSignedShort(int $unsigned): int
125
    {
126
        if (!($unsigned & 0x8000)) {
127
            return $unsigned;
128
        }
129
130
        return -(($unsigned & 0x7fff) + 1);
131
    }
132
133
    private function nanotimeToDateTimeImmutable(int $timestamp): \DateTimeImmutable
134
    {
135
        $usecs = (int)(($timestamp % 1000000000) / 1000);
136
        $secs = (int)($timestamp / 1000000000);
137
138
        return \DateTimeImmutable::createFromFormat('u U', \sprintf("%06d %d", $usecs, $secs));
139
    }
140
141
    private function decodeAcknowledgement(): DeviceResponses\Acknowledgement
142
    {
143
        return new DeviceResponses\Acknowledgement();
144
    }
145
146
    private function decodeEchoRequest(string $data): DeviceRequests\EchoRequest
147
    {
148
        return new DeviceRequests\EchoRequest($data);
149
    }
150
151
    private function decodeEchoResponse(string $data): DeviceResponses\EchoResponse
152
    {
153
        return new DeviceResponses\EchoResponse($data);
154
    }
155
156
    private function decodeGetGroup(): DeviceRequests\GetGroup
157
    {
158
        return new DeviceRequests\GetGroup;
159
    }
160
161
    private function decodeSetGroup(string $data): DeviceCommands\SetGroup
162
    {
163
        [
164
            'guid'    => $guid,
0 ignored issues
show
Bug introduced by
The variable $guid seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
165
            'label'   => $label,
0 ignored issues
show
Bug introduced by
The variable $label seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
166
            'updated' => $updatedAt,
0 ignored issues
show
Bug introduced by
The variable $updatedAt seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
167
        ] = \unpack('a16guid/a32label/Pupdated', $data);
168
169
        $guid = $this->uuidFactory->fromBytes($guid);
0 ignored issues
show
Bug introduced by
The variable $guid seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
170
        $updatedAt = $this->nanotimeToDateTimeImmutable($updatedAt);
0 ignored issues
show
Bug introduced by
The variable $updatedAt seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
171
        $label = new DeviceDataTypes\Label(\rtrim($label, "\x00"));
0 ignored issues
show
Bug introduced by
The variable $label seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
172
173
        return new DeviceCommands\SetGroup(new DeviceDataTypes\Group($guid, $label, $updatedAt));
174
    }
175
176
    private function decodeStateGroup(string $data): DeviceResponses\StateGroup
177
    {
178
        [
179
            'guid'    => $guid,
0 ignored issues
show
Bug introduced by
The variable $guid seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
180
            'label'   => $label,
0 ignored issues
show
Bug introduced by
The variable $label seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
181
            'updated' => $updatedAt,
0 ignored issues
show
Bug introduced by
The variable $updatedAt seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
182
        ] = \unpack('a16guid/a32label/Pupdated', $data);
183
184
        $guid = $this->uuidFactory->fromBytes($guid);
0 ignored issues
show
Bug introduced by
The variable $guid seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
185
        $updatedAt = $this->nanotimeToDateTimeImmutable($updatedAt);
0 ignored issues
show
Bug introduced by
The variable $updatedAt seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
186
        $label = new DeviceDataTypes\Label(\rtrim($label, "\x00"));
0 ignored issues
show
Bug introduced by
The variable $label seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
187
188
        return new DeviceResponses\StateGroup(new DeviceDataTypes\Group($guid, $label, $updatedAt));
189
    }
190
191
    private function decodeGetHostFirmware(): DeviceRequests\GetHostFirmware
192
    {
193
        return new DeviceRequests\GetHostFirmware;
194
    }
195
196
    private function decodeStateHostFirmware(string $data): DeviceResponses\StateHostFirmware
197
    {
198
        [
199
            'build'    => $build,
0 ignored issues
show
Bug introduced by
The variable $build seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
200
            'version'  => $version,
0 ignored issues
show
Bug introduced by
The variable $version does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
201
        ] = \unpack('Pbuild/Preserved/Vversion', $data);
202
203
        $build = $this->nanotimeToDateTimeImmutable($build);
0 ignored issues
show
Bug introduced by
The variable $build seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
204
205
        return new DeviceResponses\StateHostFirmware(new DeviceDataTypes\HostFirmware($build, $version));
206
    }
207
208
    private function decodeGetHostInfo(): DeviceRequests\GetHostInfo
209
    {
210
        return new DeviceRequests\GetHostInfo;
211
    }
212
213
    /**
214
     * @param string $data
215
     * @return DeviceResponses\StateHostInfo
216
     */
217
    private function decodeStateHostInfo(string $data): DeviceResponses\StateHostInfo
218
    {
219
        [
220
            'signal'   => $signal,
0 ignored issues
show
Bug introduced by
The variable $signal does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
221
            'tx'       => $tx,
0 ignored issues
show
Bug introduced by
The variable $tx does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
222
            'rx'       => $rx,
0 ignored issues
show
Bug introduced by
The variable $rx does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
223
        ] = \unpack(\DaveRandom\LibLifxLan\FLOAT32_CODE . 'signal/Vtx/Vrx/vreserved', $data);
224
225
        return new DeviceResponses\StateHostInfo(new DeviceDataTypes\HostInfo($signal, $tx, $rx));
226
    }
227
228
    private function decodeGetInfo(): DeviceRequests\GetInfo
229
    {
230
        return new DeviceRequests\GetInfo;
231
    }
232
233
    private function decodeStateInfo(string $data): DeviceResponses\StateInfo
234
    {
235
        [
236
            'time'     => $time,
0 ignored issues
show
Bug introduced by
The variable $time seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
237
            'uptime'   => $uptime,
0 ignored issues
show
Bug introduced by
The variable $uptime does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
238
            'downtime' => $downtime,
0 ignored issues
show
Bug introduced by
The variable $downtime does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
239
        ] = \unpack('Ptime/Puptime/Pdowntime', $data);
240
241
        $time = $this->nanotimeToDateTimeImmutable($time);
0 ignored issues
show
Bug introduced by
The variable $time seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
242
243
        return new DeviceResponses\StateInfo(new DeviceDataTypes\TimeInfo($time, $uptime, $downtime));
244
    }
245
246
    private function decodeGetLabel(): DeviceRequests\GetLabel
247
    {
248
        return new DeviceRequests\GetLabel;
249
    }
250
251
    private function decodeSetLabel(string $data): DeviceCommands\SetLabel
252
    {
253
        return new DeviceCommands\SetLabel(new DeviceDataTypes\Label(\rtrim($data, "\x00")));
254
    }
255
256
    private function decodeStateLabel(string $data): DeviceResponses\StateLabel
257
    {
258
        return new DeviceResponses\StateLabel(new DeviceDataTypes\Label(\rtrim($data, "\x00")));
259
    }
260
261
    private function decodeGetLocation(): DeviceRequests\GetLocation
262
    {
263
        return new DeviceRequests\GetLocation;
264
    }
265
266
    private function decodeSetLocation(string $data): DeviceCommands\SetLocation
267
    {
268
        [
269
            'guid'    => $guid,
0 ignored issues
show
Bug introduced by
The variable $guid seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
270
            'label'   => $label,
0 ignored issues
show
Bug introduced by
The variable $label seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
271
            'updated' => $updatedAt,
0 ignored issues
show
Bug introduced by
The variable $updatedAt seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
272
        ] = \unpack('a16guid/a32label/Pupdated', $data);
273
274
        $guid = $this->uuidFactory->fromBytes($guid);
0 ignored issues
show
Bug introduced by
The variable $guid seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
275
        $updatedAt = $this->nanotimeToDateTimeImmutable($updatedAt);
0 ignored issues
show
Bug introduced by
The variable $updatedAt seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
276
        $label = new DeviceDataTypes\Label(\rtrim($label, "\x00"));
0 ignored issues
show
Bug introduced by
The variable $label seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
277
278
        return new DeviceCommands\SetLocation(new DeviceDataTypes\Location($guid, $label, $updatedAt));
279
    }
280
281
    private function decodeStateLocation(string $data): DeviceResponses\StateLocation
282
    {
283
        [
284
            'guid'    => $guid,
0 ignored issues
show
Bug introduced by
The variable $guid seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
285
            'label'   => $label,
0 ignored issues
show
Bug introduced by
The variable $label seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
286
            'updated' => $updatedAt,
0 ignored issues
show
Bug introduced by
The variable $updatedAt seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
287
        ] = \unpack('a16guid/a32label/Pupdated', $data);
288
289
        $guid = $this->uuidFactory->fromBytes($guid);
0 ignored issues
show
Bug introduced by
The variable $guid seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
290
        $updatedAt = $this->nanotimeToDateTimeImmutable($updatedAt);
0 ignored issues
show
Bug introduced by
The variable $updatedAt seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
291
        $label = new DeviceDataTypes\Label(\rtrim($label, "\x00"));
0 ignored issues
show
Bug introduced by
The variable $label seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
292
293
        return new DeviceResponses\StateLocation(new DeviceDataTypes\Location($guid, $label, $updatedAt));
294
    }
295
296
    private function decodeGetDevicePower(): DeviceRequests\GetPower
297
    {
298
        return new DeviceRequests\GetPower;
299
    }
300
301
    private function decodeSetDevicePower(string $data): DeviceCommands\SetPower
302
    {
303
        $level = \unpack('vlevel', $data)['level'];
304
305
        return new DeviceCommands\SetPower($level);
306
    }
307
308
    private function decodeStateDevicePower(string $data): DeviceResponses\StatePower
309
    {
310
        $level = \unpack('vlevel', $data)['level'];
311
312
        return new DeviceResponses\StatePower($level);
313
    }
314
315
    private function decodeGetService(): DeviceRequests\GetService
316
    {
317
        return new DeviceRequests\GetService;
318
    }
319
320
    private function decodeStateService(string $data): DeviceResponses\StateService
321
    {
322
        [
323
            'serviceType' => $serviceType,
0 ignored issues
show
Bug introduced by
The variable $serviceType does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
324
            'port' => $port,
0 ignored issues
show
Bug introduced by
The variable $port does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
325
        ] = \unpack('CserviceType/Vport', $data);
326
327
        return new DeviceResponses\StateService(new DeviceDataTypes\Service($serviceType, $port));
328
    }
329
330
    private function decodeGetVersion(): DeviceRequests\GetVersion
331
    {
332
        return new DeviceRequests\GetVersion;
333
    }
334
335
    private function decodeStateVersion(string $data): DeviceResponses\StateVersion
336
    {
337
        [
338
            'vendor'  => $vendor,
0 ignored issues
show
Bug introduced by
The variable $vendor does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
339
            'product' => $product,
0 ignored issues
show
Bug introduced by
The variable $product does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
340
            'version' => $version,
0 ignored issues
show
Bug introduced by
The variable $version does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
341
        ] = \unpack('Vvendor/Vproduct/Vversion', $data);
342
343
        return new DeviceResponses\StateVersion(new DeviceDataTypes\Version($vendor, $product, $version));
344
    }
345
346
    private function decodeGetWifiFirmware(): DeviceRequests\GetWifiFirmware
347
    {
348
        return new DeviceRequests\GetWifiFirmware;
349
    }
350
351
    private function decodeStateWifiFirmware(string $data): DeviceResponses\StateWifiFirmware
352
    {
353
        [
354
            'build'    => $build,
0 ignored issues
show
Bug introduced by
The variable $build seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
355
            'version'  => $version,
0 ignored issues
show
Bug introduced by
The variable $version does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
356
        ] = \unpack('Pbuild/Preserved/Vversion', $data);
357
358
        $build = $this->nanotimeToDateTimeImmutable($build);
0 ignored issues
show
Bug introduced by
The variable $build seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
359
360
        return new DeviceResponses\StateWifiFirmware(new DeviceDataTypes\WifiFirmware($build, $version));
361
    }
362
363
    private function decodeGetWifiInfo(): DeviceRequests\GetWifiInfo
364
    {
365
        return new DeviceRequests\GetWifiInfo;
366
    }
367
368
    private function decodeStateWifiInfo(string $data): DeviceResponses\StateWifiInfo
369
    {
370
371
        [
372
            'signal'   => $signal,
0 ignored issues
show
Bug introduced by
The variable $signal does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
373
            'tx'       => $tx,
0 ignored issues
show
Bug introduced by
The variable $tx does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
374
            'rx'       => $rx,
0 ignored issues
show
Bug introduced by
The variable $rx does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
375
        ] = \unpack(\DaveRandom\LibLifxLan\FLOAT32_CODE . 'signal/Vtx/Vrx/vreserved', $data);
376
377
        return new DeviceResponses\StateWifiInfo(new DeviceDataTypes\WifiInfo($signal, $tx, $rx));
378
    }
379
380
    private function decodeGet(): LightRequests\Get
381
    {
382
        return new LightRequests\Get;
383
    }
384
385
    private function decodeSetColor(string $data): LightCommmands\SetColor
386
    {
387
        [
388
            'hue'         => $hue,
0 ignored issues
show
Bug introduced by
The variable $hue does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
389
            'saturation'  => $saturation,
0 ignored issues
show
Bug introduced by
The variable $saturation does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
390
            'brightness'  => $brightness,
0 ignored issues
show
Bug introduced by
The variable $brightness does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
391
            'temperature' => $temperature,
0 ignored issues
show
Bug introduced by
The variable $temperature does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
392
            'duration'    => $duration,
0 ignored issues
show
Bug introduced by
The variable $duration does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
393
        ] = \unpack('Creserved/' . self::HSBK_FORMAT . '/Vduration', $data);
394
395
        $color = new LightDataTypes\HsbkColor($hue, $saturation, $brightness, $temperature);
396
397
        return new LightCommmands\SetColor(new LightDataTypes\ColorTransition($color, $duration));
398
    }
399
400
    private function decodeSetWaveform(string $data): LightCommmands\SetWaveform
401
    {
402
        $format
403
            = 'Creserved/Ctransient/'
404
            . self::HSBK_FORMAT
405
            . '/Vperiod/' . \DaveRandom\LibLifxLan\FLOAT32_CODE . 'cycles/vskewRatio/Cwaveform'
406
        ;
407
408
        [
409
            'transient'   => $transient,
0 ignored issues
show
Bug introduced by
The variable $transient does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
410
            'hue'         => $hue,
0 ignored issues
show
Bug introduced by
The variable $hue does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
411
            'saturation'  => $saturation,
0 ignored issues
show
Bug introduced by
The variable $saturation does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
412
            'brightness'  => $brightness,
0 ignored issues
show
Bug introduced by
The variable $brightness does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
413
            'temperature' => $temperature,
0 ignored issues
show
Bug introduced by
The variable $temperature does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
414
            'period'      => $period,
0 ignored issues
show
Bug introduced by
The variable $period does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
415
            'cycles'      => $cycles,
0 ignored issues
show
Bug introduced by
The variable $cycles does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
416
            'skewRatio'   => $skewRatio,
0 ignored issues
show
Bug introduced by
The variable $skewRatio seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
417
            'waveform'    => $waveform,
0 ignored issues
show
Bug introduced by
The variable $waveform does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
418
        ] = \unpack($format, $data);
419
420
        $color = new LightDataTypes\HsbkColor($hue, $saturation, $brightness, $temperature);
421
422
        $skewRatio = $this->unsignedShortToSignedShort($skewRatio);
0 ignored issues
show
Bug introduced by
The variable $skewRatio seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
423
        $effect = new LightDataTypes\Effect((bool)$transient, $color, $period, $cycles, $skewRatio, $waveform);
424
425
        return new LightCommmands\SetWaveform($effect);
426
    }
427
428
    private function decodeSetWaveformOptional(string $data): LightCommmands\SetWaveformOptional
429
    {
430
        $format
431
            = 'Creserved/Ctransient/'
432
            . self::HSBK_FORMAT
433
            . '/Vperiod/' . \DaveRandom\LibLifxLan\FLOAT32_CODE . 'cycles/vskewRatio/Cwaveform'
434
            . '/CsetHue/CsetSaturation/CsetBrightness/CsetTemperature'
435
        ;
436
437
        [
438
            'transient'      => $transient,
0 ignored issues
show
Bug introduced by
The variable $transient does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
439
            'hue'            => $hue,
0 ignored issues
show
Bug introduced by
The variable $hue does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
440
            'saturation'     => $saturation,
0 ignored issues
show
Bug introduced by
The variable $saturation does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
441
            'brightness'     => $brightness,
0 ignored issues
show
Bug introduced by
The variable $brightness does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
442
            'temperature'    => $temperature,
0 ignored issues
show
Bug introduced by
The variable $temperature does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
443
            'period'         => $period,
0 ignored issues
show
Bug introduced by
The variable $period does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
444
            'cycles'         => $cycles,
0 ignored issues
show
Bug introduced by
The variable $cycles does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
445
            'skewRatio'      => $skewRatio,
0 ignored issues
show
Bug introduced by
The variable $skewRatio seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
446
            'waveform'       => $waveform,
0 ignored issues
show
Bug introduced by
The variable $waveform does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
447
            'setHue'         => $setHue,
0 ignored issues
show
Bug introduced by
The variable $setHue does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
448
            'setSaturation'  => $setSaturation,
0 ignored issues
show
Bug introduced by
The variable $setSaturation does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
449
            'setBrightness'  => $setBrightness,
0 ignored issues
show
Bug introduced by
The variable $setBrightness does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
450
            'setTemperature' => $setTemperature,
0 ignored issues
show
Bug introduced by
The variable $setTemperature does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
451
        ] = \unpack($format, $data);
452
453
        $color = new LightDataTypes\HsbkColor($hue, $saturation, $brightness, $temperature);
454
        $skewRatio = $this->unsignedShortToSignedShort($skewRatio);
0 ignored issues
show
Bug introduced by
The variable $skewRatio seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
455
456
        $options = ($setHue ? LightDataTypes\Effect::SET_HUE : 0)
457
            | ($setSaturation ? LightDataTypes\Effect::SET_SATURATION : 0)
458
            | ($setBrightness ? LightDataTypes\Effect::SET_BRIGHTNESS : 0)
459
            | ($setTemperature ? LightDataTypes\Effect::SET_TEMPERATURE : 0);
460
461
        $effect = new LightDataTypes\Effect((bool)$transient, $color, $period, $cycles, $skewRatio, $waveform, $options);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
462
463
        return new LightCommmands\SetWaveformOptional($effect);
464
    }
465
466
    private function decodeState(string $data): LightResponses\State
467
    {
468
        [
469
            'hue'         => $hue,
0 ignored issues
show
Bug introduced by
The variable $hue does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
470
            'saturation'  => $saturation,
0 ignored issues
show
Bug introduced by
The variable $saturation does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
471
            'brightness'  => $brightness,
0 ignored issues
show
Bug introduced by
The variable $brightness does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
472
            'temperature' => $temperature,
0 ignored issues
show
Bug introduced by
The variable $temperature does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
473
            'power'       => $power,
0 ignored issues
show
Bug introduced by
The variable $power does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
474
            'label'       => $label,
0 ignored issues
show
Bug introduced by
The variable $label seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
475
        ] = \unpack(self::HSBK_FORMAT . '/vreserved/vpower/a32label/Preserved', $data);
476
477
        $color = new LightDataTypes\HsbkColor($hue, $saturation, $brightness, $temperature);
478
        $label = new DeviceDataTypes\Label(\rtrim($label, "\x00"));
0 ignored issues
show
Bug introduced by
The variable $label seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
479
480
        return new LightResponses\State(new LightDataTypes\State($color, $power, $label));
481
    }
482
483
    private function decodeGetInfrared(): LightRequests\GetInfrared
484
    {
485
        return new LightRequests\GetInfrared;
486
    }
487
488
    private function decodeSetInfrared(string $data): LightCommmands\SetInfrared
489
    {
490
        $level = \unpack('vlevel', $data)['level'];
491
492
        return new LightCommmands\SetInfrared($level);
493
    }
494
495
    private function decodeStateInfrared(string $data): LightResponses\StateInfrared
496
    {
497
        $level = \unpack('vlevel', $data)['level'];
498
499
        return new LightResponses\StateInfrared($level);
500
    }
501
502
    private function decodeGetLightPower(): LightRequests\GetPower
503
    {
504
        return new LightRequests\GetPower;
505
    }
506
507
    private function decodeSetLightPower(string $data): LightCommmands\SetPower
508
    {
509
        [
510
            'level'    => $level,
0 ignored issues
show
Bug introduced by
The variable $level does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
511
            'duration' => $duration,
0 ignored issues
show
Bug introduced by
The variable $duration does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
512
        ] = \unpack('vlevel/Vduration', $data);
513
514
        return new LightCommmands\SetPower(new LightDataTypes\PowerTransition($level, $duration));
515
    }
516
517
    private function decodeStateLightPower(string $data): LightResponses\StatePower
518
    {
519
        $level = \unpack('vlevel', $data)['level'];
520
521
        return new LightResponses\StatePower($level);
522
    }
523
524
    public function __construct(UuidFactoryInterface $uuidFactory = null)
525
    {
526
        $this->uuidFactory = $uuidFactory ?? new UuidFactory;
527
    }
528
529
    /**
530
     * @param int $type
531
     * @param string $data
532
     * @return Message
533
     * @throws DecodingException
534
     */
535
    public function decodeMessage(int $type, string $data): Message
536
    {
537
        if (!\array_key_exists($type, self::MESSAGE_INFO)) {
538
            return new UnknownMessage($type, $data);
539
        }
540
541
        [$payloadLength, $messageName] = self::MESSAGE_INFO[$type];
0 ignored issues
show
Bug introduced by
The variable $payloadLength does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $messageName does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
542
543
        if (\strlen($data) !== $payloadLength) {
544
            throw new InvalidMessagePayloadLengthException(
545
                "Invalid payload length for {$messageName} message,"
546
                . " expecting {$payloadLength} bytes, got " . \strlen($data)
547
            );
548
        }
549
550
        try {
551
            return ([$this, 'decode' . $messageName])($data);
552
        } /** @noinspection PhpRedundantCatchClauseInspection */ catch (InvalidValueException $e) {
553
            throw new MalformedMessagePayloadException($e->getMessage(), $e->getCode(), $e);
554
        }
555
    }
556
}
557