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 |
||
| 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 |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param array $sharedHeaders |
||
| 170 | * |
||
| 171 | * @return JWEBuilder |
||
| 172 | */ |
||
| 173 | public function withSharedHeaders(array $sharedHeaders): JWEBuilder |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param JWK $recipientKey |
||
| 187 | * @param array $recipientHeaders |
||
| 188 | * |
||
| 189 | * @return JWEBuilder |
||
| 190 | */ |
||
| 191 | public function addRecipient(JWK $recipientKey, array $recipientHeaders = []): JWEBuilder |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return JWE |
||
| 230 | */ |
||
| 231 | public function build(): JWE |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param array $completeHeaders |
||
| 260 | */ |
||
| 261 | private function checkAndSetContentEncryptionAlgorithm(array $completeHeaders): void |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param array $recipient |
||
| 273 | * @param string $cek |
||
| 274 | * @param array $additionalHeaders |
||
| 275 | * |
||
| 276 | * @return Recipient |
||
| 277 | */ |
||
| 278 | private function processRecipient(array $recipient, string $cek, array &$additionalHeaders): Recipient |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param string $cek |
||
| 295 | * @param string $encodedSharedProtectedHeaders |
||
| 296 | * |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | private function encryptJWE(string $cek, string $encodedSharedProtectedHeaders): array |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | private function preparePayload(): ?string |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param array $completeHeaders |
||
| 334 | * @param string $cek |
||
| 335 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
| 336 | * @param JWK $recipientKey |
||
| 337 | * @param array $additionalHeaders |
||
| 338 | * |
||
| 339 | * @return string|null |
||
| 340 | */ |
||
| 341 | private function getEncryptedKey(array $completeHeaders, string $cek, KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): ?string |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param array $completeHeaders |
||
| 360 | * @param string $cek |
||
| 361 | * @param KeyAgreementWrappingInterface $keyEncryptionAlgorithm |
||
| 362 | * @param array $additionalHeaders |
||
| 363 | * @param JWK $recipientKey |
||
| 364 | * |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | private function getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyAgreementWrappingInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): string |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param array $completeHeaders |
||
| 374 | * @param string $cek |
||
| 375 | * @param KeyEncryptionInterface $keyEncryptionAlgorithm |
||
| 376 | * @param JWK $recipientKey |
||
| 377 | * @param array $additionalHeaders |
||
| 378 | * |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | private function getEncryptedKeyFromKeyEncryptionAlgorithm(array $completeHeaders, string $cek, KeyEncryptionInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param array $completeHeaders |
||
| 388 | * @param string $cek |
||
| 389 | * @param KeyWrappingInterface $keyEncryptionAlgorithm |
||
| 390 | * @param JWK $recipientKey |
||
| 391 | * @param array $additionalHeaders |
||
| 392 | * |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | private function getEncryptedKeyFromKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyWrappingInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
| 402 | * @param JWK $recipientKey |
||
| 403 | */ |
||
| 404 | private function checkKey(KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, JWK $recipientKey) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @param array $additionalHeaders |
||
| 416 | * |
||
| 417 | * @return string |
||
| 418 | */ |
||
| 419 | private function determineCEK(array &$additionalHeaders): string |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param array $completeHeaders |
||
| 454 | * |
||
| 455 | * @return CompressionInterface|null |
||
| 456 | */ |
||
| 457 | private function getCompressionMethod(array $completeHeaders): ?CompressionInterface |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param string $current |
||
| 468 | * @param string $new |
||
| 469 | * |
||
| 470 | * @return bool |
||
| 471 | */ |
||
| 472 | private function areKeyManagementModesCompatible(string $current, string $new): bool |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @param int $size |
||
| 489 | * |
||
| 490 | * @return string |
||
| 491 | */ |
||
| 492 | private function createCEK(int $size): string |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @param int $size |
||
| 499 | * |
||
| 500 | * @return string |
||
| 501 | */ |
||
| 502 | private function createIV(int $size): string |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @param array $completeHeaders |
||
| 509 | * |
||
| 510 | * @return KeyEncryptionAlgorithmInterface |
||
| 511 | */ |
||
| 512 | private function getKeyEncryptionAlgorithm(array $completeHeaders): KeyEncryptionAlgorithmInterface |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @param array $completeHeaders |
||
| 527 | * |
||
| 528 | * @return ContentEncryptionAlgorithmInterface |
||
| 529 | */ |
||
| 530 | private function getContentEncryptionAlgorithm(array $completeHeaders): ContentEncryptionAlgorithmInterface |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @param array $header1 |
||
| 545 | * @param array $header2 |
||
| 546 | */ |
||
| 547 | private function checkDuplicatedHeaderParameters(array $header1, array $header2) |
||
| 554 | } |
||
| 555 |
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: