Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BufferUnpacker 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 BufferUnpacker, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class BufferUnpacker |
||
| 20 | { |
||
| 21 | const INT_AS_EXCEPTION = 0; |
||
| 22 | const INT_AS_STR = 1; |
||
| 23 | const INT_AS_GMP = 2; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | private $intOverflowMode = self::INT_AS_EXCEPTION; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $buffer = ''; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | private $offset = 0; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var Collection |
||
| 42 | */ |
||
| 43 | private $transformers; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param int|null $intOverflowMode |
||
| 47 | */ |
||
| 48 | 101 | public function __construct($intOverflowMode = null) |
|
| 49 | { |
||
| 50 | 101 | if (null !== $intOverflowMode) { |
|
| 51 | 2 | $this->intOverflowMode = $intOverflowMode; |
|
| 52 | 2 | } |
|
| 53 | 101 | } |
|
| 54 | |||
| 55 | /** |
||
| 56 | * @param Collection|null $transformers |
||
| 57 | */ |
||
| 58 | 2 | public function setTransformers(Collection $transformers = null) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @return Collection|null |
||
| 65 | */ |
||
| 66 | 1 | public function getTransformers() |
|
| 67 | { |
||
| 68 | 1 | return $this->transformers; |
|
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param int $intOverflowMode |
||
| 73 | * |
||
| 74 | * @throws \InvalidArgumentException |
||
| 75 | */ |
||
| 76 | 2 | public function setIntOverflowMode($intOverflowMode) |
|
| 77 | { |
||
| 78 | 2 | if (!\in_array($intOverflowMode, [ |
|
| 79 | 2 | self::INT_AS_EXCEPTION, |
|
| 80 | 2 | self::INT_AS_STR, |
|
| 81 | 2 | self::INT_AS_GMP, |
|
| 82 | 2 | ], true)) { |
|
| 83 | 1 | throw new \InvalidArgumentException(\sprintf('Invalid integer overflow mode: %s.', $intOverflowMode)); |
|
| 84 | } |
||
| 85 | |||
| 86 | 1 | $this->intOverflowMode = $intOverflowMode; |
|
| 87 | 1 | } |
|
| 88 | |||
| 89 | /** |
||
| 90 | * @return int |
||
| 91 | */ |
||
| 92 | 1 | public function getIntOverflowMode() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $data |
||
| 99 | * |
||
| 100 | * @return $this |
||
| 101 | */ |
||
| 102 | 5 | public function append($data) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @param string $buffer |
||
| 111 | * |
||
| 112 | * @return $this |
||
| 113 | */ |
||
| 114 | 93 | public function reset($buffer = '') |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | 3 | public function tryUnpack() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @return mixed |
||
| 149 | * |
||
| 150 | * @throws UnpackingFailedException |
||
| 151 | */ |
||
| 152 | 97 | public function unpack() |
|
| 153 | { |
||
| 154 | 97 | $this->ensureLength(1); |
|
| 155 | |||
| 156 | 95 | $c = \ord($this->buffer[$this->offset]); |
|
| 157 | 95 | ++$this->offset; |
|
| 158 | |||
| 159 | // fixint |
||
| 160 | 95 | if ($c <= 0x7f) { |
|
| 161 | 20 | return $c; |
|
| 162 | } |
||
| 163 | // fixstr |
||
| 164 | 92 | if ($c >= 0xa0 && $c <= 0xbf) { |
|
| 165 | 12 | return $this->unpackStr($c & 0x1f); |
|
| 166 | } |
||
| 167 | // fixarray |
||
| 168 | 87 | if ($c >= 0x90 && $c <= 0x9f) { |
|
| 169 | 6 | return $this->unpackArray($c & 0xf); |
|
| 170 | } |
||
| 171 | // fixmap |
||
| 172 | 84 | if ($c >= 0x80 && $c <= 0x8f) { |
|
| 173 | 11 | return $this->unpackMap($c & 0xf); |
|
| 174 | } |
||
| 175 | // negfixint |
||
| 176 | 80 | if ($c >= 0xe0) { |
|
| 177 | 4 | return $c - 256; |
|
| 178 | } |
||
| 179 | |||
| 180 | switch ($c) { |
||
| 181 | 76 | case 0xc0: return null; |
|
| 182 | 75 | case 0xc2: return false; |
|
| 183 | 73 | case 0xc3: return true; |
|
| 184 | |||
| 185 | // MP_BIN |
||
| 186 | 68 | case 0xc4: return $this->unpackStr($this->unpackUint8()); |
|
| 187 | 64 | case 0xc5: return $this->unpackStr($this->unpackUint16()); |
|
| 188 | 63 | case 0xc6: return $this->unpackStr($this->unpackUint32()); |
|
| 189 | |||
| 190 | // MP_FLOAT |
||
| 191 | 62 | case 0xca: return $this->unpackFloat32(); |
|
| 192 | 60 | case 0xcb: return $this->unpackFloat64(); |
|
| 193 | |||
| 194 | // MP_UINT |
||
| 195 | 57 | case 0xcc: return $this->unpackUint8(); |
|
| 196 | 54 | case 0xcd: return $this->unpackUint16(); |
|
| 197 | 50 | case 0xce: return $this->unpackUint32(); |
|
| 198 | 47 | case 0xcf: return $this->unpackUint64(); |
|
| 199 | |||
| 200 | // MP_INT |
||
| 201 | 40 | case 0xd0: return $this->unpackInt8(); |
|
| 202 | 36 | case 0xd1: return $this->unpackInt16(); |
|
| 203 | 32 | case 0xd2: return $this->unpackInt32(); |
|
| 204 | 28 | case 0xd3: return $this->unpackInt64(); |
|
| 205 | |||
| 206 | // MP_STR |
||
| 207 | 24 | case 0xd9: return $this->unpackStr($this->unpackUint8()); |
|
| 208 | 20 | case 0xda: return $this->unpackStr($this->unpackUint16()); |
|
| 209 | 18 | case 0xdb: return $this->unpackStr($this->unpackUint32()); |
|
| 210 | |||
| 211 | // MP_ARRAY |
||
| 212 | 17 | case 0xdc: return $this->unpackArray($this->unpackUint16()); |
|
| 213 | 15 | case 0xdd: return $this->unpackArray($this->unpackUint32()); |
|
| 214 | |||
| 215 | // MP_MAP |
||
| 216 | 14 | case 0xde: return $this->unpackMap($this->unpackUint16()); |
|
| 217 | 12 | case 0xdf: return $this->unpackMap($this->unpackUint32()); |
|
| 218 | |||
| 219 | // MP_EXT |
||
| 220 | 11 | case 0xd4: return $this->unpackExt(1); |
|
| 221 | 9 | case 0xd5: return $this->unpackExt(2); |
|
| 222 | 8 | case 0xd6: return $this->unpackExt(4); |
|
| 223 | 7 | case 0xd7: return $this->unpackExt(8); |
|
| 224 | 6 | case 0xd8: return $this->unpackExt(16); |
|
| 225 | 5 | case 0xc7: return $this->unpackExt($this->unpackUint8()); |
|
| 226 | 3 | case 0xc8: return $this->unpackExt($this->unpackUint16()); |
|
| 227 | 2 | case 0xc9: return $this->unpackExt($this->unpackUint32()); |
|
| 228 | } |
||
| 229 | |||
| 230 | 1 | throw new UnpackingFailedException(\sprintf('Unknown code: 0x%x.', $c)); |
|
| 231 | } |
||
| 232 | |||
| 233 | 16 | private function unpackUint8() |
|
| 234 | { |
||
| 235 | 16 | $this->ensureLength(1); |
|
| 236 | |||
| 237 | 16 | $num = $this->buffer[$this->offset]; |
|
| 238 | 16 | ++$this->offset; |
|
| 239 | |||
| 240 | 16 | return \ord($num); |
|
| 241 | } |
||
| 242 | |||
| 243 | 13 | private function unpackUint16() |
|
| 244 | { |
||
| 245 | 13 | $this->ensureLength(2); |
|
| 246 | |||
| 247 | 13 | $hi = \ord($this->buffer[$this->offset]); |
|
| 248 | 13 | $lo = \ord($this->buffer[$this->offset + 1]); |
|
| 249 | 13 | $this->offset += 2; |
|
| 250 | |||
| 251 | 13 | return $hi << 8 | $lo; |
|
| 252 | } |
||
| 253 | |||
| 254 | 9 | View Code Duplication | private function unpackUint32() |
| 255 | { |
||
| 256 | 9 | $this->ensureLength(4); |
|
| 257 | |||
| 258 | 9 | $num = \substr($this->buffer, $this->offset, 4); |
|
| 259 | 9 | $this->offset += 4; |
|
| 260 | |||
| 261 | 9 | $num = \unpack('N', $num); |
|
| 262 | |||
| 263 | 9 | return $num[1]; |
|
| 264 | } |
||
| 265 | |||
| 266 | 7 | private function unpackUint64() |
|
| 267 | { |
||
| 268 | 7 | $this->ensureLength(8); |
|
| 269 | |||
| 270 | 7 | $num = \substr($this->buffer, $this->offset, 8); |
|
| 271 | 7 | $this->offset += 8; |
|
| 272 | |||
| 273 | //$num = \unpack('J', $num); |
||
| 274 | |||
| 275 | 7 | $set = \unpack('N2', $num); |
|
| 276 | 7 | $value = $set[1] << 32 | $set[2]; |
|
| 277 | |||
| 278 | // PHP does not support unsigned integers. |
||
| 279 | // If a number is bigger than 2^63, it will be interpreted as a float. |
||
| 280 | // @link http://php.net/manual/en/language.types.integer.php#language.types.integer.overflow |
||
| 281 | |||
| 282 | 7 | return ($value < 0) ? $this->handleIntOverflow($value) : $value; |
|
| 283 | } |
||
| 284 | |||
| 285 | 14 | private function unpackInt8() |
|
| 286 | { |
||
| 287 | 14 | $this->ensureLength(1); |
|
| 288 | |||
| 289 | 14 | $num = \ord($this->buffer[$this->offset]); |
|
| 290 | 14 | ++$this->offset; |
|
| 291 | |||
| 292 | 14 | if ($num > 0x7f) { |
|
| 293 | 3 | return $num - 256; |
|
| 294 | } |
||
| 295 | |||
| 296 | 11 | return $num; |
|
| 297 | } |
||
| 298 | |||
| 299 | 4 | private function unpackInt16() |
|
| 300 | { |
||
| 301 | 4 | $this->ensureLength(2); |
|
| 302 | |||
| 303 | 4 | $hi = \ord($this->buffer[$this->offset]); |
|
| 304 | 4 | $lo = \ord($this->buffer[$this->offset + 1]); |
|
| 305 | 4 | $this->offset += 2; |
|
| 306 | |||
| 307 | 4 | if ($hi > 0x7f) { |
|
| 308 | 3 | return -(0x010000 - ($hi << 8 | $lo)); |
|
| 309 | } |
||
| 310 | |||
| 311 | 1 | return $hi << 8 | $lo; |
|
| 312 | } |
||
| 313 | |||
| 314 | 4 | private function unpackInt32() |
|
| 315 | { |
||
| 316 | 4 | $this->ensureLength(4); |
|
| 317 | |||
| 318 | 4 | $num = \substr($this->buffer, $this->offset, 4); |
|
| 319 | 4 | $this->offset += 4; |
|
| 320 | |||
| 321 | 4 | $num = \unpack('i', \strrev($num)); |
|
| 322 | |||
| 323 | 4 | return $num[1]; |
|
| 324 | } |
||
| 325 | |||
| 326 | 4 | View Code Duplication | private function unpackInt64() |
| 327 | { |
||
| 328 | 4 | $this->ensureLength(8); |
|
| 329 | |||
| 330 | 4 | $num = \substr($this->buffer, $this->offset, 8); |
|
| 331 | 4 | $this->offset += 8; |
|
| 332 | |||
| 333 | 4 | $set = \unpack('N2', $num); |
|
| 334 | |||
| 335 | 4 | return $set[1] << 32 | $set[2]; |
|
| 336 | } |
||
| 337 | |||
| 338 | 2 | private function unpackFloat32() |
|
| 339 | { |
||
| 340 | 2 | $this->ensureLength(4); |
|
| 341 | |||
| 342 | 2 | $num = \substr($this->buffer, $this->offset, 4); |
|
| 343 | 2 | $this->offset += 4; |
|
| 344 | |||
| 345 | 2 | $num = \unpack('f', \strrev($num)); |
|
| 346 | |||
| 347 | 2 | return $num[1]; |
|
| 348 | } |
||
| 349 | |||
| 350 | 3 | private function unpackFloat64() |
|
| 351 | { |
||
| 352 | 3 | $this->ensureLength(8); |
|
| 353 | |||
| 354 | 3 | $num = \substr($this->buffer, $this->offset, 8); |
|
| 355 | 3 | $this->offset += 8; |
|
| 356 | |||
| 357 | 3 | $num = \unpack('d', \strrev($num)); |
|
| 358 | |||
| 359 | 3 | return $num[1]; |
|
| 360 | } |
||
| 361 | |||
| 362 | 25 | private function unpackStr($length) |
|
| 375 | |||
| 376 | 9 | View Code Duplication | private function unpackArray($size) |
| 385 | |||
| 386 | 14 | View Code Duplication | private function unpackMap($size) |
| 395 | |||
| 396 | 10 | private function unpackExt($length) |
|
| 411 | |||
| 412 | 97 | private function ensureLength($length) |
|
| 418 | |||
| 419 | 3 | private function handleIntOverflow($value) |
|
| 430 | } |
||
| 431 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.