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 Recipient[] |
||
| 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 |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param string|null $aad |
||
| 143 | * |
||
| 144 | * @return JWEBuilder |
||
| 145 | */ |
||
| 146 | public function withAAD(?string $aad): JWEBuilder |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param array $sharedProtectedHeaders |
||
| 156 | * |
||
| 157 | * @return JWEBuilder |
||
| 158 | */ |
||
| 159 | public function withSharedProtectedHeaders(array $sharedProtectedHeaders): JWEBuilder |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param array $sharedHeaders |
||
| 173 | * |
||
| 174 | * @return JWEBuilder |
||
| 175 | */ |
||
| 176 | public function withSharedHeaders(array $sharedHeaders): JWEBuilder |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param JWK $recipientKey |
||
| 190 | * @param array $recipientHeaders |
||
| 191 | * |
||
| 192 | * @return JWEBuilder |
||
| 193 | */ |
||
| 194 | public function addRecipient(JWK $recipientKey, array $recipientHeaders = []): JWEBuilder |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return JWE |
||
| 233 | */ |
||
| 234 | public function build(): JWE |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param array $completeHeaders |
||
| 263 | */ |
||
| 264 | private function checkAndSetContentEncryptionAlgorithm(array $completeHeaders): void |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param array $recipient |
||
| 276 | * @param string $cek |
||
| 277 | * @param array $additionalHeaders |
||
| 278 | * |
||
| 279 | * @return Recipient |
||
| 280 | */ |
||
| 281 | private function processRecipient(array $recipient, string $cek, array &$additionalHeaders): Recipient |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param string $cek |
||
| 298 | * @param string $encodedSharedProtectedHeaders |
||
| 299 | * |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | private function encryptJWE(string $cek, string $encodedSharedProtectedHeaders): array |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | private function preparePayload(): ?string |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param array $completeHeaders |
||
| 337 | * @param string $cek |
||
| 338 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
| 339 | * @param JWK $recipientKey |
||
| 340 | * @param array $additionalHeaders |
||
| 341 | * |
||
| 342 | * @return string|null |
||
| 343 | */ |
||
| 344 | private function getEncryptedKey(array $completeHeaders, string $cek, KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): ?string |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param array $completeHeaders |
||
| 363 | * @param string $cek |
||
| 364 | * @param KeyAgreementWrappingInterface $keyEncryptionAlgorithm |
||
| 365 | * @param array $additionalHeaders |
||
| 366 | * @param JWK $recipientKey |
||
| 367 | * |
||
| 368 | * @return string |
||
| 369 | */ |
||
| 370 | private function getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyAgreementWrappingInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): string |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param array $completeHeaders |
||
| 377 | * @param string $cek |
||
| 378 | * @param KeyEncryptionInterface $keyEncryptionAlgorithm |
||
| 379 | * @param JWK $recipientKey |
||
| 380 | * @param array $additionalHeaders |
||
| 381 | * |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | private function getEncryptedKeyFromKeyEncryptionAlgorithm(array $completeHeaders, string $cek, KeyEncryptionInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param array $completeHeaders |
||
| 391 | * @param string $cek |
||
| 392 | * @param KeyWrappingInterface $keyEncryptionAlgorithm |
||
| 393 | * @param JWK $recipientKey |
||
| 394 | * @param array $additionalHeaders |
||
| 395 | * |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | private function getEncryptedKeyFromKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyWrappingInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
| 405 | * @param JWK $recipientKey |
||
| 406 | */ |
||
| 407 | private function checkKey(KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, JWK $recipientKey) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param array $additionalHeaders |
||
| 419 | * |
||
| 420 | * @return string |
||
| 421 | */ |
||
| 422 | private function determineCEK(array &$additionalHeaders): string |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param array $completeHeaders |
||
| 457 | * |
||
| 458 | * @return CompressionInterface|null |
||
| 459 | */ |
||
| 460 | private function getCompressionMethod(array $completeHeaders): ?CompressionInterface |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param string $current |
||
| 471 | * @param string $new |
||
| 472 | * |
||
| 473 | * @return bool |
||
| 474 | */ |
||
| 475 | private function areKeyManagementModesCompatible(string $current, string $new): bool |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @param int $size |
||
| 492 | * |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | private function createCEK(int $size): string |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @param int $size |
||
| 502 | * |
||
| 503 | * @return string |
||
| 504 | */ |
||
| 505 | private function createIV(int $size): string |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @param array $completeHeaders |
||
| 512 | * |
||
| 513 | * @return KeyEncryptionAlgorithmInterface |
||
| 514 | */ |
||
| 515 | private function getKeyEncryptionAlgorithm(array $completeHeaders): KeyEncryptionAlgorithmInterface |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param array $completeHeaders |
||
| 530 | * |
||
| 531 | * @return ContentEncryptionAlgorithmInterface |
||
| 532 | */ |
||
| 533 | private function getContentEncryptionAlgorithm(array $completeHeaders): ContentEncryptionAlgorithmInterface |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @param array $header1 |
||
| 548 | * @param array $header2 |
||
| 549 | */ |
||
| 550 | private function checkDuplicatedHeaderParameters(array $header1, array $header2) |
||
| 557 | } |
||
| 558 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: