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 |
||
| 30 | final class JWEBuilder |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var mixed |
||
| 34 | */ |
||
| 35 | private $payload; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string|null |
||
| 39 | */ |
||
| 40 | private $aad; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private $recipients = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var JWAManager |
||
| 49 | */ |
||
| 50 | private $keyEncryptionAlgorithmManager; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var JWAManager |
||
| 54 | */ |
||
| 55 | private $contentEncryptionAlgorithmManager; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var CompressionMethodsManager |
||
| 59 | */ |
||
| 60 | private $compressionManager; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $sharedProtectedHeaders = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | private $sharedHeaders = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var null|CompressionInterface |
||
| 74 | */ |
||
| 75 | private $compressionMethod = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var null|ContentEncryptionAlgorithmInterface |
||
| 79 | */ |
||
| 80 | private $contentEncryptionAlgorithm = null; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var null|string |
||
| 84 | */ |
||
| 85 | private $keyManagementMode = null; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * JWEBuilder constructor. |
||
| 89 | * |
||
| 90 | * @param JWAManager $keyEncryptionAlgorithmManager |
||
| 91 | * @param JWAManager $contentEncryptionAlgorithmManager |
||
| 92 | * @param CompressionMethodsManager $compressionManager |
||
| 93 | */ |
||
| 94 | public function __construct(JWAManager $keyEncryptionAlgorithmManager, JWAManager $contentEncryptionAlgorithmManager, CompressionMethodsManager $compressionManager) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return string[] |
||
| 103 | */ |
||
| 104 | public function getSupportedKeyEncryptionAlgorithms(): array |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return string[] |
||
| 111 | */ |
||
| 112 | public function getSupportedContentEncryptionAlgorithms(): array |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return string[] |
||
| 119 | */ |
||
| 120 | public function getSupportedCompressionMethods(): array |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param mixed $payload |
||
| 127 | * |
||
| 128 | * @return JWEBuilder |
||
| 129 | */ |
||
| 130 | public function withPayload($payload): JWEBuilder |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param string|null $aad |
||
| 140 | * |
||
| 141 | * @return JWEBuilder |
||
| 142 | */ |
||
| 143 | public function withAAD(?string $aad): JWEBuilder |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param array $sharedProtectedHeaders |
||
| 153 | * |
||
| 154 | * @return JWEBuilder |
||
| 155 | */ |
||
| 156 | public function withSharedProtectedHeaders(array $sharedProtectedHeaders): JWEBuilder |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param array $sharedHeaders |
||
| 166 | * |
||
| 167 | * @return JWEBuilder |
||
| 168 | */ |
||
| 169 | public function withSharedHeaders(array $sharedHeaders): JWEBuilder |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param JWK $recipientKey |
||
| 179 | * @param array $recipientHeaders |
||
| 180 | * |
||
| 181 | * @return JWEBuilder |
||
| 182 | */ |
||
| 183 | public function addRecipient(JWK $recipientKey, array $recipientHeaders = []): JWEBuilder |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return JWE |
||
| 220 | */ |
||
| 221 | public function build(): JWE |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param array $completeHeaders |
||
| 250 | */ |
||
| 251 | private function checkAndSetContentEncryptionAlgorithm(array $completeHeaders): void |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param array $recipient |
||
| 263 | * @param string $cek |
||
| 264 | * @param array $additionalHeaders |
||
| 265 | * |
||
| 266 | * @return Recipient |
||
| 267 | */ |
||
| 268 | private function processRecipient(array $recipient, string $cek, array &$additionalHeaders): Recipient |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param string $cek |
||
| 285 | * @param string $encodedSharedProtectedHeaders |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | private function encryptJWE(string $cek, string $encodedSharedProtectedHeaders): array |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | private function preparePayload(): ?string |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param array $completeHeaders |
||
| 324 | * @param string $cek |
||
| 325 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
| 326 | * @param JWK $recipientKey |
||
| 327 | * @param array $additionalHeaders |
||
| 328 | * |
||
| 329 | * @return string|null |
||
| 330 | */ |
||
| 331 | private function getEncryptedKey(array $completeHeaders, string $cek, KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): ?string |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param array $completeHeaders |
||
| 349 | * @param string $cek |
||
| 350 | * @param KeyAgreementWrappingInterface $keyEncryptionAlgorithm |
||
| 351 | * @param array $additionalHeaders |
||
| 352 | * @param JWK $recipientKey |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | private function getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyAgreementWrappingInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): string |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param array $completeHeaders |
||
| 363 | * @param string $cek |
||
| 364 | * @param KeyEncryptionInterface $keyEncryptionAlgorithm |
||
| 365 | * @param JWK $recipientKey |
||
| 366 | * @param array $additionalHeaders |
||
| 367 | * |
||
| 368 | * @return string |
||
| 369 | */ |
||
| 370 | private function getEncryptedKeyFromKeyEncryptionAlgorithm(array $completeHeaders, string $cek, KeyEncryptionInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param array $completeHeaders |
||
| 377 | * @param string $cek |
||
| 378 | * @param KeyWrappingInterface $keyEncryptionAlgorithm |
||
| 379 | * @param JWK $recipientKey |
||
| 380 | * @param array $additionalHeaders |
||
| 381 | * |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | private function getEncryptedKeyFromKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyWrappingInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
| 391 | * @param JWK $recipientKey |
||
| 392 | */ |
||
| 393 | private function checkKey(KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, JWK $recipientKey) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param array $additionalHeaders |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | private function determineCEK(array &$additionalHeaders): string |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @param array $completeHeaders |
||
| 443 | * |
||
| 444 | * @return CompressionInterface|null |
||
| 445 | */ |
||
| 446 | private function getCompressionMethod(array $completeHeaders): ?CompressionInterface |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param string $current |
||
| 457 | * @param string $new |
||
| 458 | * |
||
| 459 | * @return bool |
||
| 460 | */ |
||
| 461 | private function areKeyManagementModesCompatible(string $current, string $new): bool |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @param int $size |
||
| 478 | * |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | private function createCEK(int $size): string |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param int $size |
||
| 488 | * |
||
| 489 | * @return string |
||
| 490 | */ |
||
| 491 | private function createIV(int $size): string |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @param array $completeHeaders |
||
| 498 | * |
||
| 499 | * @return KeyEncryptionAlgorithmInterface |
||
| 500 | */ |
||
| 501 | private function getKeyEncryptionAlgorithm(array $completeHeaders): KeyEncryptionAlgorithmInterface |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @param array $completeHeaders |
||
| 516 | * |
||
| 517 | * @return ContentEncryptionAlgorithmInterface |
||
| 518 | */ |
||
| 519 | private function getContentEncryptionAlgorithm(array $completeHeaders): ContentEncryptionAlgorithmInterface |
||
| 531 | } |
||
| 532 |