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 bool |
||
37 | */ |
||
38 | private $private = false; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | private $values = []; |
||
44 | |||
45 | /** |
||
46 | * @var BigInteger |
||
47 | */ |
||
48 | private $modulus; |
||
49 | |||
50 | /** |
||
51 | * @var int |
||
52 | */ |
||
53 | private $modulus_length; |
||
54 | |||
55 | /** |
||
56 | * @var BigInteger |
||
57 | */ |
||
58 | private $public_exponent; |
||
59 | |||
60 | /** |
||
61 | * @var BigInteger|null |
||
62 | */ |
||
63 | private $private_exponent = null; |
||
64 | |||
65 | /** |
||
66 | * @var BigInteger[] |
||
67 | */ |
||
68 | private $primes = []; |
||
69 | |||
70 | /** |
||
71 | * @var BigInteger[] |
||
72 | */ |
||
73 | private $exponents = []; |
||
74 | |||
75 | /** |
||
76 | * @var BigInteger|null |
||
77 | */ |
||
78 | private $coefficient = null; |
||
79 | |||
80 | /** |
||
81 | * @param JWK $data |
||
82 | */ |
||
83 | private function __construct(JWK $data) |
||
89 | |||
90 | /** |
||
91 | * @param JWK $jwk |
||
92 | * |
||
93 | * @return RSAKey |
||
94 | */ |
||
95 | public static function createFromJWK(JWK $jwk): self |
||
99 | |||
100 | /** |
||
101 | * @return BigInteger |
||
102 | */ |
||
103 | public function getModulus(): BigInteger |
||
107 | |||
108 | /** |
||
109 | * @return int |
||
110 | */ |
||
111 | public function getModulusLength(): int |
||
115 | |||
116 | /** |
||
117 | * @return BigInteger |
||
118 | */ |
||
119 | public function getExponent(): BigInteger |
||
128 | |||
129 | /** |
||
130 | * @return BigInteger |
||
131 | */ |
||
132 | public function getPublicExponent(): BigInteger |
||
136 | |||
137 | /** |
||
138 | * @return BigInteger|null |
||
139 | */ |
||
140 | public function getPrivateExponent(): ?BigInteger |
||
144 | |||
145 | /** |
||
146 | * @return BigInteger[] |
||
147 | */ |
||
148 | public function getPrimes(): array |
||
152 | |||
153 | /** |
||
154 | * @return BigInteger[] |
||
155 | */ |
||
156 | public function getExponents(): array |
||
160 | |||
161 | /** |
||
162 | * @return BigInteger|null |
||
163 | */ |
||
164 | public function getCoefficient(): ?BigInteger |
||
168 | |||
169 | /** |
||
170 | * @return bool |
||
171 | */ |
||
172 | public function isPublic(): bool |
||
176 | |||
177 | /** |
||
178 | * @param RSAKey $private |
||
179 | * |
||
180 | * @return RSAKey |
||
181 | */ |
||
182 | public static function toPublic(self $private): self |
||
194 | |||
195 | /** |
||
196 | * @return array |
||
197 | */ |
||
198 | public function toArray(): array |
||
202 | |||
203 | /** |
||
204 | * @param array $jwk |
||
205 | */ |
||
206 | private function loadJWK(array $jwk) |
||
217 | |||
218 | private function populateBigIntegers() |
||
242 | |||
243 | /** |
||
244 | * @param string $value |
||
245 | * |
||
246 | * @return BigInteger |
||
247 | */ |
||
248 | private function convertBase64StringToBigInteger(string $value): BigInteger |
||
252 | |||
253 | /** |
||
254 | * @return string |
||
255 | */ |
||
256 | public function toPEM(): string |
||
272 | |||
273 | /** |
||
274 | * @throws \Exception |
||
275 | */ |
||
276 | private function initPublicKey() |
||
290 | |||
291 | private function initPrivateKey() |
||
320 | |||
321 | /** |
||
322 | * @param string $value |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | private function fromBase64ToInteger($value) |
||
330 | |||
331 | /** |
||
332 | * Exponentiate with or without Chinese Remainder Theorem. |
||
333 | * Operation with primes 'p' and 'q' is appox. 2x faster. |
||
334 | * |
||
335 | * @param RSAKey $key |
||
336 | * @param BigInteger $c |
||
337 | * |
||
338 | * @return BigInteger |
||
339 | */ |
||
340 | public static function exponentiate(RSAKey $key, BigInteger $c): BigInteger |
||
362 | } |
||
363 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: