Complex classes like JWEBuilder 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 JWEBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | final class JWEBuilder |
||
32 | { |
||
33 | /** |
||
34 | * @var JsonConverterInterface |
||
35 | */ |
||
36 | private $jsonConverter; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $payload; |
||
42 | |||
43 | /** |
||
44 | * @var string|null |
||
45 | */ |
||
46 | private $aad; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | private $recipients = []; |
||
52 | |||
53 | /** |
||
54 | * @var JWAManager |
||
55 | */ |
||
56 | private $keyEncryptionAlgorithmManager; |
||
57 | |||
58 | /** |
||
59 | * @var JWAManager |
||
60 | */ |
||
61 | private $contentEncryptionAlgorithmManager; |
||
62 | |||
63 | /** |
||
64 | * @var CompressionMethodManager |
||
65 | */ |
||
66 | private $compressionManager; |
||
67 | |||
68 | /** |
||
69 | * @var array |
||
70 | */ |
||
71 | private $sharedProtectedHeaders = []; |
||
72 | |||
73 | /** |
||
74 | * @var array |
||
75 | */ |
||
76 | private $sharedHeaders = []; |
||
77 | |||
78 | /** |
||
79 | * @var null|CompressionMethodInterface |
||
80 | */ |
||
81 | private $compressionMethod = null; |
||
82 | |||
83 | /** |
||
84 | * @var null|ContentEncryptionAlgorithmInterface |
||
85 | */ |
||
86 | private $contentEncryptionAlgorithm = null; |
||
87 | |||
88 | /** |
||
89 | * @var null|string |
||
90 | */ |
||
91 | private $keyManagementMode = null; |
||
92 | |||
93 | /** |
||
94 | * JWEBuilder constructor. |
||
95 | * @param JsonConverterInterface $jsonConverter |
||
96 | * @param JWAManager $keyEncryptionAlgorithmManager |
||
97 | * @param JWAManager $contentEncryptionAlgorithmManager |
||
98 | * @param CompressionMethodManager $compressionManager |
||
99 | */ |
||
100 | public function __construct(JsonConverterInterface $jsonConverter, JWAManager $keyEncryptionAlgorithmManager, JWAManager $contentEncryptionAlgorithmManager, CompressionMethodManager $compressionManager) |
||
107 | |||
108 | /** |
||
109 | * @return string[] |
||
110 | */ |
||
111 | public function getSupportedKeyEncryptionAlgorithms(): array |
||
115 | |||
116 | /** |
||
117 | * @return string[] |
||
118 | */ |
||
119 | public function getSupportedContentEncryptionAlgorithms(): array |
||
123 | |||
124 | /** |
||
125 | * @return string[] |
||
126 | */ |
||
127 | public function getSupportedCompressionMethods(): array |
||
131 | |||
132 | /** |
||
133 | * @param mixed $payload |
||
134 | * |
||
135 | * @return JWEBuilder |
||
136 | */ |
||
137 | public function withPayload($payload): JWEBuilder |
||
148 | |||
149 | /** |
||
150 | * @param string|null $aad |
||
151 | * |
||
152 | * @return JWEBuilder |
||
153 | */ |
||
154 | public function withAAD(?string $aad): JWEBuilder |
||
161 | |||
162 | /** |
||
163 | * @param array $sharedProtectedHeaders |
||
164 | * |
||
165 | * @return JWEBuilder |
||
166 | */ |
||
167 | public function withSharedProtectedHeaders(array $sharedProtectedHeaders): JWEBuilder |
||
178 | |||
179 | /** |
||
180 | * @param array $sharedHeaders |
||
181 | * |
||
182 | * @return JWEBuilder |
||
183 | */ |
||
184 | public function withSharedHeaders(array $sharedHeaders): JWEBuilder |
||
195 | |||
196 | /** |
||
197 | * @param JWK $recipientKey |
||
198 | * @param array $recipientHeaders |
||
199 | * |
||
200 | * @return JWEBuilder |
||
201 | */ |
||
202 | public function addRecipient(JWK $recipientKey, array $recipientHeaders = []): JWEBuilder |
||
238 | |||
239 | /** |
||
240 | * @return JWE |
||
241 | */ |
||
242 | public function build(): JWE |
||
268 | |||
269 | /** |
||
270 | * @param array $completeHeaders |
||
271 | */ |
||
272 | private function checkAndSetContentEncryptionAlgorithm(array $completeHeaders): void |
||
281 | |||
282 | /** |
||
283 | * @param array $recipient |
||
284 | * @param string $cek |
||
285 | * @param array $additionalHeaders |
||
286 | * |
||
287 | * @return Recipient |
||
288 | */ |
||
289 | private function processRecipient(array $recipient, string $cek, array &$additionalHeaders): Recipient |
||
303 | |||
304 | /** |
||
305 | * @param string $cek |
||
306 | * @param string $encodedSharedProtectedHeaders |
||
307 | * |
||
308 | * @return array |
||
309 | */ |
||
310 | private function encryptJWE(string $cek, string $encodedSharedProtectedHeaders): array |
||
321 | |||
322 | /** |
||
323 | * @return string |
||
324 | */ |
||
325 | private function preparePayload(): ?string |
||
339 | |||
340 | /** |
||
341 | * @param array $completeHeaders |
||
342 | * @param string $cek |
||
343 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
344 | * @param JWK $recipientKey |
||
345 | * @param array $additionalHeaders |
||
346 | * |
||
347 | * @return string|null |
||
348 | */ |
||
349 | private function getEncryptedKey(array $completeHeaders, string $cek, KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): ?string |
||
365 | |||
366 | /** |
||
367 | * @param array $completeHeaders |
||
368 | * @param string $cek |
||
369 | * @param KeyAgreementWrappingInterface $keyEncryptionAlgorithm |
||
370 | * @param array $additionalHeaders |
||
371 | * @param JWK $recipientKey |
||
372 | * |
||
373 | * @return string |
||
374 | */ |
||
375 | private function getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyAgreementWrappingInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): string |
||
379 | |||
380 | /** |
||
381 | * @param array $completeHeaders |
||
382 | * @param string $cek |
||
383 | * @param KeyEncryptionInterface $keyEncryptionAlgorithm |
||
384 | * @param JWK $recipientKey |
||
385 | * @param array $additionalHeaders |
||
386 | * |
||
387 | * @return string |
||
388 | */ |
||
389 | private function getEncryptedKeyFromKeyEncryptionAlgorithm(array $completeHeaders, string $cek, KeyEncryptionInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string |
||
393 | |||
394 | /** |
||
395 | * @param array $completeHeaders |
||
396 | * @param string $cek |
||
397 | * @param KeyWrappingInterface $keyEncryptionAlgorithm |
||
398 | * @param JWK $recipientKey |
||
399 | * @param array $additionalHeaders |
||
400 | * |
||
401 | * @return string |
||
402 | */ |
||
403 | private function getEncryptedKeyFromKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyWrappingInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string |
||
407 | |||
408 | /** |
||
409 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
410 | * @param JWK $recipientKey |
||
411 | */ |
||
412 | private function checkKey(KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, JWK $recipientKey) |
||
421 | |||
422 | /** |
||
423 | * @param array $additionalHeaders |
||
424 | * |
||
425 | * @return string |
||
426 | */ |
||
427 | private function determineCEK(array &$additionalHeaders): string |
||
459 | |||
460 | /** |
||
461 | * @param array $completeHeaders |
||
462 | * |
||
463 | * @return CompressionMethodInterface|null |
||
464 | */ |
||
465 | private function getCompressionMethod(array $completeHeaders): ?CompressionMethodInterface |
||
473 | |||
474 | /** |
||
475 | * @param string $current |
||
476 | * @param string $new |
||
477 | * |
||
478 | * @return bool |
||
479 | */ |
||
480 | private function areKeyManagementModesCompatible(string $current, string $new): bool |
||
494 | |||
495 | /** |
||
496 | * @param int $size |
||
497 | * |
||
498 | * @return string |
||
499 | */ |
||
500 | private function createCEK(int $size): string |
||
504 | |||
505 | /** |
||
506 | * @param int $size |
||
507 | * |
||
508 | * @return string |
||
509 | */ |
||
510 | private function createIV(int $size): string |
||
514 | |||
515 | /** |
||
516 | * @param array $completeHeaders |
||
517 | * |
||
518 | * @return KeyEncryptionAlgorithmInterface |
||
519 | */ |
||
520 | private function getKeyEncryptionAlgorithm(array $completeHeaders): KeyEncryptionAlgorithmInterface |
||
532 | |||
533 | /** |
||
534 | * @param array $completeHeaders |
||
535 | * |
||
536 | * @return ContentEncryptionAlgorithmInterface |
||
537 | */ |
||
538 | private function getContentEncryptionAlgorithm(array $completeHeaders): ContentEncryptionAlgorithmInterface |
||
550 | |||
551 | /** |
||
552 | * @param array $header1 |
||
553 | * @param array $header2 |
||
554 | */ |
||
555 | private function checkDuplicatedHeaderParameters(array $header1, array $header2) |
||
562 | } |
||
563 |