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 PayloadEncoderInterface |
||
35 | */ |
||
36 | private $payloadEncoder; |
||
37 | /** |
||
38 | * @var mixed |
||
39 | */ |
||
40 | private $payload; |
||
41 | |||
42 | /** |
||
43 | * @var string|null |
||
44 | */ |
||
45 | private $aad; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | private $recipients = []; |
||
51 | |||
52 | /** |
||
53 | * @var JWAManager |
||
54 | */ |
||
55 | private $keyEncryptionAlgorithmManager; |
||
56 | |||
57 | /** |
||
58 | * @var JWAManager |
||
59 | */ |
||
60 | private $contentEncryptionAlgorithmManager; |
||
61 | |||
62 | /** |
||
63 | * @var CompressionMethodManager |
||
64 | */ |
||
65 | private $compressionManager; |
||
66 | |||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | private $sharedProtectedHeaders = []; |
||
71 | |||
72 | /** |
||
73 | * @var array |
||
74 | */ |
||
75 | private $sharedHeaders = []; |
||
76 | |||
77 | /** |
||
78 | * @var null|CompressionMethodInterface |
||
79 | */ |
||
80 | private $compressionMethod = null; |
||
81 | |||
82 | /** |
||
83 | * @var null|ContentEncryptionAlgorithmInterface |
||
84 | */ |
||
85 | private $contentEncryptionAlgorithm = null; |
||
86 | |||
87 | /** |
||
88 | * @var null|string |
||
89 | */ |
||
90 | private $keyManagementMode = null; |
||
91 | |||
92 | /** |
||
93 | * JWEBuilder constructor. |
||
94 | * @param PayloadEncoderInterface $payloadEncoder |
||
95 | * @param JWAManager $keyEncryptionAlgorithmManager |
||
96 | * @param JWAManager $contentEncryptionAlgorithmManager |
||
97 | * @param CompressionMethodManager $compressionManager |
||
98 | */ |
||
99 | public function __construct(PayloadEncoderInterface $payloadEncoder, JWAManager $keyEncryptionAlgorithmManager, JWAManager $contentEncryptionAlgorithmManager, CompressionMethodManager $compressionManager) |
||
106 | |||
107 | /** |
||
108 | * @return string[] |
||
109 | */ |
||
110 | public function getSupportedKeyEncryptionAlgorithms(): array |
||
114 | |||
115 | /** |
||
116 | * @return string[] |
||
117 | */ |
||
118 | public function getSupportedContentEncryptionAlgorithms(): array |
||
122 | |||
123 | /** |
||
124 | * @return string[] |
||
125 | */ |
||
126 | public function getSupportedCompressionMethods(): array |
||
130 | |||
131 | /** |
||
132 | * @param mixed $payload |
||
133 | * |
||
134 | * @return JWEBuilder |
||
135 | */ |
||
136 | public function withPayload($payload): JWEBuilder |
||
147 | |||
148 | /** |
||
149 | * @param string|null $aad |
||
150 | * |
||
151 | * @return JWEBuilder |
||
152 | */ |
||
153 | public function withAAD(?string $aad): JWEBuilder |
||
160 | |||
161 | /** |
||
162 | * @param array $sharedProtectedHeaders |
||
163 | * |
||
164 | * @return JWEBuilder |
||
165 | */ |
||
166 | public function withSharedProtectedHeaders(array $sharedProtectedHeaders): JWEBuilder |
||
177 | |||
178 | /** |
||
179 | * @param array $sharedHeaders |
||
180 | * |
||
181 | * @return JWEBuilder |
||
182 | */ |
||
183 | public function withSharedHeaders(array $sharedHeaders): JWEBuilder |
||
194 | |||
195 | /** |
||
196 | * @param JWK $recipientKey |
||
197 | * @param array $recipientHeaders |
||
198 | * |
||
199 | * @return JWEBuilder |
||
200 | */ |
||
201 | public function addRecipient(JWK $recipientKey, array $recipientHeaders = []): JWEBuilder |
||
237 | |||
238 | /** |
||
239 | * @return JWE |
||
240 | */ |
||
241 | public function build(): JWE |
||
267 | |||
268 | /** |
||
269 | * @param array $completeHeaders |
||
270 | */ |
||
271 | private function checkAndSetContentEncryptionAlgorithm(array $completeHeaders): void |
||
280 | |||
281 | /** |
||
282 | * @param array $recipient |
||
283 | * @param string $cek |
||
284 | * @param array $additionalHeaders |
||
285 | * |
||
286 | * @return Recipient |
||
287 | */ |
||
288 | private function processRecipient(array $recipient, string $cek, array &$additionalHeaders): Recipient |
||
302 | |||
303 | /** |
||
304 | * @param string $cek |
||
305 | * @param string $encodedSharedProtectedHeaders |
||
306 | * |
||
307 | * @return array |
||
308 | */ |
||
309 | private function encryptJWE(string $cek, string $encodedSharedProtectedHeaders): array |
||
320 | |||
321 | /** |
||
322 | * @return string |
||
323 | */ |
||
324 | private function preparePayload(): ?string |
||
341 | |||
342 | /** |
||
343 | * @param array $completeHeaders |
||
344 | * @param string $cek |
||
345 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
346 | * @param JWK $recipientKey |
||
347 | * @param array $additionalHeaders |
||
348 | * |
||
349 | * @return string|null |
||
350 | */ |
||
351 | private function getEncryptedKey(array $completeHeaders, string $cek, KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): ?string |
||
367 | |||
368 | /** |
||
369 | * @param array $completeHeaders |
||
370 | * @param string $cek |
||
371 | * @param KeyAgreementWrappingInterface $keyEncryptionAlgorithm |
||
372 | * @param array $additionalHeaders |
||
373 | * @param JWK $recipientKey |
||
374 | * |
||
375 | * @return string |
||
376 | */ |
||
377 | private function getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyAgreementWrappingInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): string |
||
381 | |||
382 | /** |
||
383 | * @param array $completeHeaders |
||
384 | * @param string $cek |
||
385 | * @param KeyEncryptionInterface $keyEncryptionAlgorithm |
||
386 | * @param JWK $recipientKey |
||
387 | * @param array $additionalHeaders |
||
388 | * |
||
389 | * @return string |
||
390 | */ |
||
391 | private function getEncryptedKeyFromKeyEncryptionAlgorithm(array $completeHeaders, string $cek, KeyEncryptionInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string |
||
395 | |||
396 | /** |
||
397 | * @param array $completeHeaders |
||
398 | * @param string $cek |
||
399 | * @param KeyWrappingInterface $keyEncryptionAlgorithm |
||
400 | * @param JWK $recipientKey |
||
401 | * @param array $additionalHeaders |
||
402 | * |
||
403 | * @return string |
||
404 | */ |
||
405 | private function getEncryptedKeyFromKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyWrappingInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string |
||
409 | |||
410 | /** |
||
411 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
412 | * @param JWK $recipientKey |
||
413 | */ |
||
414 | private function checkKey(KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, JWK $recipientKey) |
||
423 | |||
424 | /** |
||
425 | * @param array $additionalHeaders |
||
426 | * |
||
427 | * @return string |
||
428 | */ |
||
429 | private function determineCEK(array &$additionalHeaders): string |
||
461 | |||
462 | /** |
||
463 | * @param array $completeHeaders |
||
464 | * |
||
465 | * @return CompressionMethodInterface|null |
||
466 | */ |
||
467 | private function getCompressionMethod(array $completeHeaders): ?CompressionMethodInterface |
||
475 | |||
476 | /** |
||
477 | * @param string $current |
||
478 | * @param string $new |
||
479 | * |
||
480 | * @return bool |
||
481 | */ |
||
482 | private function areKeyManagementModesCompatible(string $current, string $new): bool |
||
496 | |||
497 | /** |
||
498 | * @param int $size |
||
499 | * |
||
500 | * @return string |
||
501 | */ |
||
502 | private function createCEK(int $size): string |
||
506 | |||
507 | /** |
||
508 | * @param int $size |
||
509 | * |
||
510 | * @return string |
||
511 | */ |
||
512 | private function createIV(int $size): string |
||
516 | |||
517 | /** |
||
518 | * @param array $completeHeaders |
||
519 | * |
||
520 | * @return KeyEncryptionAlgorithmInterface |
||
521 | */ |
||
522 | private function getKeyEncryptionAlgorithm(array $completeHeaders): KeyEncryptionAlgorithmInterface |
||
534 | |||
535 | /** |
||
536 | * @param array $completeHeaders |
||
537 | * |
||
538 | * @return ContentEncryptionAlgorithmInterface |
||
539 | */ |
||
540 | private function getContentEncryptionAlgorithm(array $completeHeaders): ContentEncryptionAlgorithmInterface |
||
552 | |||
553 | /** |
||
554 | * @param array $header1 |
||
555 | * @param array $header2 |
||
556 | */ |
||
557 | private function checkDuplicatedHeaderParameters(array $header1, array $header2) |
||
564 | } |
||
565 |