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 BinarySupport 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 BinarySupport, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class BinarySupport |
||
8 | { |
||
9 | /** |
||
10 | * Build structure of labels. |
||
11 | * |
||
12 | * @param string $q dot-separated labels list. |
||
13 | * @return string |
||
14 | */ |
||
15 | public static function labels($q) |
||
28 | |||
29 | /** |
||
30 | * Parse structure of labels. |
||
31 | * |
||
32 | * @param string $data |
||
33 | * @param string $orig |
||
34 | * @return string Dot-separated labels list. |
||
35 | */ |
||
36 | public static function parseLabels(&$data, $orig = null) |
||
60 | |||
61 | /** |
||
62 | * Build length-value binary snippet. |
||
63 | * |
||
64 | * @param string $str Data. |
||
65 | * @param int $len Number of bytes to encode length, defaults to 1. |
||
66 | * @return string |
||
67 | */ |
||
68 | public static function LV($str, $len = 1, $lrev = false) |
||
76 | |||
77 | /** |
||
78 | * Build null-terminated string, with 2-byte of length. |
||
79 | * |
||
80 | * @param string $str Data. |
||
81 | * @return string |
||
82 | */ |
||
83 | public static function LVnull($str) |
||
87 | |||
88 | /** |
||
89 | * Build byte. |
||
90 | * |
||
91 | * @param int $int |
||
92 | * @return string |
||
93 | */ |
||
94 | public static function byte($int) |
||
98 | |||
99 | /** |
||
100 | * Build word (2 bytes) big-endian. |
||
101 | * |
||
102 | * @param int $int |
||
103 | * @return string |
||
104 | */ |
||
105 | public static function word($int) |
||
109 | |||
110 | /** |
||
111 | * Build word (2 bytes) little-endian. |
||
112 | * |
||
113 | * @param int $int |
||
114 | * @return string |
||
115 | */ |
||
116 | public static function wordl($int) |
||
120 | |||
121 | /** |
||
122 | * Build double word (4 bytes) big-endian. |
||
123 | * |
||
124 | * @param int $int |
||
125 | * @return string |
||
126 | */ |
||
127 | public static function dword($int) |
||
131 | |||
132 | /** |
||
133 | * Build double word (4 bytes) little endian. |
||
134 | * |
||
135 | * @param int $int |
||
136 | * @return string |
||
137 | */ |
||
138 | public static function dwordl($int) |
||
142 | |||
143 | /** |
||
144 | * Build quadro word (8 bytes) big endian. |
||
145 | * |
||
146 | * @param int $int |
||
147 | * @return string |
||
148 | */ |
||
149 | public static function qword($int) |
||
153 | |||
154 | /** |
||
155 | * Build quadro word (8 bytes) little endian. |
||
156 | * |
||
157 | * @param int $int |
||
158 | * @return string |
||
159 | */ |
||
160 | public static function qwordl($int) |
||
164 | |||
165 | /** |
||
166 | * Parse byte, and remove it. |
||
167 | * |
||
168 | * @param &string $p Data |
||
|
|||
169 | * @return int |
||
170 | */ |
||
171 | public static function getByte(&$p) |
||
178 | |||
179 | /** |
||
180 | * Get single-byte character. |
||
181 | * |
||
182 | * @param &string $p Data |
||
183 | * @return string |
||
184 | */ |
||
185 | public static function getChar(&$p) |
||
192 | |||
193 | /** |
||
194 | * Parse word (2 bytes) |
||
195 | * |
||
196 | * @param &string $p Data. |
||
197 | * @param bool $l Little-endian, defaults to false. |
||
198 | * @return int |
||
199 | */ |
||
200 | View Code Duplication | public static function getWord(&$p, $l = false) |
|
207 | |||
208 | /** |
||
209 | * Get word (2 bytes). |
||
210 | * |
||
211 | * @param &string $p Data |
||
212 | * @param bool $l Little-endian, defaults to false. |
||
213 | * @return string |
||
214 | */ |
||
215 | View Code Duplication | public static function getStrWord(&$p, $l = false) |
|
224 | |||
225 | /** |
||
226 | * Get double word (4 bytes). |
||
227 | * |
||
228 | * @param &string $p Data. |
||
229 | * @param bool $l Little-endian, defaults to false. |
||
230 | * @return int |
||
231 | */ |
||
232 | View Code Duplication | public static function getDWord(&$p, $l = false) |
|
239 | |||
240 | /** |
||
241 | * Parse quadro word (8 bytes). |
||
242 | * |
||
243 | * @param &string $p Data |
||
244 | * @param bool $l Little-endian, defaults to false. |
||
245 | * @return int |
||
246 | */ |
||
247 | View Code Duplication | public static function getQword(&$p, $l = false) |
|
254 | |||
255 | /** |
||
256 | * Get quadro word (8 bytes). |
||
257 | * |
||
258 | * @param &string $p Data. |
||
259 | * @param bool $l Little-endian, defaults to false. |
||
260 | * @return string |
||
261 | */ |
||
262 | View Code Duplication | public static function getStrQWord(&$p, $l = false) |
|
272 | |||
273 | /** |
||
274 | * Parse null-terminated string. |
||
275 | * |
||
276 | * @param &string $str |
||
277 | * @return string |
||
278 | */ |
||
279 | View Code Duplication | public static function getString(&$str) |
|
290 | |||
291 | /** |
||
292 | * Parse length-value structure. |
||
293 | * |
||
294 | * @param &string $p |
||
295 | * @param int $l number of length bytes. |
||
296 | * @param bool $nul Null-terminated, defaults to false. |
||
297 | * @param bool $lrev Little-endian, default to false. |
||
298 | * @return string |
||
299 | */ |
||
300 | public static function getLV(&$p, $l = 1, $nul = false, $lrev = false) |
||
334 | |||
335 | /** |
||
336 | * Converts integer to binary string. |
||
337 | * |
||
338 | * @param int $len |
||
339 | * @param int $int |
||
340 | * @param bool $l Little-endian, defaults to false. |
||
341 | * @return string |
||
342 | */ |
||
343 | public static function int2bytes($len, $int = 0, $l = false) |
||
365 | |||
366 | /** |
||
367 | * Convert array of flags into bit array. |
||
368 | * |
||
369 | * @param array $flags |
||
370 | * @param int $len defaults to 4. |
||
371 | * @return string |
||
372 | */ |
||
373 | public static function flags2bitarray($flags, $len = 4) |
||
382 | |||
383 | /** |
||
384 | * @see BinarySupport::int2bytes |
||
385 | */ |
||
386 | public static function i2b($bytes, $int = 0, $l = false) |
||
390 | |||
391 | /** |
||
392 | * Convert bytes into integer. |
||
393 | * |
||
394 | * @param string $str |
||
395 | * @param bool $l little-endian encoding, defaults to false |
||
396 | * @return int |
||
397 | */ |
||
398 | public static function bytes2int($str, $l = false) |
||
414 | |||
415 | /** |
||
416 | * @see BinarySupport::bytes2int |
||
417 | */ |
||
418 | public static function b2i($hex = 0, $l = false) |
||
422 | |||
423 | /** |
||
424 | * Convert bitmap into bytes. |
||
425 | * |
||
426 | * @param string $bitmap |
||
427 | * @param int $checkLen |
||
428 | * @return string |
||
429 | * @throws Exception |
||
430 | */ |
||
431 | public static function bitmap2bytes($bitmap, $checkLen = 0) |
||
446 | |||
447 | /** |
||
448 | * Get bitmap. |
||
449 | * |
||
450 | * @param string $byte |
||
451 | * @return string |
||
452 | */ |
||
453 | public static function getBitmap($byte) |
||
457 | |||
458 | /** |
||
459 | * Binary Substring. |
||
460 | * |
||
461 | * @param string $s |
||
462 | * @param string $p |
||
463 | * @param int|null $len |
||
464 | * @return string |
||
465 | */ |
||
466 | protected static function binarySubstr($s, $p, $len = null) |
||
480 | } |
||
481 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.