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 |
||
| 20 | class BufferUnpacker |
||
| 21 | { |
||
| 22 | private $buffer; |
||
| 23 | private $offset = 0; |
||
| 24 | private $isBigIntAsStr; |
||
| 25 | private $isBigIntAsGmp; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var Extension[]|null |
||
| 29 | */ |
||
| 30 | private $transformers; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param string $buffer |
||
| 34 | * @param UnpackOptions|int|null $options |
||
| 35 | * |
||
| 36 | * @throws InvalidOptionException |
||
| 37 | */ |
||
| 38 | 123 | public function __construct($buffer = '', $options = null) |
|
| 49 | |||
| 50 | public function registerTransformer(Extension $ext) |
||
| 51 | { |
||
| 52 | $this->transformers[$ext->getType()] = $ext; |
||
| 53 | 2 | } |
|
| 54 | |||
| 55 | 2 | /** |
|
| 56 | 2 | * @param string $data |
|
| 57 | * |
||
| 58 | * @return $this |
||
| 59 | */ |
||
| 60 | public function append($data) |
||
| 61 | 1 | { |
|
| 62 | $this->buffer .= $data; |
||
| 63 | 1 | ||
| 64 | return $this; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string $buffer |
||
| 69 | * |
||
| 70 | * @return $this |
||
| 71 | 5 | */ |
|
| 72 | public function reset($buffer = '') |
||
| 73 | 5 | { |
|
| 74 | $this->buffer = $buffer; |
||
| 75 | 5 | $this->offset = 0; |
|
| 76 | |||
| 77 | return $this; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param int $length |
||
| 82 | * |
||
| 83 | 109 | * @return $this |
|
| 84 | */ |
||
| 85 | 109 | public function skip($length) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @return array |
||
| 94 | 3 | */ |
|
| 95 | public function tryUnpack() |
||
| 96 | 3 | { |
|
| 97 | 3 | $data = []; |
|
| 98 | $offset = $this->offset; |
||
| 99 | |||
| 100 | try { |
||
| 101 | 3 | do { |
|
| 102 | 3 | $data[] = $this->unpack(); |
|
| 103 | 3 | $offset = $this->offset; |
|
| 104 | 1 | } while (isset($this->buffer[$this->offset])); |
|
| 105 | 1 | } catch (InsufficientDataException $e) { |
|
| 106 | $this->offset = $offset; |
||
| 107 | } |
||
| 108 | 3 | ||
| 109 | 3 | if ($this->offset) { |
|
| 110 | 3 | $this->buffer = isset($this->buffer[$this->offset]) ? \substr($this->buffer, $this->offset) : ''; |
|
| 111 | $this->offset = 0; |
||
| 112 | } |
||
| 113 | 3 | ||
| 114 | return $data; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @throws UnpackingFailedException |
||
| 119 | * |
||
| 120 | * @return mixed |
||
| 121 | 118 | */ |
|
| 122 | public function unpack() |
||
| 123 | 118 | { |
|
| 124 | 4 | if (!isset($this->buffer[$this->offset])) { |
|
| 125 | throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 1); |
||
| 126 | } |
||
| 127 | 116 | ||
| 128 | 116 | $c = \ord($this->buffer[$this->offset]); |
|
| 129 | ++$this->offset; |
||
| 130 | |||
| 131 | 116 | // fixint |
|
| 132 | 20 | if ($c <= 0x7f) { |
|
| 133 | return $c; |
||
| 134 | } |
||
| 135 | 113 | // fixstr |
|
| 136 | 13 | if ($c >= 0xa0 && $c <= 0xbf) { |
|
| 137 | return ($c & 0x1f) ? $this->unpackStr($c & 0x1f) : ''; |
||
| 138 | } |
||
| 139 | 107 | // fixarray |
|
| 140 | 6 | if ($c >= 0x90 && $c <= 0x9f) { |
|
| 141 | return ($c & 0xf) ? $this->unpackArray($c & 0xf) : []; |
||
| 142 | } |
||
| 143 | 104 | // fixmap |
|
| 144 | 11 | if ($c >= 0x80 && $c <= 0x8f) { |
|
| 145 | return ($c & 0xf) ? $this->unpackMap($c & 0xf) : []; |
||
| 146 | } |
||
| 147 | 100 | // negfixint |
|
| 148 | 4 | if ($c >= 0xe0) { |
|
| 149 | return $c - 256; |
||
| 150 | } |
||
| 151 | 96 | ||
| 152 | 96 | switch ($c) { |
|
| 153 | 95 | case 0xc0: return null; |
|
| 154 | 93 | case 0xc2: return false; |
|
| 155 | case 0xc3: return true; |
||
| 156 | |||
| 157 | 88 | // MP_BIN |
|
| 158 | 84 | case 0xc4: return $this->unpackStr($this->unpackUint8()); |
|
| 159 | 83 | case 0xc5: return $this->unpackStr($this->unpackUint16()); |
|
| 160 | case 0xc6: return $this->unpackStr($this->unpackUint32()); |
||
| 161 | |||
| 162 | 82 | // MP_FLOAT |
|
| 163 | 79 | case 0xca: return $this->unpackFloat32(); |
|
| 164 | case 0xcb: return $this->unpackFloat64(); |
||
| 165 | |||
| 166 | 75 | // MP_UINT |
|
| 167 | 71 | case 0xcc: return $this->unpackUint8(); |
|
| 168 | 66 | case 0xcd: return $this->unpackUint16(); |
|
| 169 | 62 | case 0xce: return $this->unpackUint32(); |
|
| 170 | case 0xcf: return $this->unpackUint64(); |
||
| 171 | |||
| 172 | 52 | // MP_INT |
|
| 173 | 47 | case 0xd0: return $this->unpackInt8(); |
|
| 174 | 42 | case 0xd1: return $this->unpackInt16(); |
|
| 175 | 37 | case 0xd2: return $this->unpackInt32(); |
|
| 176 | case 0xd3: return $this->unpackInt64(); |
||
| 177 | |||
| 178 | 32 | // MP_STR |
|
| 179 | 28 | case 0xd9: return $this->unpackStr($this->unpackUint8()); |
|
| 180 | 26 | case 0xda: return $this->unpackStr($this->unpackUint16()); |
|
| 181 | case 0xdb: return $this->unpackStr($this->unpackUint32()); |
||
| 182 | |||
| 183 | 25 | // MP_ARRAY |
|
| 184 | 23 | case 0xdc: return $this->unpackArray($this->unpackUint16()); |
|
| 185 | case 0xdd: return $this->unpackArray($this->unpackUint32()); |
||
| 186 | |||
| 187 | 22 | // MP_MAP |
|
| 188 | 20 | case 0xde: return $this->unpackMap($this->unpackUint16()); |
|
| 189 | case 0xdf: return $this->unpackMap($this->unpackUint32()); |
||
| 190 | |||
| 191 | 19 | // MP_EXT |
|
| 192 | 16 | case 0xd4: return $this->unpackExt(1); |
|
| 193 | 14 | case 0xd5: return $this->unpackExt(2); |
|
| 194 | 12 | case 0xd6: return $this->unpackExt(4); |
|
| 195 | 10 | case 0xd7: return $this->unpackExt(8); |
|
| 196 | 8 | case 0xd8: return $this->unpackExt(16); |
|
| 197 | 5 | case 0xc7: return $this->unpackExt($this->unpackUint8()); |
|
| 198 | 3 | case 0xc8: return $this->unpackExt($this->unpackUint16()); |
|
| 199 | case 0xc9: return $this->unpackExt($this->unpackUint32()); |
||
| 200 | } |
||
| 201 | 1 | ||
| 202 | throw new UnpackingFailedException(\sprintf('Unknown code: 0x%x.', $c)); |
||
| 203 | } |
||
| 204 | 18 | ||
| 205 | public function unpackUint8() |
||
| 213 | 16 | ||
| 214 | public function unpackUint16() |
||
| 215 | { |
||
| 216 | 15 | if (!isset($this->buffer[$this->offset + 1])) { |
|
| 217 | throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 2); |
||
| 218 | 15 | } |
|
| 226 | 13 | ||
| 227 | View Code Duplication | public function unpackUint32() |
|
| 240 | 9 | ||
| 241 | public function unpackUint64() |
||
| 261 | 9 | ||
| 262 | public function unpackInt8() |
||
| 273 | 14 | ||
| 274 | 3 | public function unpackInt16() |
|
| 290 | 4 | ||
| 291 | 3 | View Code Duplication | public function unpackInt32() |
| 304 | 4 | ||
| 305 | View Code Duplication | public function unpackInt64() |
|
| 318 | 4 | ||
| 319 | View Code Duplication | public function unpackFloat32() |
|
| 332 | 2 | ||
| 333 | View Code Duplication | public function unpackFloat64() |
|
| 346 | 3 | ||
| 347 | public function unpackStr($length) |
||
| 358 | |||
| 359 | 25 | View Code Duplication | public function unpackArrayLength() |
| 375 | 13 | ||
| 376 | public function unpackArray($size) |
||
| 385 | 15 | ||
| 386 | View Code Duplication | public function unpackMapLength() |
|
| 402 | |||
| 403 | 5 | public function unpackMap($size) |
|
| 412 | 1 | ||
| 413 | public function unpackExt($length) |
||
| 434 | |||
| 435 | private function handleIntOverflow($value) |
||
| 446 | } |
||
| 447 |
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.