Complex classes like Decoder 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 Decoder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | final class Decoder |
||
| 17 | { |
||
| 18 | /** @var string */ |
||
| 19 | private $data = ''; |
||
| 20 | |||
| 21 | /** @var int */ |
||
| 22 | private $offset = 0; |
||
| 23 | |||
| 24 | 62 | public function decode(string $data) |
|
| 31 | |||
| 32 | 62 | private function parse() |
|
| 33 | { |
||
| 34 | 62 | if (!isset($this->data[$this->offset])) { |
|
| 35 | throw InsufficientData::fromOffset($this->data, $this->offset, 1); |
||
| 36 | } |
||
| 37 | |||
| 38 | 62 | $byte = ORD[$this->data[$this->offset++]]; |
|
| 39 | |||
| 40 | 62 | if ($byte < 0xc0) { |
|
| 41 | // positive fixint |
||
| 42 | 14 | if ($byte < 0x80) { |
|
| 43 | 11 | return $byte; |
|
| 44 | } |
||
| 45 | // fixmap |
||
| 46 | 6 | if ($byte < 0x90) { |
|
| 47 | 2 | return $this->decodeMap($byte & 0xf); |
|
| 48 | } |
||
| 49 | // fixarray |
||
| 50 | 4 | if ($byte < 0xa0) { |
|
| 51 | 2 | return $this->decodeArray($byte & 0x0f); |
|
| 52 | } |
||
| 53 | // fixstr |
||
| 54 | 2 | return $this->decodeStr($byte & 0x1f); |
|
| 55 | } |
||
| 56 | // negative fixint |
||
| 57 | 54 | if ($byte > 0xdf) { |
|
| 58 | 2 | return $byte - 0x100; |
|
| 59 | } |
||
| 60 | |||
| 61 | 52 | switch ($byte) { |
|
| 62 | 52 | case 0xc0: return null; |
|
| 63 | 51 | case 0xc2: return false; |
|
| 64 | 50 | case 0xc3: return true; |
|
| 65 | |||
| 66 | // bin 8/16/32 |
||
| 67 | 48 | case 0xc4: return $this->decodeStr($this->decodeUint8()); |
|
| 68 | 48 | case 0xc5: return $this->decodeStr($this->decodeUint16()); |
|
| 69 | 48 | case 0xc6: return $this->decodeStr($this->decodeUint32()); |
|
| 70 | |||
| 71 | // float 32/64 |
||
| 72 | 48 | case 0xca: return $this->decodeFloat32(); |
|
| 73 | 44 | case 0xcb: return $this->decodeFloat64(); |
|
| 74 | |||
| 75 | // uint 8/16/32/64 |
||
| 76 | 38 | case 0xcc: return $this->decodeUint8(); |
|
| 77 | 36 | case 0xcd: return $this->decodeUint16(); |
|
| 78 | 34 | case 0xce: return $this->decodeUint32(); |
|
| 79 | 32 | case 0xcf: return $this->decodeUint64(); |
|
| 80 | |||
| 81 | // int 8/16/32/64 |
||
| 82 | 30 | case 0xd0: return $this->decodeInt8(); |
|
| 83 | 28 | case 0xd1: return $this->decodeInt16(); |
|
| 84 | 26 | case 0xd2: return $this->decodeInt32(); |
|
| 85 | 24 | case 0xd3: return $this->decodeInt64(); |
|
| 86 | |||
| 87 | // str 8/16/32 |
||
| 88 | 21 | case 0xd9: return $this->decodeStr($this->decodeUint8()); |
|
| 89 | 19 | case 0xda: return $this->decodeStr($this->decodeUint16()); |
|
| 90 | 17 | case 0xdb: return $this->decodeStr($this->decodeUint32()); |
|
| 91 | |||
| 92 | // array 16/32 |
||
| 93 | 16 | case 0xdc: return $this->decodeArray($this->decodeUint16()); |
|
| 94 | 14 | case 0xdd: return $this->decodeArray($this->decodeUint32()); |
|
| 95 | |||
| 96 | // map 16/32 |
||
| 97 | 13 | case 0xde: return $this->decodeMap($this->decodeUint16()); |
|
| 98 | 11 | case 0xdf: return $this->decodeMap($this->decodeUint32()); |
|
| 99 | |||
| 100 | // fixext 1/2/4/8/16 |
||
| 101 | 10 | case 0xd4: return $this->decodeExt(1); |
|
| 102 | 9 | case 0xd5: return $this->decodeExt(2); |
|
| 103 | 8 | case 0xd6: return $this->decodeExt(4); |
|
| 104 | 7 | case 0xd7: return $this->decodeExt(8); |
|
| 105 | 6 | case 0xd8: return $this->decodeExt(16); |
|
| 106 | |||
| 107 | // ext 8/16/32 |
||
| 108 | 5 | case 0xc7: return $this->decodeExt($this->decodeUint8()); |
|
| 109 | 4 | case 0xc8: return $this->decodeExt($this->decodeUint16()); |
|
| 110 | 2 | case 0xc9: return $this->decodeExt($this->decodeUint32()); |
|
| 111 | } |
||
| 112 | |||
| 113 | 1 | throw UnknownByteHeader::fromOffset($byte, $this->offset); |
|
| 114 | } |
||
| 115 | |||
| 116 | 4 | private function decodeFloat32(): float |
|
| 117 | { |
||
| 118 | 4 | if (!isset($this->data[$this->offset + 3])) { |
|
| 119 | throw InsufficientData::fromOffset($this->data, $this->offset, 4); |
||
| 120 | } |
||
| 121 | |||
| 122 | 4 | $num = ORD[$this->data[$this->offset++]] * 0x1000000 |
|
| 123 | 4 | | ORD[$this->data[$this->offset++]] << 16 |
|
| 124 | 4 | | ORD[$this->data[$this->offset++]] << 8 |
|
| 125 | 4 | | ORD[$this->data[$this->offset++]]; |
|
| 126 | |||
| 127 | 4 | return toFloat($num); |
|
| 128 | } |
||
| 129 | |||
| 130 | 6 | private function decodeFloat64(): float |
|
| 131 | { |
||
| 132 | 6 | if (!isset($this->data[$this->offset + 7])) { |
|
| 133 | throw InsufficientData::fromOffset($this->data, $this->offset, 8); |
||
| 134 | } |
||
| 135 | |||
| 136 | 6 | $x = ORD[$this->data[$this->offset++]] * 0x1000000 |
|
| 137 | 6 | | ORD[$this->data[$this->offset++]] << 16 |
|
| 138 | 6 | | ORD[$this->data[$this->offset++]] << 8 |
|
| 139 | 6 | | ORD[$this->data[$this->offset++]]; |
|
| 140 | |||
| 141 | 6 | $y = ORD[$this->data[$this->offset++]] * 0x1000000 |
|
| 142 | 6 | | ORD[$this->data[$this->offset++]] << 16 |
|
| 143 | 6 | | ORD[$this->data[$this->offset++]] << 8 |
|
| 144 | 6 | | ORD[$this->data[$this->offset++]]; |
|
| 145 | |||
| 146 | 6 | return toDouble($y, $x); |
|
| 147 | } |
||
| 148 | |||
| 149 | 7 | private function decodeUint8(): int |
|
| 150 | { |
||
| 151 | 7 | if (!isset($this->data[$this->offset])) { |
|
| 152 | throw InsufficientData::fromOffset($this->data, $this->offset, 1); |
||
| 153 | } |
||
| 154 | |||
| 155 | 7 | return ORD($this->data[$this->offset++]); |
|
| 156 | } |
||
| 157 | |||
| 158 | 11 | private function decodeUint16(): int |
|
| 159 | { |
||
| 160 | 11 | if (!isset($this->data[$this->offset + 1])) { |
|
| 161 | throw InsufficientData::fromOffset($this->data, $this->offset, 2); |
||
| 162 | } |
||
| 163 | |||
| 164 | 11 | return ORD[$this->data[$this->offset++]] << 8 |
|
| 165 | 11 | | ORD[$this->data[$this->offset++]]; |
|
| 166 | } |
||
| 167 | |||
| 168 | 6 | private function decodeUint32(): int |
|
| 169 | { |
||
| 170 | 6 | if (!isset($this->data[$this->offset + 3])) { |
|
| 171 | throw InsufficientData::fromOffset($this->data, $this->offset, 4); |
||
| 172 | } |
||
| 173 | |||
| 174 | 6 | return ORD[$this->data[$this->offset++]] * 0x1000000 |
|
| 175 | 6 | | ORD[$this->data[$this->offset++]] << 16 |
|
| 176 | 6 | | ORD[$this->data[$this->offset++]] << 8 |
|
| 177 | 6 | | ORD[$this->data[$this->offset++]]; |
|
| 178 | } |
||
| 179 | |||
| 180 | 2 | private function decodeUint64() |
|
| 181 | { |
||
| 182 | 2 | if (!isset($this->data[$this->offset + 7])) { |
|
| 183 | throw InsufficientData::fromOffset($this->data, $this->offset, 8); |
||
| 184 | } |
||
| 185 | |||
| 186 | 2 | $num = (ORD[$this->data[$this->offset++]] * 0x1000000 |
|
| 187 | 2 | | ORD[$this->data[$this->offset++]] << 16 |
|
| 188 | 2 | | ORD[$this->data[$this->offset++]] << 8 |
|
| 189 | 2 | | ORD[$this->data[$this->offset++]]) * 0x100000000 |
|
| 190 | 2 | | ORD[$this->data[$this->offset++]] * 0x1000000 |
|
| 191 | 2 | | ORD[$this->data[$this->offset++]] << 16 |
|
| 192 | 2 | | ORD[$this->data[$this->offset++]] << 8 |
|
| 193 | 2 | | ORD[$this->data[$this->offset++]]; |
|
| 194 | |||
| 195 | 2 | return $num < 0 ? sprintf('%u', $num) : $num; |
|
| 196 | } |
||
| 197 | |||
| 198 | 11 | private function decodeInt8(): int |
|
| 199 | { |
||
| 200 | 11 | if (!isset($this->data[$this->offset])) { |
|
| 201 | throw InsufficientData::fromOffset($this->data, $this->offset, 1); |
||
| 202 | } |
||
| 203 | |||
| 204 | 11 | $num = ORD[$this->data[$this->offset++]]; |
|
| 205 | |||
| 206 | 11 | return $num & 0x80 ? $num - 0x100 : $num; |
|
| 207 | } |
||
| 208 | |||
| 209 | 2 | private function decodeInt16(): int |
|
| 210 | { |
||
| 211 | 2 | if (!isset($this->data[$this->offset + 1])) { |
|
| 212 | throw InsufficientData::fromOffset($this->data, $this->offset, 2); |
||
| 213 | } |
||
| 214 | |||
| 215 | 2 | $num = ORD[$this->data[$this->offset++]] << 8 |
|
| 216 | 2 | | ORD[$this->data[$this->offset++]]; |
|
| 217 | |||
| 218 | 2 | return $num & 0x8000 ? $num - 0x10000 : $num; |
|
| 219 | } |
||
| 220 | |||
| 221 | 2 | private function decodeInt32(): int |
|
| 222 | { |
||
| 223 | 2 | if (!isset($this->data[$this->offset + 3])) { |
|
| 224 | throw InsufficientData::fromOffset($this->data, $this->offset, 4); |
||
| 225 | } |
||
| 226 | |||
| 227 | 2 | $num = ORD[$this->data[$this->offset++]] * 0x1000000 |
|
| 228 | 2 | | ORD[$this->data[$this->offset++]] << 16 |
|
| 229 | 2 | | ORD[$this->data[$this->offset++]] << 8 |
|
| 230 | 2 | | ORD[$this->data[$this->offset++]]; |
|
| 231 | |||
| 232 | 2 | return $num & 0x80000000 ? $num - 0x100000000 : $num; |
|
| 233 | } |
||
| 234 | |||
| 235 | 3 | private function decodeInt64(): int |
|
| 236 | { |
||
| 237 | 3 | if (!isset($this->data[$this->offset + 7])) { |
|
| 238 | throw InsufficientData::fromOffset($this->data, $this->offset, 8); |
||
| 239 | } |
||
| 240 | |||
| 241 | 3 | $num = ORD[$this->data[$this->offset++]]; |
|
| 242 | |||
| 243 | 3 | $negate = ($num & 0x80) === 0x80; |
|
| 244 | |||
| 245 | 3 | if ($negate) { |
|
| 246 | 2 | $num = (($num ^ 0xff) * 0x100000000000000) |
|
| 247 | 2 | | (ORD[$this->data[$this->offset++]] ^ 0xff) * 0x1000000000000 |
|
| 248 | 2 | | (ORD[$this->data[$this->offset++]] ^ 0xff) * 0x10000000000 |
|
| 249 | 2 | | (ORD[$this->data[$this->offset++]] ^ 0xff) * 0x100000000 |
|
| 250 | 2 | | (ORD[$this->data[$this->offset++]] ^ 0xff) * 0x1000000 |
|
| 251 | 2 | | (ORD[$this->data[$this->offset++]] ^ 0xff) * 0x10000 |
|
| 252 | 2 | | (ORD[$this->data[$this->offset++]] ^ 0xff) * 0x100 |
|
| 253 | 2 | | ORD[$this->data[$this->offset++]] ^ 0xff; |
|
| 254 | |||
| 255 | 2 | return ~$num; |
|
| 256 | } |
||
| 257 | |||
| 258 | 1 | $num = ($num * 0x100000000000000) |
|
| 259 | 1 | | ORD[$this->data[$this->offset++]] * 0x1000000000000 |
|
| 260 | 1 | | ORD[$this->data[$this->offset++]] * 0x10000000000 |
|
| 261 | 1 | | ORD[$this->data[$this->offset++]] * 0x100000000 |
|
| 262 | 1 | | ORD[$this->data[$this->offset++]] * 0x1000000 |
|
| 263 | 1 | | ORD[$this->data[$this->offset++]] * 0x10000 |
|
| 264 | 1 | | ORD[$this->data[$this->offset++]] * 0x100 |
|
| 265 | 1 | | ORD[$this->data[$this->offset++]]; |
|
| 266 | |||
| 267 | 1 | return $num; |
|
| 268 | } |
||
| 269 | |||
| 270 | 7 | private function decodeStr(int $length): string |
|
| 271 | { |
||
| 272 | 7 | if (0 === $length) { |
|
| 273 | 1 | return ''; |
|
| 274 | } |
||
| 275 | |||
| 276 | 6 | if (!isset($this->data[$this->offset + $length - 1])) { |
|
| 277 | throw InsufficientData::fromOffset($this->data, $this->offset, $length); |
||
| 278 | } |
||
| 279 | |||
| 280 | 6 | $str = substr($this->data, $this->offset++, $length); |
|
| 281 | 6 | $this->offset += $length; |
|
| 282 | |||
| 283 | 6 | return $str; |
|
| 284 | } |
||
| 285 | |||
| 286 | 5 | private function decodeArray(int $size): array |
|
| 295 | |||
| 296 | 5 | private function decodeMap(int $size): array |
|
| 305 | |||
| 306 | 9 | private function decodeExt(int $length): Ext |
|
| 307 | { |
||
| 308 | 9 | if (!isset($this->data[$this->offset + $length - 1])) { |
|
| 309 | throw InsufficientData::fromOffset($this->data, $this->offset, $length); |
||
| 310 | } |
||
| 311 | |||
| 312 | 9 | $type = $this->decodeInt8(); |
|
| 313 | |||
| 314 | 9 | $data = substr($this->data, $this->offset++, $length); |
|
| 315 | 9 | $this->offset += $length; |
|
| 316 | |||
| 317 | 9 | return Ext::make($type, $data); |
|
| 319 | } |
||
| 320 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: