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 Binary 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 Binary, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Binary |
||
| 13 | { |
||
| 14 | use \PHPDaemon\Traits\ClassWatchdog; |
||
| 15 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Build structure of labels |
||
| 19 | * @param string $q Dot-separated labels list |
||
| 20 | * @return string |
||
| 21 | */ |
||
| 22 | public static function labels($q) |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Parse structure of labels |
||
| 37 | * @param string &$data Binary data |
||
| 38 | * @param string $orig Original packet |
||
|
|
|||
| 39 | * @return string Dot-separated labels list |
||
| 40 | */ |
||
| 41 | public static function parseLabels(&$data, $orig = null) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Convert bytes into integer |
||
| 66 | * @param string $str Bytes |
||
| 67 | * @param boolean $l Little endian? Default is false |
||
| 68 | * @return integer |
||
| 69 | */ |
||
| 70 | View Code Duplication | public static function bytes2int($str, $l = false) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Build nul-terminated string, with 2-byte of length |
||
| 85 | * @param string $str Data |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | public static function LVnull($str) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Build length-value binary snippet |
||
| 95 | * @param string $str Data |
||
| 96 | * @param integer $len Number of bytes to encode length. Default is 1 |
||
| 97 | * @param boolean $lrev Reverse? |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | public static function LV($str, $len = 1, $lrev = false) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Converts integer to binary string |
||
| 111 | * @alias Binary::int2bytes |
||
| 112 | * @param integer $len Length |
||
| 113 | * @param integer $int Integer |
||
| 114 | * @param boolean $l Optional. Little endian. Default value - false |
||
| 115 | * @return string Resulting binary string |
||
| 116 | */ |
||
| 117 | public static function i2b($len, $int = 0, $l = false) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Converts integer to binary string |
||
| 124 | * @param integer $len Length |
||
| 125 | * @param integer $int Integer |
||
| 126 | * @param boolean $l Optional. Little endian. Default value - false |
||
| 127 | * @return string Resulting binary string |
||
| 128 | */ |
||
| 129 | View Code Duplication | public static function int2bytes($len, $int = 0, $l = false) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Build byte |
||
| 153 | * @param integer $int Byte number |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | public static function byte($int) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Build word (2 bytes) little-endian |
||
| 163 | * @param integer $int Integer |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public static function wordl($int) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Build word (2 bytes) big-endian |
||
| 173 | * @param integer $int Integer |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | public static function word($int) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Build double word (4 bytes) little endian |
||
| 183 | * @param integer $int Integer |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public static function dwordl($int) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Build double word (4 bytes) big-endian |
||
| 193 | * @param integer $int Integer |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public static function dword($int) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Build quadro word (8 bytes) little endian |
||
| 203 | * @param integer $int Integer |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public static function qwordl($int) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Build quadro word (8 bytes) big endian |
||
| 213 | * @param integer $int Integer |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | public static function qword($int) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Parse byte, and remove it |
||
| 223 | * @param string &$p Data |
||
| 224 | * @return integer |
||
| 225 | */ |
||
| 226 | public static function getByte(&$p) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Get single-byte character |
||
| 235 | * @param string &$p Data |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | public static function getChar(&$p) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Parse word (2 bytes) |
||
| 247 | * @param string &$p Data |
||
| 248 | * @param boolean $l Little endian? |
||
| 249 | * @return integer |
||
| 250 | */ |
||
| 251 | public static function getWord(&$p, $l = false) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get word (2 bytes) |
||
| 260 | * @param string &$p Data |
||
| 261 | * @param boolean $l Little endian? |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | View Code Duplication | public static function getStrWord(&$p, $l = false) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Get double word (4 bytes) |
||
| 276 | * @param string &$p Data |
||
| 277 | * @param boolean $l Little endian? |
||
| 278 | * @return integer |
||
| 279 | */ |
||
| 280 | public static function getDWord(&$p, $l = false) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Parse quadro word (8 bytes) |
||
| 289 | * @param string &$p Data |
||
| 290 | * @param boolean $l Little endian? |
||
| 291 | * @return integer |
||
| 292 | */ |
||
| 293 | public static function getQword(&$p, $l = false) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get quadro word (8 bytes) |
||
| 302 | * @param string &$p Data |
||
| 303 | * @param boolean $l Little endian? |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | View Code Duplication | public static function getStrQWord(&$p, $l = false) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Parse nul-terminated string |
||
| 318 | * @param string &$str Data |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | View Code Duplication | public static function getString(&$str) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Parse length-value structure |
||
| 334 | * @param string &$p Data |
||
| 335 | * @param integer $l Number of length bytes |
||
| 336 | * @param boolean $nul Nul-terminated? Default is false |
||
| 337 | * @param boolean $lrev Length is little endian? |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | public static function getLV(&$p, $l = 1, $nul = false, $lrev = false) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Convert bytes into integer |
||
| 372 | * @alias Binary::bytes2int |
||
| 373 | * @param string $str Bytes |
||
| 374 | * @param boolean $l Little endian? Default is false |
||
| 375 | * @return integer |
||
| 376 | */ |
||
| 377 | public static function b2i($str = 0, $l = false) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Convert array of flags into bit array |
||
| 384 | * @param array $flags Flags |
||
| 385 | * @param integer $len Length. Default is 4 |
||
| 386 | * @return string |
||
| 387 | */ |
||
| 388 | public static function flags2bitarray($flags, $len = 4) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Convert bitmap into bytes |
||
| 399 | * @param string $bitmap Bitmap |
||
| 400 | * @param integer $check_len Check length? |
||
| 401 | * @return string|false |
||
| 402 | */ |
||
| 403 | public static function bitmap2bytes($bitmap, $check_len = 0) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Get bitmap |
||
| 418 | * @param integer $byte Byte |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | public static function getbitmap($byte) |
||
| 425 | } |
||
| 426 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.