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 | 96 | public function __construct($intOverflowMode = null) |
|
49 | { |
||
50 | 96 | if (null !== $intOverflowMode) { |
|
51 | 2 | $this->intOverflowMode = $intOverflowMode; |
|
52 | 2 | } |
|
53 | 96 | } |
|
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() |
|
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() |
|
93 | { |
||
94 | 1 | return $this->intOverflowMode; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param string $data |
||
99 | * |
||
100 | * @return $this |
||
101 | */ |
||
102 | 5 | public function append($data) |
|
108 | |||
109 | /** |
||
110 | * @param string|null $buffer |
||
111 | * |
||
112 | * @return $this |
||
113 | */ |
||
114 | 88 | public function reset($buffer = null) |
|
121 | |||
122 | /** |
||
123 | * @return array |
||
124 | */ |
||
125 | 3 | public function tryUnpack() |
|
146 | |||
147 | /** |
||
148 | * @return mixed |
||
149 | * |
||
150 | * @throws UnpackingFailedException |
||
151 | */ |
||
152 | 92 | public function unpack() |
|
153 | { |
||
154 | 92 | $this->ensureLength(1); |
|
155 | |||
156 | 90 | $c = ord($this->buffer[$this->offset]); |
|
157 | 90 | $this->offset += 1; |
|
158 | |||
159 | // fixint |
||
160 | 90 | if ($c <= 0x7f) { |
|
161 | 19 | return $c; |
|
162 | } |
||
163 | // fixstr |
||
164 | 87 | if ($c >= 0xa0 && $c <= 0xbf) { |
|
165 | 12 | return $this->unpackStr($c & 0x1f); |
|
166 | } |
||
167 | // fixarray |
||
168 | 82 | if ($c >= 0x90 && $c <= 0x9f) { |
|
169 | 6 | return $this->unpackArray($c & 0xf); |
|
170 | } |
||
171 | // fixmap |
||
172 | 79 | if ($c >= 0x80 && $c <= 0x8f) { |
|
173 | 10 | return $this->unpackMap($c & 0xf); |
|
174 | } |
||
175 | // negfixint |
||
176 | 75 | if ($c >= 0xe0) { |
|
177 | 3 | return $c - 256; |
|
178 | } |
||
179 | |||
180 | switch ($c) { |
||
181 | 72 | case 0xc0: return null; |
|
182 | 71 | case 0xc2: return false; |
|
183 | 69 | case 0xc3: return true; |
|
184 | |||
185 | // MP_BIN |
||
186 | 64 | case 0xc4: return $this->unpackStr($this->unpackU8()); |
|
187 | 60 | case 0xc5: return $this->unpackStr($this->unpackU16()); |
|
188 | 59 | case 0xc6: return $this->unpackStr($this->unpackU32()); |
|
189 | |||
190 | 58 | case 0xca: return $this->unpackFloat(); |
|
191 | 56 | case 0xcb: return $this->unpackDouble(); |
|
192 | |||
193 | // MP_UINT |
||
194 | 53 | case 0xcc: return $this->unpackU8(); |
|
195 | 50 | case 0xcd: return $this->unpackU16(); |
|
196 | 46 | case 0xce: return $this->unpackU32(); |
|
197 | 43 | case 0xcf: return $this->unpackU64(); |
|
198 | |||
199 | // MP_INT |
||
200 | 36 | case 0xd0: return $this->unpackI8(); |
|
201 | 33 | case 0xd1: return $this->unpackI16(); |
|
202 | 30 | case 0xd2: return $this->unpackI32(); |
|
203 | 27 | case 0xd3: return $this->unpackI64(); |
|
204 | |||
205 | // MP_STR |
||
206 | 24 | case 0xd9: return $this->unpackStr($this->unpackU8()); |
|
207 | 20 | case 0xda: return $this->unpackStr($this->unpackU16()); |
|
208 | 18 | case 0xdb: return $this->unpackStr($this->unpackU32()); |
|
209 | |||
210 | // MP_ARRAY |
||
211 | 17 | case 0xdc: return $this->unpackArray($this->unpackU16()); |
|
212 | 15 | case 0xdd: return $this->unpackArray($this->unpackU32()); |
|
213 | |||
214 | // MP_MAP |
||
215 | 14 | case 0xde: return $this->unpackMap($this->unpackU16()); |
|
216 | 12 | case 0xdf: return $this->unpackMap($this->unpackU32()); |
|
217 | |||
218 | // MP_EXT |
||
219 | 11 | case 0xd4: return $this->unpackExt(1); |
|
220 | 9 | case 0xd5: return $this->unpackExt(2); |
|
221 | 8 | case 0xd6: return $this->unpackExt(4); |
|
222 | 7 | case 0xd7: return $this->unpackExt(8); |
|
223 | 6 | case 0xd8: return $this->unpackExt(16); |
|
224 | 5 | case 0xc7: return $this->unpackExt($this->unpackU8()); |
|
225 | 3 | case 0xc8: return $this->unpackExt($this->unpackU16()); |
|
226 | 2 | case 0xc9: return $this->unpackExt($this->unpackU32()); |
|
227 | } |
||
228 | |||
229 | 1 | throw new UnpackingFailedException(sprintf('Unknown code: 0x%x.', $c)); |
|
230 | } |
||
231 | |||
232 | 16 | private function unpackU8() |
|
243 | |||
244 | 13 | View Code Duplication | private function unpackU16() |
255 | |||
256 | 9 | View Code Duplication | private function unpackU32() |
267 | |||
268 | 7 | private function unpackU64() |
|
286 | |||
287 | 13 | private function unpackI8() |
|
298 | |||
299 | 3 | View Code Duplication | private function unpackI16() |
310 | |||
311 | 3 | private function unpackI32() |
|
322 | |||
323 | 3 | View Code Duplication | private function unpackI64() |
334 | |||
335 | 2 | private function unpackFloat() |
|
346 | |||
347 | 3 | private function unpackDouble() |
|
358 | |||
359 | 25 | private function unpackStr($length) |
|
372 | |||
373 | 9 | private function unpackArray($size) |
|
383 | |||
384 | 13 | private function unpackMap($size) |
|
397 | |||
398 | 10 | private function unpackExt($length) |
|
413 | |||
414 | 92 | private function ensureLength($length) |
|
420 | |||
421 | 3 | private function handleIntOverflow($value) |
|
422 | { |
||
423 | 3 | if (self::INT_AS_STR === $this->intOverflowMode) { |
|
432 | } |
||
433 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.