Complex classes like TypedJWK 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 TypedJWK, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | trait TypedJWK |
||
32 | { |
||
33 | /** |
||
34 | * Whether parameters are present. |
||
35 | * |
||
36 | * @param string ...$names Parameter names |
||
37 | * @return bool |
||
38 | */ |
||
39 | abstract public function has(...$names); |
||
40 | |||
41 | /** |
||
42 | * Get a parameter. |
||
43 | * |
||
44 | * @param string $name Parameter name |
||
45 | * @return JWKParameter |
||
46 | */ |
||
47 | abstract public function get($name); |
||
48 | |||
49 | /** |
||
50 | * Check whether the algorithm parameter is present. |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | 22 | public function hasAlgorithmParameter() { |
|
57 | |||
58 | /** |
||
59 | * Get the algorithm parameter. |
||
60 | * |
||
61 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
62 | * @throws \LogicException If the parameter is not present |
||
63 | * @return AlgorithmParameter |
||
64 | */ |
||
65 | 19 | public function algorithmParameter() { |
|
69 | |||
70 | /** |
||
71 | * Check whether the curve parameter is present. |
||
72 | * |
||
73 | * @return bool |
||
74 | */ |
||
75 | 1 | public function hasCurveParameter() { |
|
78 | |||
79 | /** |
||
80 | * Get the curve parameter. |
||
81 | * |
||
82 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
83 | * @throws \LogicException If the parameter is not present |
||
84 | * @return CurveParameter |
||
85 | */ |
||
86 | 1 | public function curveParameter() { |
|
90 | |||
91 | /** |
||
92 | * Check whether the ECC private key parameter is present. |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | 1 | public function hasECCPrivateKeyParameter() { |
|
99 | |||
100 | /** |
||
101 | * Get the ECC private key parameter. |
||
102 | * |
||
103 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
104 | * @throws \LogicException If the parameter is not present |
||
105 | * @return ECCPrivateKeyParameter |
||
106 | */ |
||
107 | 1 | public function ECCPrivateKeyParameter() { |
|
111 | |||
112 | /** |
||
113 | * Check whether the exponent parameter is present. |
||
114 | * |
||
115 | * @return bool |
||
116 | */ |
||
117 | 1 | public function hasExponentParameter() { |
|
120 | |||
121 | /** |
||
122 | * Get the exponent parameter. |
||
123 | * |
||
124 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
125 | * @throws \LogicException If the parameter is not present |
||
126 | * @return ExponentParameter |
||
127 | */ |
||
128 | 41 | public function exponentParameter() { |
|
132 | |||
133 | /** |
||
134 | * Check whether the first CRT coefficient parameter is present. |
||
135 | * |
||
136 | * @return bool |
||
137 | */ |
||
138 | 1 | public function hasFirstCRTCoefficientParameter() { |
|
141 | |||
142 | /** |
||
143 | * Get the first CRT coefficient parameter. |
||
144 | * |
||
145 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
146 | * @throws \LogicException If the parameter is not present |
||
147 | * @return FirstCRTCoefficientParameter |
||
148 | */ |
||
149 | 17 | public function firstCRTCoefficientParameter() { |
|
153 | |||
154 | /** |
||
155 | * Check whether the first factor CRT exponent parameter is present. |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | 1 | public function hasFirstFactorCRTExponentParameter() { |
|
162 | |||
163 | /** |
||
164 | * Get the first factor CRT exponent parameter. |
||
165 | * |
||
166 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
167 | * @throws \LogicException If the parameter is not present |
||
168 | * @return FirstFactorCRTExponentParameter |
||
169 | */ |
||
170 | 17 | public function firstFactorCRTExponentParameter() { |
|
174 | |||
175 | /** |
||
176 | * Check whether the first prime factor parameter is present. |
||
177 | * |
||
178 | * @return bool |
||
179 | */ |
||
180 | 1 | public function hasFirstPrimeFactorParameter() { |
|
183 | |||
184 | /** |
||
185 | * Get the first prime factor parameter. |
||
186 | * |
||
187 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
188 | * @throws \LogicException If the parameter is not present |
||
189 | * @return FirstPrimeFactorParameter |
||
190 | */ |
||
191 | 17 | public function firstPrimeFactorParameter() { |
|
195 | |||
196 | /** |
||
197 | * Check whether the key ID parameter is present. |
||
198 | * |
||
199 | * @return bool |
||
200 | */ |
||
201 | 1 | public function hasKeyIDParameter() { |
|
204 | |||
205 | /** |
||
206 | * Get the key ID parameter. |
||
207 | * |
||
208 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
209 | * @throws \LogicException If the parameter is not present |
||
210 | * @return KeyIDParameter |
||
211 | */ |
||
212 | 1 | public function keyIDParameter() { |
|
216 | |||
217 | /** |
||
218 | * Check whether the key operations parameter is present. |
||
219 | * |
||
220 | * @return bool |
||
221 | */ |
||
222 | 1 | public function hasKeyOperationsParameter() { |
|
225 | |||
226 | /** |
||
227 | * Get the key operations parameter. |
||
228 | * |
||
229 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
230 | * @throws \LogicException If the parameter is not present |
||
231 | * @return KeyOperationsParameter |
||
232 | */ |
||
233 | 1 | public function keyOperationsParameter() { |
|
237 | |||
238 | /** |
||
239 | * Check whether the key type parameter is present. |
||
240 | * |
||
241 | * @return bool |
||
242 | */ |
||
243 | 1 | public function hasKeyTypeParameter() { |
|
246 | |||
247 | /** |
||
248 | * Get the key type parameter. |
||
249 | * |
||
250 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
251 | * @throws \LogicException If the parameter is not present |
||
252 | * @return KeyTypeParameter |
||
253 | */ |
||
254 | 78 | public function keyTypeParameter() { |
|
258 | |||
259 | /** |
||
260 | * Check whether the key value parameter is present. |
||
261 | * |
||
262 | * @return bool |
||
263 | */ |
||
264 | 1 | public function hasKeyValueParameter() { |
|
267 | |||
268 | /** |
||
269 | * Get the key value parameter. |
||
270 | * |
||
271 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
272 | * @throws \LogicException If the parameter is not present |
||
273 | * @return KeyValueParameter |
||
274 | */ |
||
275 | 19 | public function keyValueParameter() { |
|
279 | |||
280 | /** |
||
281 | * Check whether the modulus parameter is present. |
||
282 | * |
||
283 | * @return bool |
||
284 | */ |
||
285 | 1 | public function hasModulusParameter() { |
|
288 | |||
289 | /** |
||
290 | * Get the modulus parameter. |
||
291 | * |
||
292 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
293 | * @throws \LogicException If the parameter is not present |
||
294 | * @return ModulusParameter |
||
295 | */ |
||
296 | 41 | public function modulusParameter() { |
|
300 | |||
301 | /** |
||
302 | * Check whether the other primes info parameter is present. |
||
303 | * |
||
304 | * @return bool |
||
305 | */ |
||
306 | 1 | public function hasOtherPrimesInfoParameter() { |
|
309 | |||
310 | /** |
||
311 | * Get the other primes info parameter. |
||
312 | * |
||
313 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
314 | * @throws \LogicException If the parameter is not present |
||
315 | * @return OtherPrimesInfoParameter |
||
316 | */ |
||
317 | 1 | public function otherPrimesInfoParameter() { |
|
321 | |||
322 | /** |
||
323 | * Check whether the private exponent parameter is present. |
||
324 | * |
||
325 | * @return bool |
||
326 | */ |
||
327 | 1 | public function hasPrivateExponentParameter() { |
|
330 | |||
331 | /** |
||
332 | * Get the private exponent parameter. |
||
333 | * |
||
334 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
335 | * @throws \LogicException If the parameter is not present |
||
336 | * @return PrivateExponentParameter |
||
337 | */ |
||
338 | 17 | public function privateExponentParameter() { |
|
342 | |||
343 | /** |
||
344 | * Check whether the public key use parameter is present. |
||
345 | * |
||
346 | * @return bool |
||
347 | */ |
||
348 | 1 | public function hasPublicKeyUseParameter() { |
|
351 | |||
352 | /** |
||
353 | * Get the public key use parameter. |
||
354 | * |
||
355 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
356 | * @throws \LogicException If the parameter is not present |
||
357 | * @return PublicKeyUseParameter |
||
358 | */ |
||
359 | 1 | public function publicKeyUseParameter() { |
|
363 | |||
364 | /** |
||
365 | * Check whether the second factor CRT exponent parameter is present. |
||
366 | * |
||
367 | * @return bool |
||
368 | */ |
||
369 | 1 | public function hasSecondFactorCRTExponentParameter() { |
|
372 | |||
373 | /** |
||
374 | * Get the second factor CRT exponent parameter. |
||
375 | * |
||
376 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
377 | * @throws \LogicException If the parameter is not present |
||
378 | * @return SecondFactorCRTExponentParameter |
||
379 | */ |
||
380 | 17 | public function secondFactorCRTExponentParameter() { |
|
384 | |||
385 | /** |
||
386 | * Check whether the second prime factor parameter is present. |
||
387 | * |
||
388 | * @return bool |
||
389 | */ |
||
390 | 1 | public function hasSecondPrimeFactorParameter() { |
|
393 | |||
394 | /** |
||
395 | * Get the second prime factor parameter. |
||
396 | * |
||
397 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
398 | * @throws \LogicException If the parameter is not present |
||
399 | * @return SecondPrimeFactorParameter |
||
400 | */ |
||
401 | 17 | public function secondPrimeFactorParameter() { |
|
405 | |||
406 | /** |
||
407 | * Check whether the X coordinate parameter is present. |
||
408 | * |
||
409 | * @return bool |
||
410 | */ |
||
411 | 1 | public function hasXCoordinateParameter() { |
|
414 | |||
415 | /** |
||
416 | * Get the X coordinate parameter. |
||
417 | * |
||
418 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
419 | * @throws \LogicException If the parameter is not present |
||
420 | * @return XCoordinateParameter |
||
421 | */ |
||
422 | 1 | public function XCoordinateParameter() { |
|
426 | |||
427 | /** |
||
428 | * Check whether the Y coordinate parameter is present. |
||
429 | * |
||
430 | * @return bool |
||
431 | */ |
||
432 | 1 | public function hasYCoordinateParameter() { |
|
435 | |||
436 | /** |
||
437 | * Get the Y coordinate parameter. |
||
438 | * |
||
439 | * @throws \UnexpectedValueException If the parameter has a wrong class |
||
440 | * @throws \LogicException If the parameter is not present |
||
441 | * @return YCoordinateParameter |
||
442 | */ |
||
443 | 1 | public function YCoordinateParameter() { |
|
447 | |||
448 | /** |
||
449 | * Check that the parameter is an instance of the given class. |
||
450 | * |
||
451 | * @param JWKParameter $param Parameter |
||
452 | * @param string $cls Class name |
||
453 | * @throws \UnexpectedValueException |
||
454 | * @return JWKParameter |
||
455 | */ |
||
456 | 111 | private static function _checkType(JWKParameter $param, $cls) { |
|
463 | } |
||
464 |