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 |
||
| 25 | final class RSAKey extends Sequence |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private $values = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var \Jose\Util\BigInteger |
||
| 34 | */ |
||
| 35 | private $modulus; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | private $modulus_length; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var \Jose\Util\BigInteger |
||
| 44 | */ |
||
| 45 | private $public_exponent; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \Jose\Util\BigInteger|null |
||
| 49 | */ |
||
| 50 | private $private_exponent = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var \Jose\Util\BigInteger[] |
||
| 54 | */ |
||
| 55 | private $primes = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var \Jose\Util\BigInteger[] |
||
| 59 | */ |
||
| 60 | private $exponents = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var \Jose\Util\BigInteger|null |
||
| 64 | */ |
||
| 65 | private $coefficient = null; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param \Jose\Object\JWKInterface|string|array $data |
||
| 69 | */ |
||
| 70 | public function __construct($data) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @return bool |
||
| 89 | */ |
||
| 90 | public function isPublic() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | public function isPrivate() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return \Jose\Util\BigInteger |
||
| 105 | */ |
||
| 106 | public function getModulus() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return int |
||
| 113 | */ |
||
| 114 | public function getModulusLength() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return \Jose\Util\BigInteger |
||
| 121 | */ |
||
| 122 | public function getExponent() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return \Jose\Util\BigInteger |
||
| 134 | */ |
||
| 135 | public function getPublicExponent() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return \Jose\Util\BigInteger |
||
| 142 | */ |
||
| 143 | public function getPrivateExponent() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return \Jose\Util\BigInteger[] |
||
| 150 | */ |
||
| 151 | public function getPrimes() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return \Jose\Util\BigInteger[] |
||
| 158 | */ |
||
| 159 | public function getExponents() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return \Jose\Util\BigInteger|null |
||
| 166 | */ |
||
| 167 | public function getCoefficient() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param \Jose\KeyConverter\RSAKey $private |
||
| 174 | * |
||
| 175 | * @return \Jose\KeyConverter\RSAKey |
||
| 176 | */ |
||
| 177 | public static function toPublic(RSAKey $private) |
||
| 189 | |||
| 190 | public function __toString() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | public function toArray() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function toDER() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function toPEM() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param string $data |
||
| 225 | * |
||
| 226 | * @throws \Exception |
||
| 227 | * @throws \FG\ASN1\Exception\ParserException |
||
| 228 | * |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | private function loadPEM($data) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param array $jwk |
||
| 263 | */ |
||
| 264 | private function loadJWK(array $jwk) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * This method adds Chinese Remainder Theorem (CRT) parameters if primes 'p' and 'q' are available. |
||
| 280 | */ |
||
| 281 | private function populateCRT() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @throws \Exception |
||
| 301 | */ |
||
| 302 | private function initPublicKey() |
||
| 318 | |||
| 319 | private function initPrivateKey() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param string $value |
||
| 354 | * |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | private function fromBase64ToInteger($value) |
||
| 361 | |||
| 362 | private function populateBigIntegers() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param string $value |
||
| 387 | * |
||
| 388 | * @return \Jose\Util\BigInteger |
||
| 389 | */ |
||
| 390 | private function convertBase64StringToBigInteger($value) |
||
| 394 | } |
||
| 395 |