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 |
||
26 | { |
||
27 | /** |
||
28 | * @var Sequence |
||
29 | */ |
||
30 | private $sequence; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | private $values = []; |
||
36 | |||
37 | /** |
||
38 | * @var BigInteger |
||
39 | */ |
||
40 | private $modulus; |
||
41 | |||
42 | /** |
||
43 | * @var int |
||
44 | */ |
||
45 | private $modulus_length; |
||
46 | |||
47 | /** |
||
48 | * @var BigInteger |
||
49 | */ |
||
50 | private $public_exponent; |
||
51 | |||
52 | /** |
||
53 | * @var BigInteger|null |
||
54 | */ |
||
55 | private $private_exponent = null; |
||
56 | |||
57 | /** |
||
58 | * @var BigInteger[] |
||
59 | */ |
||
60 | private $primes = []; |
||
61 | |||
62 | /** |
||
63 | * @var BigInteger[] |
||
64 | */ |
||
65 | private $exponents = []; |
||
66 | |||
67 | /** |
||
68 | * @var BigInteger|null |
||
69 | */ |
||
70 | private $coefficient = null; |
||
71 | |||
72 | /** |
||
73 | * @param JWK $data |
||
74 | */ |
||
75 | private function __construct(JWK $data) |
||
81 | |||
82 | /** |
||
83 | * @param string $pem |
||
84 | * |
||
85 | * @return RSAKey |
||
86 | */ |
||
87 | public static function createFromPEM(string $pem): RSAKey |
||
93 | |||
94 | /** |
||
95 | * @param string $data |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | private static function loadPEM(string $data): array |
||
134 | |||
135 | /** |
||
136 | * @param JWK $jwk |
||
137 | * |
||
138 | * @return RSAKey |
||
139 | */ |
||
140 | public static function createFromJWK(JWK $jwk): RSAKey |
||
144 | |||
145 | /** |
||
146 | * @return BigInteger |
||
147 | */ |
||
148 | public function getModulus(): BigInteger |
||
152 | |||
153 | /** |
||
154 | * @return int |
||
155 | */ |
||
156 | public function getModulusLength(): int |
||
160 | |||
161 | /** |
||
162 | * @return BigInteger |
||
163 | */ |
||
164 | public function getExponent(): BigInteger |
||
173 | |||
174 | /** |
||
175 | * @return BigInteger |
||
176 | */ |
||
177 | public function getPublicExponent(): BigInteger |
||
181 | |||
182 | /** |
||
183 | * @return BigInteger|null |
||
184 | */ |
||
185 | public function getPrivateExponent(): ?BigInteger |
||
189 | |||
190 | /** |
||
191 | * @return BigInteger[] |
||
192 | */ |
||
193 | public function getPrimes(): array |
||
197 | |||
198 | /** |
||
199 | * @return BigInteger[] |
||
200 | */ |
||
201 | public function getExponents(): array |
||
205 | |||
206 | /** |
||
207 | * @return BigInteger|null |
||
208 | */ |
||
209 | public function getCoefficient(): ?BigInteger |
||
213 | |||
214 | /** |
||
215 | * @return bool |
||
216 | */ |
||
217 | public function isPublic(): bool |
||
221 | |||
222 | /** |
||
223 | * @param RSAKey $private |
||
224 | * |
||
225 | * @return RSAKey |
||
226 | */ |
||
227 | public static function toPublic(RSAKey $private): RSAKey |
||
239 | |||
240 | /** |
||
241 | * @return array |
||
242 | */ |
||
243 | public function toArray(): array |
||
247 | |||
248 | /** |
||
249 | * @return string |
||
250 | */ |
||
251 | public function toPEM(): string |
||
259 | |||
260 | /** |
||
261 | * @param array $jwk |
||
262 | */ |
||
263 | private function loadJWK(array $jwk) |
||
280 | |||
281 | /** |
||
282 | * This method adds Chinese Remainder Theorem (CRT) parameters if primes 'p' and 'q' are available. |
||
283 | */ |
||
284 | private function populateCRT() |
||
309 | |||
310 | /** |
||
311 | * @throws \Exception |
||
312 | */ |
||
313 | private function initPublicKey() |
||
329 | |||
330 | private function initPrivateKey() |
||
362 | |||
363 | /** |
||
364 | * @param string $value |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | private function fromBase64ToInteger(string $value): string |
||
372 | |||
373 | private function populateBigIntegers() |
||
395 | |||
396 | /** |
||
397 | * @param string $value |
||
398 | * |
||
399 | * @return BigInteger |
||
400 | */ |
||
401 | private function convertBase64StringToBigInteger(string $value): BigInteger |
||
405 | |||
406 | /** |
||
407 | * @param BigInteger $d |
||
408 | * @param BigInteger $e |
||
409 | * @param BigInteger $n |
||
410 | * |
||
411 | * @return BigInteger[] |
||
412 | */ |
||
413 | private function findPrimeFactors(BigInteger $d, BigInteger $e, BigInteger $n): array |
||
475 | } |
||
476 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.