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 AlgorithmManager |
||
| 55 | */ |
||
| 56 | private $keyEncryptionAlgorithmManager; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var AlgorithmManager |
||
| 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 | * |
||
| 96 | * @param JsonConverterInterface $jsonConverter |
||
| 97 | * @param AlgorithmManager $keyEncryptionAlgorithmManager |
||
| 98 | * @param AlgorithmManager $contentEncryptionAlgorithmManager |
||
| 99 | * @param CompressionMethodManager $compressionManager |
||
| 100 | */ |
||
| 101 | public function __construct(JsonConverterInterface $jsonConverter, AlgorithmManager $keyEncryptionAlgorithmManager, AlgorithmManager $contentEncryptionAlgorithmManager, CompressionMethodManager $compressionManager) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return string[] |
||
| 111 | */ |
||
| 112 | public function getSupportedKeyEncryptionAlgorithms(): array |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return string[] |
||
| 119 | */ |
||
| 120 | public function getSupportedContentEncryptionAlgorithms(): array |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return string[] |
||
| 127 | */ |
||
| 128 | public function getSupportedCompressionMethods(): array |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param mixed $payload |
||
| 135 | * |
||
| 136 | * @return JWEBuilder |
||
| 137 | */ |
||
| 138 | public function withPayload($payload): JWEBuilder |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param string|null $aad |
||
| 152 | * |
||
| 153 | * @return JWEBuilder |
||
| 154 | */ |
||
| 155 | public function withAAD(?string $aad): JWEBuilder |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param array $sharedProtectedHeaders |
||
| 165 | * |
||
| 166 | * @return JWEBuilder |
||
| 167 | */ |
||
| 168 | public function withSharedProtectedHeaders(array $sharedProtectedHeaders): JWEBuilder |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param array $sharedHeaders |
||
| 182 | * |
||
| 183 | * @return JWEBuilder |
||
| 184 | */ |
||
| 185 | public function withSharedHeaders(array $sharedHeaders): JWEBuilder |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param JWK $recipientKey |
||
| 199 | * @param array $recipientHeaders |
||
| 200 | * |
||
| 201 | * @return JWEBuilder |
||
| 202 | */ |
||
| 203 | public function addRecipient(JWK $recipientKey, array $recipientHeaders = []): JWEBuilder |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @return JWE |
||
| 242 | */ |
||
| 243 | public function build(): JWE |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param array $completeHeaders |
||
| 272 | */ |
||
| 273 | private function checkAndSetContentEncryptionAlgorithm(array $completeHeaders): void |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param array $recipient |
||
| 285 | * @param string $cek |
||
| 286 | * @param array $additionalHeaders |
||
| 287 | * |
||
| 288 | * @return Recipient |
||
| 289 | */ |
||
| 290 | private function processRecipient(array $recipient, string $cek, array &$additionalHeaders): Recipient |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param string $cek |
||
| 307 | * @param string $encodedSharedProtectedHeaders |
||
| 308 | * |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | private function encryptJWE(string $cek, string $encodedSharedProtectedHeaders): array |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | private function preparePayload(): ?string |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param array $completeHeaders |
||
| 343 | * @param string $cek |
||
| 344 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
| 345 | * @param JWK $recipientKey |
||
| 346 | * @param array $additionalHeaders |
||
| 347 | * |
||
| 348 | * @return string|null |
||
| 349 | */ |
||
| 350 | private function getEncryptedKey(array $completeHeaders, string $cek, KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): ?string |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param array $completeHeaders |
||
| 369 | * @param string $cek |
||
| 370 | * @param KeyAgreementWrappingInterface $keyEncryptionAlgorithm |
||
| 371 | * @param array $additionalHeaders |
||
| 372 | * @param JWK $recipientKey |
||
| 373 | * |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | private function getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyAgreementWrappingInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): string |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param array $completeHeaders |
||
| 383 | * @param string $cek |
||
| 384 | * @param KeyEncryptionInterface $keyEncryptionAlgorithm |
||
| 385 | * @param JWK $recipientKey |
||
| 386 | * @param array $additionalHeaders |
||
| 387 | * |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | private function getEncryptedKeyFromKeyEncryptionAlgorithm(array $completeHeaders, string $cek, KeyEncryptionInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param array $completeHeaders |
||
| 397 | * @param string $cek |
||
| 398 | * @param KeyWrappingInterface $keyEncryptionAlgorithm |
||
| 399 | * @param JWK $recipientKey |
||
| 400 | * @param array $additionalHeaders |
||
| 401 | * |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | private function getEncryptedKeyFromKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyWrappingInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
| 411 | * @param JWK $recipientKey |
||
| 412 | */ |
||
| 413 | private function checkKey(KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, JWK $recipientKey) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param array $additionalHeaders |
||
| 425 | * |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | private function determineCEK(array &$additionalHeaders): string |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @param array $completeHeaders |
||
| 463 | * |
||
| 464 | * @return CompressionMethodInterface|null |
||
| 465 | */ |
||
| 466 | private function getCompressionMethod(array $completeHeaders): ?CompressionMethodInterface |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @param string $current |
||
| 477 | * @param string $new |
||
| 478 | * |
||
| 479 | * @return bool |
||
| 480 | */ |
||
| 481 | private function areKeyManagementModesCompatible(string $current, string $new): bool |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @param int $size |
||
| 498 | * |
||
| 499 | * @return string |
||
| 500 | */ |
||
| 501 | private function createCEK(int $size): string |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param int $size |
||
| 508 | * |
||
| 509 | * @return string |
||
| 510 | */ |
||
| 511 | private function createIV(int $size): string |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @param array $completeHeaders |
||
| 518 | * |
||
| 519 | * @return KeyEncryptionAlgorithmInterface |
||
| 520 | */ |
||
| 521 | private function getKeyEncryptionAlgorithm(array $completeHeaders): KeyEncryptionAlgorithmInterface |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @param array $completeHeaders |
||
| 536 | * |
||
| 537 | * @return ContentEncryptionAlgorithmInterface |
||
| 538 | */ |
||
| 539 | private function getContentEncryptionAlgorithm(array $completeHeaders): ContentEncryptionAlgorithmInterface |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @param array $header1 |
||
| 554 | * @param array $header2 |
||
| 555 | */ |
||
| 556 | private function checkDuplicatedHeaderParameters(array $header1, array $header2) |
||
| 563 | } |
||
| 564 |