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 | class JWEBuilder |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var JsonConverter |
||
| 35 | */ |
||
| 36 | private $jsonConverter; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var null|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 $sharedProtectedHeader = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | private $sharedHeader = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var null|CompressionMethod |
||
| 80 | */ |
||
| 81 | private $compressionMethod = null; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var null|ContentEncryptionAlgorithm |
||
| 85 | */ |
||
| 86 | private $contentEncryptionAlgorithm = null; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var null|string |
||
| 90 | */ |
||
| 91 | private $keyManagementMode = null; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * JWEBuilder constructor. |
||
| 95 | * |
||
| 96 | * @param JsonConverter $jsonConverter |
||
| 97 | * @param AlgorithmManager $keyEncryptionAlgorithmManager |
||
| 98 | * @param AlgorithmManager $contentEncryptionAlgorithmManager |
||
| 99 | * @param CompressionMethodManager $compressionManager |
||
| 100 | */ |
||
| 101 | public function __construct(JsonConverter $jsonConverter, AlgorithmManager $keyEncryptionAlgorithmManager, AlgorithmManager $contentEncryptionAlgorithmManager, CompressionMethodManager $compressionManager) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Reset the current data. |
||
| 111 | * |
||
| 112 | * @return JWEBuilder |
||
| 113 | */ |
||
| 114 | public function create(): self |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Returns the key encryption algorithm manager. |
||
| 130 | * |
||
| 131 | * @return AlgorithmManager |
||
| 132 | */ |
||
| 133 | public function getKeyEncryptionAlgorithmManager(): AlgorithmManager |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Returns the content encryption algorithm manager. |
||
| 140 | * |
||
| 141 | * @return AlgorithmManager |
||
| 142 | */ |
||
| 143 | public function getContentEncryptionAlgorithmManager(): AlgorithmManager |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Returns the compression method manager. |
||
| 150 | * |
||
| 151 | * @return CompressionMethodManager |
||
| 152 | */ |
||
| 153 | public function getCompressionMethodManager(): CompressionMethodManager |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Set the payload of the JWE to build. |
||
| 160 | * |
||
| 161 | * @param mixed $payload |
||
| 162 | * |
||
| 163 | * @return JWEBuilder |
||
| 164 | */ |
||
| 165 | public function withPayload($payload): self |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Set the Additional Authenticated Data of the JWE to build. |
||
| 179 | * |
||
| 180 | * @param string|null $aad |
||
| 181 | * |
||
| 182 | * @return JWEBuilder |
||
| 183 | */ |
||
| 184 | public function withAAD(?string $aad): self |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Set the shared protected header of the JWE to build. |
||
| 194 | * |
||
| 195 | * @param array $sharedProtectedHeader |
||
| 196 | * |
||
| 197 | * @return JWEBuilder |
||
| 198 | */ |
||
| 199 | public function withSharedProtectedHeader(array $sharedProtectedHeader): self |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Set the shared header of the JWE to build. |
||
| 213 | * |
||
| 214 | * @param array $sharedHeader |
||
| 215 | * |
||
| 216 | * @return JWEBuilder |
||
| 217 | */ |
||
| 218 | public function withSharedHeader(array $sharedHeader): self |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Adds a recipient to the JWE to build. |
||
| 232 | * |
||
| 233 | * @param JWK $recipientKey |
||
| 234 | * @param array $recipientHeader |
||
| 235 | * |
||
| 236 | * @return JWEBuilder |
||
| 237 | */ |
||
| 238 | public function addRecipient(JWK $recipientKey, array $recipientHeader = []): self |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Builds the JWE. |
||
| 277 | * |
||
| 278 | * @return JWE |
||
| 279 | */ |
||
| 280 | public function build(): JWE |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param array $completeHeader |
||
| 312 | */ |
||
| 313 | private function checkAndSetContentEncryptionAlgorithm(array $completeHeader): void |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param array $recipient |
||
| 325 | * @param string $cek |
||
| 326 | * @param array $additionalHeader |
||
| 327 | * |
||
| 328 | * @return Recipient |
||
| 329 | */ |
||
| 330 | private function processRecipient(array $recipient, string $cek, array &$additionalHeader): Recipient |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param string $cek |
||
| 347 | * @param string $encodedSharedProtectedHeader |
||
| 348 | * |
||
| 349 | * @return array |
||
| 350 | */ |
||
| 351 | private function encryptJWE(string $cek, string $encodedSharedProtectedHeader): array |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | private function preparePayload(): ?string |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param array $completeHeader |
||
| 380 | * @param string $cek |
||
| 381 | * @param KeyEncryptionAlgorithm $keyEncryptionAlgorithm |
||
| 382 | * @param JWK $recipientKey |
||
| 383 | * @param array $additionalHeader |
||
| 384 | * |
||
| 385 | * @return string|null |
||
| 386 | */ |
||
| 387 | private function getEncryptedKey(array $completeHeader, string $cek, KeyEncryptionAlgorithm $keyEncryptionAlgorithm, array &$additionalHeader, JWK $recipientKey): ?string |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param array $completeHeader |
||
| 406 | * @param string $cek |
||
| 407 | * @param KeyAgreementWithKeyWrapping $keyEncryptionAlgorithm |
||
| 408 | * @param array $additionalHeader |
||
| 409 | * @param JWK $recipientKey |
||
| 410 | * |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | private function getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm(array $completeHeader, string $cek, KeyAgreementWithKeyWrapping $keyEncryptionAlgorithm, array &$additionalHeader, JWK $recipientKey): string |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param array $completeHeader |
||
| 420 | * @param string $cek |
||
| 421 | * @param KeyEncryption $keyEncryptionAlgorithm |
||
| 422 | * @param JWK $recipientKey |
||
| 423 | * @param array $additionalHeader |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | private function getEncryptedKeyFromKeyEncryptionAlgorithm(array $completeHeader, string $cek, KeyEncryption $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeader): string |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param array $completeHeader |
||
| 434 | * @param string $cek |
||
| 435 | * @param KeyWrapping $keyEncryptionAlgorithm |
||
| 436 | * @param JWK $recipientKey |
||
| 437 | * @param array $additionalHeader |
||
| 438 | * |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | private function getEncryptedKeyFromKeyWrappingAlgorithm(array $completeHeader, string $cek, KeyWrapping $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeader): string |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @param KeyEncryptionAlgorithm $keyEncryptionAlgorithm |
||
| 448 | * @param JWK $recipientKey |
||
| 449 | */ |
||
| 450 | private function checkKey(KeyEncryptionAlgorithm $keyEncryptionAlgorithm, JWK $recipientKey) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param array $additionalHeader |
||
| 462 | * |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | private function determineCEK(array &$additionalHeader): string |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @param array $completeHeader |
||
| 500 | * |
||
| 501 | * @return CompressionMethod|null |
||
| 502 | */ |
||
| 503 | private function getCompressionMethod(array $completeHeader): ?CompressionMethod |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param string $current |
||
| 514 | * @param string $new |
||
| 515 | * |
||
| 516 | * @return bool |
||
| 517 | */ |
||
| 518 | private function areKeyManagementModesCompatible(string $current, string $new): bool |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @param int $size |
||
| 535 | * |
||
| 536 | * @return string |
||
| 537 | */ |
||
| 538 | private function createCEK(int $size): string |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @param int $size |
||
| 545 | * |
||
| 546 | * @return string |
||
| 547 | */ |
||
| 548 | private function createIV(int $size): string |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @param array $completeHeader |
||
| 555 | * |
||
| 556 | * @return KeyEncryptionAlgorithm |
||
| 557 | */ |
||
| 558 | private function getKeyEncryptionAlgorithm(array $completeHeader): KeyEncryptionAlgorithm |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @param array $completeHeader |
||
| 573 | * |
||
| 574 | * @return ContentEncryptionAlgorithm |
||
| 575 | */ |
||
| 576 | private function getContentEncryptionAlgorithm(array $completeHeader): ContentEncryptionAlgorithm |
||
| 588 | |||
| 589 | /** |
||
| 590 | * @param array $header1 |
||
| 591 | * @param array $header2 |
||
| 592 | */ |
||
| 593 | private function checkDuplicatedHeaderParameters(array $header1, array $header2) |
||
| 600 | } |
||
| 601 |