Complex classes like MessageDecoder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MessageDecoder, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1);  | 
            ||
| 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'],  | 
            ||
| 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'],  | 
            ||
| 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'],  | 
            ||
| 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'],  | 
            ||
| 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'],  | 
            ||
| 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'],  | 
            ||
| 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  | 
            ||
| 132 | |||
| 133 | private function nanotimeToDateTimeImmutable(int $timestamp): \DateTimeImmutable  | 
            ||
| 140 | |||
| 141 | private function decodeAcknowledgement(): DeviceResponses\Acknowledgement  | 
            ||
| 145 | |||
| 146 | private function decodeEchoRequest(string $data): DeviceRequests\EchoRequest  | 
            ||
| 150 | |||
| 151 | private function decodeEchoResponse(string $data): DeviceResponses\EchoResponse  | 
            ||
| 155 | |||
| 156 | private function decodeGetGroup(): DeviceRequests\GetGroup  | 
            ||
| 160 | |||
| 161 | private function decodeSetGroup(string $data): DeviceCommands\SetGroup  | 
            ||
| 175 | |||
| 176 | private function decodeStateGroup(string $data): DeviceResponses\StateGroup  | 
            ||
| 190 | |||
| 191 | private function decodeGetHostFirmware(): DeviceRequests\GetHostFirmware  | 
            ||
| 195 | |||
| 196 | private function decodeStateHostFirmware(string $data): DeviceResponses\StateHostFirmware  | 
            ||
| 207 | |||
| 208 | private function decodeGetHostInfo(): DeviceRequests\GetHostInfo  | 
            ||
| 212 | |||
| 213 | /**  | 
            ||
| 214 | * @param string $data  | 
            ||
| 215 | * @return DeviceResponses\StateHostInfo  | 
            ||
| 216 | */  | 
            ||
| 217 | private function decodeStateHostInfo(string $data): DeviceResponses\StateHostInfo  | 
            ||
| 227 | |||
| 228 | private function decodeGetInfo(): DeviceRequests\GetInfo  | 
            ||
| 232 | |||
| 233 | private function decodeStateInfo(string $data): DeviceResponses\StateInfo  | 
            ||
| 245 | |||
| 246 | private function decodeGetLabel(): DeviceRequests\GetLabel  | 
            ||
| 250 | |||
| 251 | private function decodeSetLabel(string $data): DeviceCommands\SetLabel  | 
            ||
| 255 | |||
| 256 | private function decodeStateLabel(string $data): DeviceResponses\StateLabel  | 
            ||
| 260 | |||
| 261 | private function decodeGetLocation(): DeviceRequests\GetLocation  | 
            ||
| 265 | |||
| 266 | private function decodeSetLocation(string $data): DeviceCommands\SetLocation  | 
            ||
| 280 | |||
| 281 | private function decodeStateLocation(string $data): DeviceResponses\StateLocation  | 
            ||
| 295 | |||
| 296 | private function decodeGetDevicePower(): DeviceRequests\GetPower  | 
            ||
| 300 | |||
| 301 | private function decodeSetDevicePower(string $data): DeviceCommands\SetPower  | 
            ||
| 307 | |||
| 308 | private function decodeStateDevicePower(string $data): DeviceResponses\StatePower  | 
            ||
| 314 | |||
| 315 | private function decodeGetService(): DeviceRequests\GetService  | 
            ||
| 319 | |||
| 320 | private function decodeStateService(string $data): DeviceResponses\StateService  | 
            ||
| 329 | |||
| 330 | private function decodeGetVersion(): DeviceRequests\GetVersion  | 
            ||
| 334 | |||
| 335 | private function decodeStateVersion(string $data): DeviceResponses\StateVersion  | 
            ||
| 345 | |||
| 346 | private function decodeGetWifiFirmware(): DeviceRequests\GetWifiFirmware  | 
            ||
| 350 | |||
| 351 | private function decodeStateWifiFirmware(string $data): DeviceResponses\StateWifiFirmware  | 
            ||
| 362 | |||
| 363 | private function decodeGetWifiInfo(): DeviceRequests\GetWifiInfo  | 
            ||
| 367 | |||
| 368 | private function decodeStateWifiInfo(string $data): DeviceResponses\StateWifiInfo  | 
            ||
| 379 | |||
| 380 | private function decodeGet(): LightRequests\Get  | 
            ||
| 384 | |||
| 385 | private function decodeSetColor(string $data): LightCommmands\SetColor  | 
            ||
| 399 | |||
| 400 | private function decodeSetWaveform(string $data): LightCommmands\SetWaveform  | 
            ||
| 427 | |||
| 428 | private function decodeSetWaveformOptional(string $data): LightCommmands\SetWaveformOptional  | 
            ||
| 465 | |||
| 466 | private function decodeState(string $data): LightResponses\State  | 
            ||
| 482 | |||
| 483 | private function decodeGetInfrared(): LightRequests\GetInfrared  | 
            ||
| 487 | |||
| 488 | private function decodeSetInfrared(string $data): LightCommmands\SetInfrared  | 
            ||
| 494 | |||
| 495 | private function decodeStateInfrared(string $data): LightResponses\StateInfrared  | 
            ||
| 501 | |||
| 502 | private function decodeGetLightPower(): LightRequests\GetPower  | 
            ||
| 506 | |||
| 507 | private function decodeSetLightPower(string $data): LightCommmands\SetPower  | 
            ||
| 516 | |||
| 517 | private function decodeStateLightPower(string $data): LightResponses\StatePower  | 
            ||
| 523 | |||
| 524 | public function __construct(UuidFactoryInterface $uuidFactory = null)  | 
            ||
| 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  | 
            ||
| 555 | }  | 
            ||
| 556 |