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 |
||
29 | class RSAKey |
||
30 | { |
||
31 | /** |
||
32 | * @var Sequence |
||
33 | */ |
||
34 | private $sequence; |
||
35 | |||
36 | /** |
||
37 | * @var bool |
||
38 | */ |
||
39 | private $private = false; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | private $values = []; |
||
45 | |||
46 | /** |
||
47 | * @var BigInteger |
||
48 | */ |
||
49 | private $modulus; |
||
50 | |||
51 | /** |
||
52 | * @var int |
||
53 | */ |
||
54 | private $modulus_length; |
||
55 | |||
56 | /** |
||
57 | * @var BigInteger |
||
58 | */ |
||
59 | private $public_exponent; |
||
60 | |||
61 | /** |
||
62 | * @var null|BigInteger |
||
63 | */ |
||
64 | private $private_exponent; |
||
65 | |||
66 | /** |
||
67 | * @var BigInteger[] |
||
68 | */ |
||
69 | private $primes = []; |
||
70 | |||
71 | /** |
||
72 | * @var BigInteger[] |
||
73 | */ |
||
74 | private $exponents = []; |
||
75 | |||
76 | /** |
||
77 | * @var null|BigInteger |
||
78 | */ |
||
79 | private $coefficient; |
||
80 | |||
81 | private function __construct(JWK $data) |
||
87 | |||
88 | /** |
||
89 | * @return RSAKey |
||
90 | */ |
||
91 | public static function createFromJWK(JWK $jwk): self |
||
95 | |||
96 | public function getModulus(): BigInteger |
||
100 | |||
101 | public function getModulusLength(): int |
||
105 | |||
106 | public function getExponent(): BigInteger |
||
115 | |||
116 | public function getPublicExponent(): BigInteger |
||
120 | |||
121 | public function getPrivateExponent(): ?BigInteger |
||
125 | |||
126 | /** |
||
127 | * @return BigInteger[] |
||
128 | */ |
||
129 | public function getPrimes(): array |
||
133 | |||
134 | /** |
||
135 | * @return BigInteger[] |
||
136 | */ |
||
137 | public function getExponents(): array |
||
141 | |||
142 | public function getCoefficient(): ?BigInteger |
||
146 | |||
147 | public function isPublic(): bool |
||
151 | |||
152 | /** |
||
153 | * @param RSAKey $private |
||
154 | * |
||
155 | * @return RSAKey |
||
156 | */ |
||
157 | public static function toPublic(self $private): self |
||
169 | |||
170 | public function toArray(): array |
||
174 | |||
175 | public function toPEM(): string |
||
191 | |||
192 | /** |
||
193 | * Exponentiate with or without Chinese Remainder Theorem. |
||
194 | * Operation with primes 'p' and 'q' is appox. 2x faster. |
||
195 | * |
||
196 | * @param RSAKey $key |
||
197 | * |
||
198 | * @throws RuntimeException if the exponentiation cannot be achieved |
||
199 | */ |
||
200 | public static function exponentiate(self $key, BigInteger $c): BigInteger |
||
221 | |||
222 | private function populateBigIntegers(): void |
||
246 | |||
247 | private function convertBase64StringToBigInteger(string $value): BigInteger |
||
251 | |||
252 | private function initPublicKey(): void |
||
266 | |||
267 | private function initPrivateKey(): void |
||
296 | |||
297 | /** |
||
298 | * @param string $value |
||
299 | * |
||
300 | * @return string |
||
301 | */ |
||
302 | private function fromBase64ToInteger($value) |
||
306 | } |
||
307 |