Complex classes like RSAKey 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 RSAKey, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | final class RSAKey |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var Sequence |
||
| 32 | */ |
||
| 33 | private $sequence; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private $values = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var BigInteger |
||
| 42 | */ |
||
| 43 | private $modulus; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var int |
||
| 47 | */ |
||
| 48 | private $modulus_length; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var BigInteger |
||
| 52 | */ |
||
| 53 | private $public_exponent; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var BigInteger|null |
||
| 57 | */ |
||
| 58 | private $private_exponent = null; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var BigInteger[] |
||
| 62 | */ |
||
| 63 | private $primes = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var BigInteger[] |
||
| 67 | */ |
||
| 68 | private $exponents = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var BigInteger|null |
||
| 72 | */ |
||
| 73 | private $coefficient = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param JWK|string|array $data |
||
| 77 | */ |
||
| 78 | private function __construct($data) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param JWK $jwk |
||
| 97 | * |
||
| 98 | * @return RSAKey |
||
| 99 | */ |
||
| 100 | public static function createFromJWK(JWK $jwk): RSAKey |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param string $pem |
||
| 107 | * |
||
| 108 | * @return RSAKey |
||
| 109 | */ |
||
| 110 | public static function createFromPEM(string $pem): RSAKey |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return bool |
||
| 117 | */ |
||
| 118 | public function isPublic(): bool |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | public function isPrivate(): bool |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return BigInteger |
||
| 133 | */ |
||
| 134 | public function getModulus(): BigInteger |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return int |
||
| 141 | */ |
||
| 142 | public function getModulusLength(): int |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return BigInteger |
||
| 149 | */ |
||
| 150 | public function getExponent(): BigInteger |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @return BigInteger |
||
| 162 | */ |
||
| 163 | public function getPublicExponent(): BigInteger |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @return BigInteger|null |
||
| 170 | */ |
||
| 171 | public function getPrivateExponent(): ?BigInteger |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return BigInteger[] |
||
| 178 | */ |
||
| 179 | public function getPrimes(): array |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return BigInteger[] |
||
| 186 | */ |
||
| 187 | public function getExponents(): array |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @return BigInteger|null |
||
| 194 | */ |
||
| 195 | public function getCoefficient(): ?BigInteger |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @param RSAKey $private |
||
| 202 | * |
||
| 203 | * @return RSAKey |
||
| 204 | */ |
||
| 205 | public static function toPublic(RSAKey $private): RSAKey |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public function toArray(): array |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public function toPEM(): string |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param string $data |
||
| 240 | * |
||
| 241 | * @throws \Exception |
||
| 242 | * @throws ParserException |
||
| 243 | */ |
||
| 244 | private function loadPEM(string $data) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param array $jwk |
||
| 276 | */ |
||
| 277 | private function loadJWK(array $jwk) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * This method adds Chinese Remainder Theorem (CRT) parameters if primes 'p' and 'q' are available. |
||
| 293 | */ |
||
| 294 | private function populateCRT() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @throws \Exception |
||
| 322 | */ |
||
| 323 | private function initPublicKey() |
||
| 339 | |||
| 340 | private function initPrivateKey() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param string $value |
||
| 375 | * |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | private function fromBase64ToInteger(string $value): string |
||
| 382 | |||
| 383 | private function populateBigIntegers() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param string $value |
||
| 408 | * |
||
| 409 | * @return BigInteger |
||
| 410 | */ |
||
| 411 | private function convertBase64StringToBigInteger(string $value): BigInteger |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @param BigInteger $d |
||
| 418 | * @param BigInteger $e |
||
| 419 | * @param BigInteger $n |
||
| 420 | * |
||
| 421 | * @return BigInteger[] |
||
| 422 | */ |
||
| 423 | private function findPrimeFactors(BigInteger $d, BigInteger $e, BigInteger $n): array |
||
| 483 | } |
||
| 484 |
If you have a function call in the test part of a
forloop, this function is executed on each iteration. Often such a function, can be moved to the initialization part and be cached.