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 | * |
||
95 | * @param PayloadEncoderInterface $payloadEncoder |
||
96 | * @param JWAManager $keyEncryptionAlgorithmManager |
||
97 | * @param JWAManager $contentEncryptionAlgorithmManager |
||
98 | * @param CompressionMethodManager $compressionManager |
||
99 | */ |
||
100 | public function __construct(PayloadEncoderInterface $payloadEncoder, 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 |
||
342 | |||
343 | /** |
||
344 | * @param array $completeHeaders |
||
345 | * @param string $cek |
||
346 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
347 | * @param JWK $recipientKey |
||
348 | * @param array $additionalHeaders |
||
349 | * |
||
350 | * @return string|null |
||
351 | */ |
||
352 | private function getEncryptedKey(array $completeHeaders, string $cek, KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): ?string |
||
368 | |||
369 | /** |
||
370 | * @param array $completeHeaders |
||
371 | * @param string $cek |
||
372 | * @param KeyAgreementWrappingInterface $keyEncryptionAlgorithm |
||
373 | * @param array $additionalHeaders |
||
374 | * @param JWK $recipientKey |
||
375 | * |
||
376 | * @return string |
||
377 | */ |
||
378 | private function getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyAgreementWrappingInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): string |
||
382 | |||
383 | /** |
||
384 | * @param array $completeHeaders |
||
385 | * @param string $cek |
||
386 | * @param KeyEncryptionInterface $keyEncryptionAlgorithm |
||
387 | * @param JWK $recipientKey |
||
388 | * @param array $additionalHeaders |
||
389 | * |
||
390 | * @return string |
||
391 | */ |
||
392 | private function getEncryptedKeyFromKeyEncryptionAlgorithm(array $completeHeaders, string $cek, KeyEncryptionInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string |
||
396 | |||
397 | /** |
||
398 | * @param array $completeHeaders |
||
399 | * @param string $cek |
||
400 | * @param KeyWrappingInterface $keyEncryptionAlgorithm |
||
401 | * @param JWK $recipientKey |
||
402 | * @param array $additionalHeaders |
||
403 | * |
||
404 | * @return string |
||
405 | */ |
||
406 | private function getEncryptedKeyFromKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyWrappingInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string |
||
410 | |||
411 | /** |
||
412 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
413 | * @param JWK $recipientKey |
||
414 | */ |
||
415 | private function checkKey(KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, JWK $recipientKey) |
||
424 | |||
425 | /** |
||
426 | * @param array $additionalHeaders |
||
427 | * |
||
428 | * @return string |
||
429 | */ |
||
430 | private function determineCEK(array &$additionalHeaders): string |
||
462 | |||
463 | /** |
||
464 | * @param array $completeHeaders |
||
465 | * |
||
466 | * @return CompressionMethodInterface|null |
||
467 | */ |
||
468 | private function getCompressionMethod(array $completeHeaders): ?CompressionMethodInterface |
||
476 | |||
477 | /** |
||
478 | * @param string $current |
||
479 | * @param string $new |
||
480 | * |
||
481 | * @return bool |
||
482 | */ |
||
483 | private function areKeyManagementModesCompatible(string $current, string $new): bool |
||
497 | |||
498 | /** |
||
499 | * @param int $size |
||
500 | * |
||
501 | * @return string |
||
502 | */ |
||
503 | private function createCEK(int $size): string |
||
507 | |||
508 | /** |
||
509 | * @param int $size |
||
510 | * |
||
511 | * @return string |
||
512 | */ |
||
513 | private function createIV(int $size): string |
||
517 | |||
518 | /** |
||
519 | * @param array $completeHeaders |
||
520 | * |
||
521 | * @return KeyEncryptionAlgorithmInterface |
||
522 | */ |
||
523 | private function getKeyEncryptionAlgorithm(array $completeHeaders): KeyEncryptionAlgorithmInterface |
||
535 | |||
536 | /** |
||
537 | * @param array $completeHeaders |
||
538 | * |
||
539 | * @return ContentEncryptionAlgorithmInterface |
||
540 | */ |
||
541 | private function getContentEncryptionAlgorithm(array $completeHeaders): ContentEncryptionAlgorithmInterface |
||
553 | |||
554 | /** |
||
555 | * @param array $header1 |
||
556 | * @param array $header2 |
||
557 | */ |
||
558 | private function checkDuplicatedHeaderParameters(array $header1, array $header2) |
||
565 | } |
||
566 |