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 | /** |
||
69 | * @param \Jose\Object\JWKInterface|string|array $data |
||
70 | */ |
||
71 | public function __construct($data) |
||
87 | |||
88 | /** |
||
89 | * @return bool |
||
90 | */ |
||
91 | public function isPublic() |
||
95 | |||
96 | /** |
||
97 | * @return bool |
||
98 | */ |
||
99 | public function isPrivate() |
||
103 | |||
104 | /** |
||
105 | * @return \Jose\Util\BigInteger |
||
106 | */ |
||
107 | public function getModulus() |
||
111 | |||
112 | /** |
||
113 | * @return int |
||
114 | */ |
||
115 | public function getModulusLength() |
||
119 | |||
120 | /** |
||
121 | * @return \Jose\Util\BigInteger |
||
122 | */ |
||
123 | public function getExponent() |
||
132 | |||
133 | /** |
||
134 | * @return \Jose\Util\BigInteger |
||
135 | */ |
||
136 | public function getPublicExponent() |
||
140 | |||
141 | /** |
||
142 | * @return \Jose\Util\BigInteger |
||
143 | */ |
||
144 | public function getPrivateExponent() |
||
148 | |||
149 | /** |
||
150 | * @return \Jose\Util\BigInteger[] |
||
151 | */ |
||
152 | public function getPrimes() |
||
156 | |||
157 | /** |
||
158 | * @return \Jose\Util\BigInteger[] |
||
159 | */ |
||
160 | public function getExponents() |
||
164 | |||
165 | /** |
||
166 | * @return \Jose\Util\BigInteger|null |
||
167 | */ |
||
168 | public function getCoefficient() |
||
172 | |||
173 | /** |
||
174 | * @param \Jose\KeyConverter\RSAKey $private |
||
175 | * |
||
176 | * @return \Jose\KeyConverter\RSAKey |
||
177 | */ |
||
178 | public static function toPublic(RSAKey $private) |
||
190 | |||
191 | public function __toString() |
||
195 | |||
196 | /** |
||
197 | * @return array |
||
198 | */ |
||
199 | public function toArray() |
||
203 | |||
204 | /** |
||
205 | * @return string |
||
206 | */ |
||
207 | public function toDER() |
||
211 | |||
212 | /** |
||
213 | * @return string |
||
214 | */ |
||
215 | public function toPEM() |
||
223 | |||
224 | /** |
||
225 | * @param string $data |
||
226 | * |
||
227 | * @throws \Exception |
||
228 | * @throws \FG\ASN1\Exception\ParserException |
||
229 | * |
||
230 | * @return array |
||
231 | */ |
||
232 | private function loadPEM($data) |
||
261 | |||
262 | /** |
||
263 | * @param array $jwk |
||
264 | */ |
||
265 | private function loadJWK(array $jwk) |
||
278 | |||
279 | /** |
||
280 | * This method adds Chinese Remainder Theorem (CRT) parameters if primes 'p' and 'q' are available. |
||
281 | */ |
||
282 | private function populateCRT() |
||
300 | |||
301 | /** |
||
302 | * @throws \Exception |
||
303 | */ |
||
304 | private function initPublicKey() |
||
320 | |||
321 | private function initPrivateKey() |
||
353 | |||
354 | /** |
||
355 | * @param string $value |
||
356 | * |
||
357 | * @return string |
||
358 | */ |
||
359 | private function fromBase64ToInteger($value) |
||
363 | |||
364 | private function populateBigIntegers() |
||
386 | |||
387 | /** |
||
388 | * @param string $value |
||
389 | * |
||
390 | * @return \Jose\Util\BigInteger |
||
391 | */ |
||
392 | private function convertBase64StringToBigInteger($value) |
||
396 | } |
||
397 |