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 |
||
34 | final class JWEBuilder |
||
35 | { |
||
36 | /** |
||
37 | * @var JsonConverterInterface |
||
38 | */ |
||
39 | private $jsonConverter; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $payload; |
||
45 | |||
46 | /** |
||
47 | * @var string|null |
||
48 | */ |
||
49 | private $aad; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | private $recipients = []; |
||
55 | |||
56 | /** |
||
57 | * @var AlgorithmManager |
||
58 | */ |
||
59 | private $keyEncryptionAlgorithmManager; |
||
60 | |||
61 | /** |
||
62 | * @var AlgorithmManager |
||
63 | */ |
||
64 | private $contentEncryptionAlgorithmManager; |
||
65 | |||
66 | /** |
||
67 | * @var CompressionMethodManager |
||
68 | */ |
||
69 | private $compressionManager; |
||
70 | |||
71 | /** |
||
72 | * @var array |
||
73 | */ |
||
74 | private $sharedProtectedHeaders = []; |
||
75 | |||
76 | /** |
||
77 | * @var array |
||
78 | */ |
||
79 | private $sharedHeaders = []; |
||
80 | |||
81 | /** |
||
82 | * @var null|CompressionMethodInterface |
||
83 | */ |
||
84 | private $compressionMethod = null; |
||
85 | |||
86 | /** |
||
87 | * @var null|ContentEncryptionAlgorithmInterface |
||
88 | */ |
||
89 | private $contentEncryptionAlgorithm = null; |
||
90 | |||
91 | /** |
||
92 | * @var null|string |
||
93 | */ |
||
94 | private $keyManagementMode = null; |
||
95 | |||
96 | /** |
||
97 | * JWEBuilder constructor. |
||
98 | * |
||
99 | * @param JsonConverterInterface $jsonConverter |
||
100 | * @param AlgorithmManager $keyEncryptionAlgorithmManager |
||
101 | * @param AlgorithmManager $contentEncryptionAlgorithmManager |
||
102 | * @param CompressionMethodManager $compressionManager |
||
103 | */ |
||
104 | public function __construct(JsonConverterInterface $jsonConverter, AlgorithmManager $keyEncryptionAlgorithmManager, AlgorithmManager $contentEncryptionAlgorithmManager, CompressionMethodManager $compressionManager) |
||
111 | |||
112 | /** |
||
113 | * @return string[] |
||
114 | */ |
||
115 | public function getSupportedKeyEncryptionAlgorithms(): array |
||
119 | |||
120 | /** |
||
121 | * @return string[] |
||
122 | */ |
||
123 | public function getSupportedContentEncryptionAlgorithms(): array |
||
127 | |||
128 | /** |
||
129 | * @return string[] |
||
130 | */ |
||
131 | public function getSupportedCompressionMethods(): array |
||
135 | |||
136 | /** |
||
137 | * @param mixed $payload |
||
138 | * |
||
139 | * @return JWEBuilder |
||
140 | */ |
||
141 | public function withPayload($payload): JWEBuilder |
||
152 | |||
153 | /** |
||
154 | * @param string|null $aad |
||
155 | * |
||
156 | * @return JWEBuilder |
||
157 | */ |
||
158 | public function withAAD(?string $aad): JWEBuilder |
||
165 | |||
166 | /** |
||
167 | * @param array $sharedProtectedHeaders |
||
168 | * |
||
169 | * @return JWEBuilder |
||
170 | */ |
||
171 | public function withSharedProtectedHeaders(array $sharedProtectedHeaders): JWEBuilder |
||
182 | |||
183 | /** |
||
184 | * @param array $sharedHeaders |
||
185 | * |
||
186 | * @return JWEBuilder |
||
187 | */ |
||
188 | public function withSharedHeaders(array $sharedHeaders): JWEBuilder |
||
199 | |||
200 | /** |
||
201 | * @param JWK $recipientKey |
||
202 | * @param array $recipientHeaders |
||
203 | * |
||
204 | * @return JWEBuilder |
||
205 | */ |
||
206 | public function addRecipient(JWK $recipientKey, array $recipientHeaders = []): JWEBuilder |
||
242 | |||
243 | /** |
||
244 | * @return JWE |
||
245 | */ |
||
246 | public function build(): JWE |
||
272 | |||
273 | /** |
||
274 | * @param array $completeHeaders |
||
275 | */ |
||
276 | private function checkAndSetContentEncryptionAlgorithm(array $completeHeaders): void |
||
285 | |||
286 | /** |
||
287 | * @param array $recipient |
||
288 | * @param string $cek |
||
289 | * @param array $additionalHeaders |
||
290 | * |
||
291 | * @return Recipient |
||
292 | */ |
||
293 | private function processRecipient(array $recipient, string $cek, array &$additionalHeaders): Recipient |
||
307 | |||
308 | /** |
||
309 | * @param string $cek |
||
310 | * @param string $encodedSharedProtectedHeaders |
||
311 | * |
||
312 | * @return array |
||
313 | */ |
||
314 | private function encryptJWE(string $cek, string $encodedSharedProtectedHeaders): array |
||
325 | |||
326 | /** |
||
327 | * @return string |
||
328 | */ |
||
329 | private function preparePayload(): ?string |
||
343 | |||
344 | /** |
||
345 | * @param array $completeHeaders |
||
346 | * @param string $cek |
||
347 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
348 | * @param JWK $recipientKey |
||
349 | * @param array $additionalHeaders |
||
350 | * |
||
351 | * @return string|null |
||
352 | */ |
||
353 | private function getEncryptedKey(array $completeHeaders, string $cek, KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): ?string |
||
369 | |||
370 | /** |
||
371 | * @param array $completeHeaders |
||
372 | * @param string $cek |
||
373 | * @param KeyAgreementWrappingInterface $keyEncryptionAlgorithm |
||
374 | * @param array $additionalHeaders |
||
375 | * @param JWK $recipientKey |
||
376 | * |
||
377 | * @return string |
||
378 | */ |
||
379 | private function getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyAgreementWrappingInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): string |
||
383 | |||
384 | /** |
||
385 | * @param array $completeHeaders |
||
386 | * @param string $cek |
||
387 | * @param KeyEncryptionInterface $keyEncryptionAlgorithm |
||
388 | * @param JWK $recipientKey |
||
389 | * @param array $additionalHeaders |
||
390 | * |
||
391 | * @return string |
||
392 | */ |
||
393 | private function getEncryptedKeyFromKeyEncryptionAlgorithm(array $completeHeaders, string $cek, KeyEncryptionInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string |
||
397 | |||
398 | /** |
||
399 | * @param array $completeHeaders |
||
400 | * @param string $cek |
||
401 | * @param KeyWrappingInterface $keyEncryptionAlgorithm |
||
402 | * @param JWK $recipientKey |
||
403 | * @param array $additionalHeaders |
||
404 | * |
||
405 | * @return string |
||
406 | */ |
||
407 | private function getEncryptedKeyFromKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyWrappingInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string |
||
411 | |||
412 | /** |
||
413 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
414 | * @param JWK $recipientKey |
||
415 | */ |
||
416 | private function checkKey(KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, JWK $recipientKey) |
||
425 | |||
426 | /** |
||
427 | * @param array $additionalHeaders |
||
428 | * |
||
429 | * @return string |
||
430 | */ |
||
431 | private function determineCEK(array &$additionalHeaders): string |
||
463 | |||
464 | /** |
||
465 | * @param array $completeHeaders |
||
466 | * |
||
467 | * @return CompressionMethodInterface|null |
||
468 | */ |
||
469 | private function getCompressionMethod(array $completeHeaders): ?CompressionMethodInterface |
||
477 | |||
478 | /** |
||
479 | * @param string $current |
||
480 | * @param string $new |
||
481 | * |
||
482 | * @return bool |
||
483 | */ |
||
484 | private function areKeyManagementModesCompatible(string $current, string $new): bool |
||
498 | |||
499 | /** |
||
500 | * @param int $size |
||
501 | * |
||
502 | * @return string |
||
503 | */ |
||
504 | private function createCEK(int $size): string |
||
508 | |||
509 | /** |
||
510 | * @param int $size |
||
511 | * |
||
512 | * @return string |
||
513 | */ |
||
514 | private function createIV(int $size): string |
||
518 | |||
519 | /** |
||
520 | * @param array $completeHeaders |
||
521 | * |
||
522 | * @return KeyEncryptionAlgorithmInterface |
||
523 | */ |
||
524 | private function getKeyEncryptionAlgorithm(array $completeHeaders): KeyEncryptionAlgorithmInterface |
||
536 | |||
537 | /** |
||
538 | * @param array $completeHeaders |
||
539 | * |
||
540 | * @return ContentEncryptionAlgorithmInterface |
||
541 | */ |
||
542 | private function getContentEncryptionAlgorithm(array $completeHeaders): ContentEncryptionAlgorithmInterface |
||
554 | |||
555 | /** |
||
556 | * @param array $header1 |
||
557 | * @param array $header2 |
||
558 | */ |
||
559 | private function checkDuplicatedHeaderParameters(array $header1, array $header2) |
||
566 | } |
||
567 |