Complex classes like BigInteger 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 BigInteger, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 14 | final class BigInteger | ||
| 15 | { | ||
| 16 | /** | ||
| 17 | * Holds the BigInteger's value. | ||
| 18 | * | ||
| 19 | * @var resource | ||
| 20 | */ | ||
| 21 | private $value; | ||
| 22 | |||
| 23 | /** | ||
| 24 | * Holds the BigInteger's magnitude. | ||
| 25 | * | ||
| 26 | * @var bool | ||
| 27 | */ | ||
| 28 | private $is_negative = false; | ||
| 29 | |||
| 30 | /** | ||
| 31 | * Precision. | ||
| 32 | */ | ||
| 33 | private $precision = -1; | ||
| 34 | |||
| 35 | /** | ||
| 36 | * Precision Bitmask. | ||
| 37 | */ | ||
| 38 | private $bitmask = false; | ||
| 39 | |||
| 40 | /** | ||
| 41 | * Converts base-2, base-10, base-16, and binary strings (base-256) to BigIntegers. | ||
| 42 | * | ||
| 43 | * If the second parameter - $base - is negative, then it will be assumed that the number's are encoded using | ||
| 44 | * two's compliment. The sole exception to this is -10, which is treated the same as 10 is. | ||
| 45 | * | ||
| 46 | * Here's an example: | ||
| 47 | * <code> | ||
| 48 | * <?php | ||
| 49 | * $a = new \Jose\Util\in base-16 | ||
| 50 | * | ||
| 51 | * echo $a->toString(); // outputs 50 | ||
| 52 | * ?> | ||
| 53 | * </code> | ||
| 54 | * | ||
| 55 | * @param $x base-10 number or base-$base number if $base set. | ||
| 56 | * @param int $base | ||
| 57 | */ | ||
| 58 | public function __construct($x = 0, $base = 10) | ||
| 153 | |||
| 154 | /** | ||
| 155 | * Converts a BigInteger to a byte string (eg. base-256). | ||
| 156 | * | ||
| 157 | * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're | ||
| 158 | * saved as two's compliment. | ||
| 159 | * | ||
| 160 | * Here's an example: | ||
| 161 | * <code> | ||
| 162 | * <?php | ||
| 163 |      *    $a = new \Jose\Util\ger('65'); | ||
| 164 | * | ||
| 165 | * echo $a->toBytes(); // outputs chr(65) | ||
| 166 | * ?> | ||
| 167 | * </code> | ||
| 168 | * | ||
| 169 | * @param bool $twos_compliment | ||
| 170 | * | ||
| 171 | * @return string | ||
| 172 | * | ||
| 173 | */ | ||
| 174 | public function toBytes($twos_compliment = false) | ||
| 208 | |||
| 209 | /** | ||
| 210 | * Adds two BigIntegers. | ||
| 211 | * | ||
| 212 | * Here's an example: | ||
| 213 | * <code> | ||
| 214 | * <?php | ||
| 215 |      *    $a = new \Jose\Util\ger('10'); | ||
| 216 |      *    $b = new \Jose\Util\ger('20'); | ||
| 217 | * | ||
| 218 | * $c = $a->add($b); | ||
| 219 | * | ||
| 220 | * echo $c->toString(); // outputs 30 | ||
| 221 | * ?> | ||
| 222 | * </code> | ||
| 223 | * | ||
| 224 | * @param \Jose\Util\BigInteger $y | ||
| 225 | * | ||
| 226 | * @return \Jose\Util\BigInteger | ||
| 227 | * | ||
| 228 | */ | ||
| 229 | public function add(BigInteger $y) | ||
| 236 | |||
| 237 | /** | ||
| 238 | * Subtracts two BigIntegers. | ||
| 239 | * | ||
| 240 | * Here's an example: | ||
| 241 | * <code> | ||
| 242 | * <?php | ||
| 243 |      *    $a = new \Jose\Util\ger('10'); | ||
| 244 |      *    $b = new \Jose\Util\ger('20'); | ||
| 245 | * | ||
| 246 | * $c = $a->subtract($b); | ||
| 247 | * | ||
| 248 | * echo $c->toString(); // outputs -10 | ||
| 249 | * ?> | ||
| 250 | * </code> | ||
| 251 | * | ||
| 252 | * @param \Jose\Util\BigInteger $y | ||
| 253 | * | ||
| 254 | * @return \Jose\Util\BigInteger | ||
| 255 | * | ||
| 256 | */ | ||
| 257 | public function subtract(BigInteger $y) | ||
| 264 | |||
| 265 | /** | ||
| 266 | * Multiplies two BigIntegers. | ||
| 267 | * | ||
| 268 | * Here's an example: | ||
| 269 | * <code> | ||
| 270 | * <?php | ||
| 271 |      *    $a = new \Jose\Util\ger('10'); | ||
| 272 |      *    $b = new \Jose\Util\ger('20'); | ||
| 273 | * | ||
| 274 | * $c = $a->multiply($b); | ||
| 275 | * | ||
| 276 | * echo $c->toString(); // outputs 200 | ||
| 277 | * ?> | ||
| 278 | * </code> | ||
| 279 | * | ||
| 280 | * @param \Jose\Util\BigInteger $x | ||
| 281 | * | ||
| 282 | * @return \Jose\Util\BigInteger | ||
| 283 | */ | ||
| 284 | public function multiply(BigInteger $x) | ||
| 291 | |||
| 292 | /** | ||
| 293 | * Divides two BigIntegers. | ||
| 294 | * | ||
| 295 | * Returns an array whose first element contains the quotient and whose second element contains the | ||
| 296 | * "common residue". If the remainder would be positive, the "common residue" and the remainder are the | ||
| 297 | * same. If the remainder would be negative, the "common residue" is equal to the sum of the remainder | ||
| 298 | * and the divisor (basically, the "common residue" is the first positive modulo). | ||
| 299 | * | ||
| 300 | * Here's an example: | ||
| 301 | * <code> | ||
| 302 | * <?php | ||
| 303 |      *    $a = new \Jose\Util\ger('10'); | ||
| 304 |      *    $b = new \Jose\Util\ger('20'); | ||
| 305 | * | ||
| 306 | * list($quotient, $remainder) = $a->divide($b); | ||
| 307 | * | ||
| 308 | * echo $quotient->toString(); // outputs 0 | ||
| 309 | * echo "\r\n"; | ||
| 310 | * echo $remainder->toString(); // outputs 10 | ||
| 311 | * ?> | ||
| 312 | * </code> | ||
| 313 | * @param \Jose\Util\BigInteger $y | ||
| 314 | * | ||
| 315 | * @return \Jose\Util\BigInteger[] | ||
| 316 | * | ||
| 317 | */ | ||
| 318 | public function divide(BigInteger $y) | ||
| 331 | |||
| 332 | /** | ||
| 333 | * Performs modular exponentiation. | ||
| 334 | * | ||
| 335 | * Here's an example: | ||
| 336 | * <code> | ||
| 337 | * <?php | ||
| 338 |      *    $a = new \Jose\Util\ger('10'); | ||
| 339 |      *    $b = new \Jose\Util\ger('20'); | ||
| 340 |      *    $c = new \Jose\Util\ger('30'); | ||
| 341 | * | ||
| 342 | * $c = $a->modPow($b, $c); | ||
| 343 | * | ||
| 344 | * echo $c->toString(); // outputs 10 | ||
| 345 | * ?> | ||
| 346 | * </code> | ||
| 347 | * | ||
| 348 | * @param \Jose\Util\BigInteger $e | ||
| 349 | * @param \Jose\Util\BigInteger $n | ||
| 350 | * | ||
| 351 | * @return \Jose\Util\BigInteger | ||
| 352 | * | ||
| 353 | * and although the approach involving repeated squaring does vastly better, it, too, is impractical | ||
| 354 | * for our purposes. The reason being that division - by far the most complicated and time-consuming | ||
| 355 | * of the basic operations (eg. +,-,*,/) - occurs multiple times within it. | ||
| 356 | * | ||
| 357 | * Modular reductions resolve this issue. Although an individual modular reduction takes more time | ||
| 358 | * then an individual division, when performed in succession (with the same modulo), they're a lot faster. | ||
| 359 | * | ||
| 360 | * The two most commonly used modular reductions are Barrett and Montgomery reduction. Montgomery reduction, | ||
| 361 | * although faster, only works when the gcd of the modulo and of the base being used is 1. In RSA, when the | ||
| 362 | * base is a power of two, the modulo - a product of two primes - is always going to have a gcd of 1 (because | ||
| 363 | * the product of two odd numbers is odd), but what about when RSA isn't used? | ||
| 364 | * | ||
| 365 | * In contrast, Barrett reduction has no such constraint. As such, some bigint implementations perform a | ||
| 366 | * Barrett reduction after every operation in the modpow function. Others perform Barrett reductions when the | ||
| 367 | * modulo is even and Montgomery reductions when the modulo is odd. BigInteger.java's modPow method, however, | ||
| 368 | * uses a trick involving the Chinese Remainder Theorem to factor the even modulo into two numbers - one odd and | ||
| 369 | * the other, a power of two - and recombine them, later. This is the method that this modPow function uses. | ||
| 370 |      *    {@link http://islab.oregonstate.edu/papers/j34monex.pdf Montgomery Reduction with Even Modulus} elaborates. | ||
| 371 | */ | ||
| 372 | public function modPow(BigInteger $e, BigInteger $n) | ||
| 392 | |||
| 393 | /** | ||
| 394 | * Calculates modular inverses. | ||
| 395 | * | ||
| 396 | * Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses. | ||
| 397 | * | ||
| 398 | * Here's an example: | ||
| 399 | * <code> | ||
| 400 | * <?php | ||
| 401 | * $a = new \Jose\Util\teger(30); | ||
| 402 | * $b = new \Jose\Util\teger(17); | ||
| 403 | * | ||
| 404 | * $c = $a->modInverse($b); | ||
| 405 | * echo $c->toString(); // outputs 4 | ||
| 406 | * | ||
| 407 | * echo "\r\n"; | ||
| 408 | * | ||
| 409 | * $d = $a->multiply($c); | ||
| 410 | * list(, $d) = $d->divide($b); | ||
| 411 | * echo $d; // outputs 1 (as per the definition of modular inverse) | ||
| 412 | * ?> | ||
| 413 | * </code> | ||
| 414 | * | ||
| 415 | * @param \Jose\Util\BigInteger $n | ||
| 416 | * | ||
| 417 | * @return \Jose\Util\BigInteger|bool | ||
| 418 | * | ||
| 419 | */ | ||
| 420 | public function modInverse(BigInteger $n) | ||
| 427 | |||
| 428 | /** | ||
| 429 | * Absolute value. | ||
| 430 | * | ||
| 431 | * @return \Jose\Util\BigInteger | ||
| 432 | */ | ||
| 433 | public function abs() | ||
| 441 | |||
| 442 | /** | ||
| 443 | * Compares two numbers. | ||
| 444 | * | ||
| 445 | * Although one might think !$x->compare($y) means $x != $y, it, in fact, means the opposite. The reason for this is | ||
| 446 | * demonstrated thusly: | ||
| 447 | * | ||
| 448 | * $x > $y: $x->compare($y) > 0 | ||
| 449 | * $x < $y: $x->compare($y) < 0 | ||
| 450 | * $x == $y: $x->compare($y) == 0 | ||
| 451 | * | ||
| 452 | * Note how the same comparison operator is used. If you want to test for equality, use $x->equals($y). | ||
| 453 | * | ||
| 454 | * @param \Jose\Util\BigInteger $y | ||
| 455 | * | ||
| 456 | * @return int < 0 if $this is less than $y; > 0 if $this is greater than $y, and 0 if they are equal. | ||
| 457 | * | ||
| 458 | */ | ||
| 459 | public function compare(BigInteger $y) | ||
| 463 | |||
| 464 | /** | ||
| 465 | * Logical Left Shift. | ||
| 466 | * | ||
| 467 | * Shifts BigInteger's by $shift bits, effectively multiplying by 2**$shift. | ||
| 468 | * | ||
| 469 | * @param int $shift | ||
| 470 | * | ||
| 471 | * @return \Jose\Util\BigInteger | ||
| 472 | * | ||
| 473 | */ | ||
| 474 | public function bitwise_leftShift($shift) | ||
| 488 | |||
| 489 | /** | ||
| 490 | * Generates a random BigInteger. | ||
| 491 | * | ||
| 492 | * Byte length is equal to $length. Uses \phpseclib\Crypt\Random if it's loaded and mt_rand if it's not. | ||
| 493 | * | ||
| 494 | * @param int $size | ||
| 495 | * | ||
| 496 | * @return \Jose\Util\BigInteger | ||
| 497 | */ | ||
| 498 | private static function _random_number_helper($size) | ||
| 502 | |||
| 503 | /** | ||
| 504 | * Generate a random number. | ||
| 505 | * | ||
| 506 | * Returns a random number between $min and $max where $min and $max | ||
| 507 | * can be defined using one of the two methods: | ||
| 508 | * | ||
| 509 | * BigInteger::random($min, $max) | ||
| 510 | * BigInteger::random($max, $min) | ||
| 511 | * | ||
| 512 | * @param \Jose\Util\BigInteger $min | ||
| 513 | * @param \Jose\Util\BigInteger $max | ||
| 514 | * | ||
| 515 | * @return \Jose\Util\BigInteger | ||
| 516 | */ | ||
| 517 | public static function random(BigInteger $min, BigInteger $max) | ||
| 572 | |||
| 573 | /** | ||
| 574 | * Normalize. | ||
| 575 | * | ||
| 576 | * Removes leading zeros and truncates (if necessary) to maintain the appropriate precision | ||
| 577 | * | ||
| 578 | * @param \Jose\Util\BigInteger $result | ||
| 579 | * | ||
| 580 | * @return \Jose\Util\BigInteger | ||
| 581 | */ | ||
| 582 | private function _normalize($result) | ||
| 593 | } | ||
| 594 | 
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..