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 Core 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 Core, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class Core |
||
| 35 | { |
||
| 36 | /** @type integer HASH_LEN The length of md5() hash string */ |
||
| 37 | const HASH_LEN = 16; |
||
| 38 | |||
| 39 | |||
| 40 | /** |
||
| 41 | * Constructor |
||
| 42 | * |
||
| 43 | */ |
||
| 44 | protected function __construct() |
||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * Destructor |
||
| 52 | * |
||
| 53 | */ |
||
| 54 | protected function __destruct() |
||
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | * Convert hexidecimal to a binary string (ex: "00110110") |
||
| 62 | * |
||
| 63 | * @param string $hex A string containing a hexidecimal number |
||
| 64 | * @return string A string representation of a binary |
||
| 65 | */ |
||
| 66 | public static function hex2Bin($hex) |
||
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * Convert hex to a string |
||
| 86 | * |
||
| 87 | * @param string $hex A string representation of Hex (IE: "1a2b3c" not 0x1a2b3c) |
||
| 88 | * @return string a string |
||
| 89 | */ |
||
| 90 | public static function hex2Str($hex) |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * Converts Hex to Decimal |
||
| 109 | * This function just calls php's hexdec() function, but I |
||
| 110 | * encapsulated it in this function to keep things uniform |
||
| 111 | * and have all possible conversion function available in |
||
| 112 | * the Cipher class |
||
| 113 | * |
||
| 114 | * @param string $hex A hex number to convert to decimal |
||
| 115 | * @return integer A decimal number |
||
| 116 | */ |
||
| 117 | public static function hex2Dec($hex) |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * Convert binary string (ie 00110110) to hex |
||
| 125 | * |
||
| 126 | * @param string $bin A binary string |
||
| 127 | * @return string A string representation of hexidecimal number |
||
| 128 | */ |
||
| 129 | public static function bin2Hex($bin) |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Converts a binary representation (ie 01101011) back to a string |
||
| 145 | * |
||
| 146 | * @param string $bin a binary representation string |
||
| 147 | * @return string A string of characters representing the binary |
||
| 148 | */ |
||
| 149 | public static function bin2Str($bin) |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * Convert a binary string (ie: 01101011) to a decimal number |
||
| 158 | * |
||
| 159 | * @param string A string representation of a binary number |
||
| 160 | * @param string $bin |
||
| 161 | * @return integer The number converted from the binary string |
||
| 162 | */ |
||
| 163 | public static function bin2Dec($bin) |
||
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * Convert a string to hex |
||
| 171 | * This function calls the PHP bin2hex(), and is here |
||
| 172 | * for consistency with the other string functions |
||
| 173 | * |
||
| 174 | * @param string $str A string |
||
| 175 | * @return string A string representation of hexidecimal number |
||
| 176 | */ |
||
| 177 | public static function str2Hex($str) |
||
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * Convert a string of characters to a decimal number |
||
| 185 | * |
||
| 186 | * @param string $str The string to convert to decimal |
||
| 187 | * @return integer The integer converted from the string |
||
| 188 | */ |
||
| 189 | public static function str2Dec($str) |
||
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * Converts a string to binary representation (ie 01101011) |
||
| 198 | * |
||
| 199 | * @param string $str A string |
||
| 200 | * @return string A binary representation of the the string |
||
| 201 | */ |
||
| 202 | public static function str2Bin($str) |
||
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * Converts Decimal to Hex |
||
| 217 | * This function just calls php's dechex() function, but I |
||
| 218 | * encapsulated it in this function to keep things uniform |
||
| 219 | * and have all possible conversion function available in |
||
| 220 | * the Cipher class |
||
| 221 | * |
||
| 222 | * The parameter $req_bytes will pad the return hex with NULL (00) |
||
| 223 | * until the hex represents the number of bytes given to $req_bytes |
||
| 224 | * This is because dechex() drops null bytes from the Hex, which may |
||
| 225 | * be needed in some cases |
||
| 226 | * |
||
| 227 | * @param integer $dec A decimal number to convert |
||
| 228 | * @param integer $req_bytes Optional, forces the string to be at least |
||
| 229 | * $req_bytes in size, this is needed because on occasion left most null bytes |
||
| 230 | * are dropped in dechex(), causing the string to have a shorter byte |
||
| 231 | * size than the initial integer. |
||
| 232 | * @return string A hexidecimal representation of the decimal number |
||
| 233 | */ |
||
| 234 | public static function dec2Hex($dec, $req_bytes = 0) |
||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * Converts Decimal to Binary |
||
| 257 | * This function just calls php's decbin() function, but I |
||
| 258 | * encapsulated it in this function to keep things uniform |
||
| 259 | * and have all possible conversion function available in |
||
| 260 | * the Cipher class |
||
| 261 | * |
||
| 262 | * @param integer $dec A decimal number to convert |
||
| 263 | * @param integer $req_bytes Optional, forces the string to be at least |
||
| 264 | * $req_bytes in size, this is needed because on occasion left most null bytes |
||
| 265 | * are dropped in dechex(), causing the string to have a shorter byte |
||
| 266 | * size than the initial integer. |
||
| 267 | * @return string A binary representation of the decimal number |
||
| 268 | */ |
||
| 269 | public static function dec2Bin($dec, $req_bytes = 0) |
||
| 274 | |||
| 275 | |||
| 276 | /** |
||
| 277 | * Convert a decimal to a string of bytes |
||
| 278 | * |
||
| 279 | * @param integer $dec A decimal number |
||
| 280 | * @param integer $req_bytes Optional, forces the string to be at least |
||
| 281 | * $req_bytes in size, this is needed because on occasion left most null bytes |
||
| 282 | * are dropped in dechex(), causing the string to have a shorter byte |
||
| 283 | * size than the initial integer. |
||
| 284 | * @return string A string with the number of bytes equal to $dec |
||
| 285 | */ |
||
| 286 | public static function dec2Str($dec, $req_bytes = 0) |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * XORs two binary strings (representation of binary, ie 01101011), |
||
| 295 | * assumed to be equal length |
||
| 296 | * |
||
| 297 | * @param string $a A string that represents binary |
||
| 298 | * @param string $b A string that represents binary |
||
| 299 | * @return string A representation of binary |
||
| 300 | */ |
||
| 301 | public static function xorBin($a, $b) |
||
| 324 | |||
| 325 | |||
| 326 | /** |
||
| 327 | * ExclusiveOR hex values. Supports an unlimited number of parameters. |
||
| 328 | * The values are string representations of hex values |
||
| 329 | * IE: "0a1b2c3d" not 0x0a1b2c3d |
||
| 330 | * |
||
| 331 | * @param string Unlimited number parameters, each a string representation of hex |
||
| 332 | * @return string A string representation of the result in Hex |
||
| 333 | */ |
||
| 334 | public static function xorHex() |
||
| 385 | |||
| 386 | |||
| 387 | /** |
||
| 388 | * Forces an integer to be signed |
||
| 389 | * |
||
| 390 | * @param integer $int An integer |
||
| 391 | * @return integer An signed integer |
||
| 392 | */ |
||
| 393 | public static function sInt($int) |
||
| 398 | |||
| 399 | |||
| 400 | /** |
||
| 401 | * Forces an integer to be unsigned |
||
| 402 | * |
||
| 403 | * @param integer $int A signed integer |
||
| 404 | * @return integer An unsigned integer |
||
| 405 | */ |
||
| 406 | public static function uInt($int) |
||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * Forces an integer to be a 32 bit signed integer |
||
| 427 | * |
||
| 428 | * @param integer $int An integer |
||
| 429 | * @return integer An signed 32 bit integer |
||
| 430 | */ |
||
| 431 | View Code Duplication | public static function sInt32($int) |
|
| 442 | |||
| 443 | |||
| 444 | /** |
||
| 445 | * Force an integer to be a 32 bit unsigned integer |
||
| 446 | * |
||
| 447 | * @param integer $int An integer |
||
| 448 | * @return integer An unsigned 32 bit integer |
||
| 449 | */ |
||
| 450 | View Code Duplication | public static function uInt32($int) |
|
| 461 | |||
| 462 | |||
| 463 | /** |
||
| 464 | * Converts an integer to the value for an signed char |
||
| 465 | * |
||
| 466 | * @param integer $int The integer to convert to a signed char |
||
| 467 | * @return integer A signed integer, representing a signed char |
||
| 468 | */ |
||
| 469 | public static function sChar($int) |
||
| 474 | |||
| 475 | |||
| 476 | /** |
||
| 477 | * Converts an integer to the value for an unsigned char |
||
| 478 | * |
||
| 479 | * @param integer $int The integer to convert to a unsigned char |
||
| 480 | * @return integer An unsigned integer, representing a unsigned char |
||
| 481 | */ |
||
| 482 | public static function uChar($int) |
||
| 487 | |||
| 488 | |||
| 489 | /** |
||
| 490 | * Rotates bits Left, appending the bits pushed off the left onto the right |
||
| 491 | * |
||
| 492 | * @param integer $shifts The number of shifts left to make |
||
| 493 | * @return integer The resulting value from the rotation |
||
| 494 | */ |
||
| 495 | public static function rotBitsLeft32($i, $shifts) |
||
| 513 | |||
| 514 | |||
| 515 | /** |
||
| 516 | * Rotates bits right, appending the bits pushed off the right onto the left |
||
| 517 | * |
||
| 518 | * @param integer $shifts The number of shifts right to make |
||
| 519 | * @return integer The resulting value from the rotation |
||
| 520 | */ |
||
| 521 | public static function rotBitsRight32($i, $shifts) |
||
| 539 | |||
| 540 | |||
| 541 | /** |
||
| 542 | * Create a string of random bytes, used for creating an IV |
||
| 543 | * and a random key. See PHP_Crypt::createKey() and PHP_Crypt::createIV() |
||
| 544 | * There are 4 ways to auto generate random bytes by setting $src parameter |
||
| 545 | * PHP_Crypt::RAND - Default, uses mt_rand() |
||
| 546 | * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
||
| 547 | * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
||
| 548 | * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
||
| 549 | * |
||
| 550 | * @param string $src Optional, Use the $src to create the random bytes |
||
| 551 | * by default PHP_Crypt::RAND is used when $src is not specified |
||
| 552 | * @param integer $byte_len The length of the byte string to create |
||
| 553 | * @return string A random string of bytes |
||
| 554 | */ |
||
| 555 | public static function randBytes($src = PHP_Crypt::RAND, $byte_len = PHP_Crypt::RAND_DEFAULT_SZ) |
||
| 635 | } |
||
| 636 | ?> |
||
| 637 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.