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 |
||
| 28 | final class JWEBuilder |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var mixed |
||
| 32 | */ |
||
| 33 | private $payload; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string|null |
||
| 37 | */ |
||
| 38 | private $aad; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | private $recipients = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var JWAManager |
||
| 47 | */ |
||
| 48 | private $keyEncryptionAlgorithmManager; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var JWAManager |
||
| 52 | */ |
||
| 53 | private $contentEncryptionAlgorithmManager; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var CompressionManager |
||
| 57 | */ |
||
| 58 | private $compressionManager; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | private $sharedProtectedHeaders = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | private $sharedHeaders = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var null|CompressionInterface |
||
| 72 | */ |
||
| 73 | private $compressionMethod = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var null|ContentEncryptionAlgorithmInterface |
||
| 77 | */ |
||
| 78 | private $contentEncryptionAlgorithm = null; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var null|string |
||
| 82 | */ |
||
| 83 | private $keyManagementMode = null; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * JWEBuilder constructor. |
||
| 87 | * |
||
| 88 | * @param JWAManager $keyEncryptionAlgorithmManager |
||
| 89 | * @param JWAManager $contentEncryptionAlgorithmManager |
||
| 90 | * @param CompressionManager $compressionManager |
||
| 91 | */ |
||
| 92 | public function __construct(JWAManager $keyEncryptionAlgorithmManager, JWAManager $contentEncryptionAlgorithmManager, CompressionManager $compressionManager) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return string[] |
||
| 101 | */ |
||
| 102 | public function getSupportedKeyEncryptionAlgorithms(): array |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return string[] |
||
| 109 | */ |
||
| 110 | public function getSupportedContentEncryptionAlgorithms(): array |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return string[] |
||
| 117 | */ |
||
| 118 | public function getSupportedCompressionMethods(): array |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param mixed $payload |
||
| 125 | * |
||
| 126 | * @return JWEBuilder |
||
| 127 | */ |
||
| 128 | public function withPayload($payload): JWEBuilder |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param string $aad |
||
| 138 | * |
||
| 139 | * @return JWEBuilder |
||
| 140 | */ |
||
| 141 | public function withAAD(string $aad): JWEBuilder |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param array $sharedProtectedHeaders |
||
| 151 | * |
||
| 152 | * @return JWEBuilder |
||
| 153 | */ |
||
| 154 | public function withSharedProtectedHeaders(array $sharedProtectedHeaders): JWEBuilder |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param array $sharedHeaders |
||
| 164 | * |
||
| 165 | * @return JWEBuilder |
||
| 166 | */ |
||
| 167 | public function withSharedHeaders(array $sharedHeaders): JWEBuilder |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param JWK $recipientKey |
||
| 177 | * @param array $recipientHeaders |
||
| 178 | * |
||
| 179 | * @return JWEBuilder |
||
| 180 | */ |
||
| 181 | public function addRecipient(JWK $recipientKey, array $recipientHeaders = []): JWEBuilder |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return JWE |
||
| 199 | */ |
||
| 200 | public function build(): JWE |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param array $completeHeaders |
||
| 226 | */ |
||
| 227 | private function checkContentEncryptionAlgorithm(array $completeHeaders): void |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param JWE $jwe |
||
| 239 | * @param Recipient $recipient |
||
| 240 | * @param string $cek |
||
| 241 | * @param ContentEncryptionAlgorithmInterface $contentEncryptionAlgorithm |
||
| 242 | * @param array $additionalHeaders |
||
| 243 | */ |
||
| 244 | private function processRecipient(JWE $jwe, Recipient &$recipient, $cek, ContentEncryptionAlgorithmInterface $contentEncryptionAlgorithm, array &$additionalHeaders) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param JWE $jwe |
||
| 263 | * @param ContentEncryptionAlgorithmInterface $contentEncryptionAlgorithm |
||
| 264 | * @param string $cek |
||
| 265 | * @param string $iv |
||
| 266 | * @param CompressionInterface|null $compressionMethod |
||
| 267 | */ |
||
| 268 | private function encryptJWE(JWE &$jwe, ContentEncryptionAlgorithmInterface $contentEncryptionAlgorithm, $cek, $iv, CompressionInterface $compressionMethod = null) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $payload |
||
| 288 | * @param CompressionInterface|null $compressionMethod |
||
| 289 | * |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | private function preparePayload($payload, CompressionInterface $compressionMethod = null) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param array $completeHeaders |
||
| 312 | * @param string $cek |
||
| 313 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
| 314 | * @param ContentEncryptionAlgorithmInterface $contentEncryptionAlgorithm |
||
| 315 | * @param JWK $recipientKey |
||
| 316 | * @param array $additionalHeaders |
||
| 317 | * |
||
| 318 | * @return string|null |
||
| 319 | */ |
||
| 320 | private function getEncryptedKey(array $completeHeaders, $cek, KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, ContentEncryptionAlgorithmInterface $contentEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param array $completeHeaders |
||
| 334 | * @param string $cek |
||
| 335 | * @param KeyAgreementWrappingInterface $keyEncryptionAlgorithm |
||
| 336 | * @param ContentEncryptionAlgorithmInterface $contentEncryptionAlgorithm |
||
| 337 | * @param array $additionalHeaders |
||
| 338 | * @param JWK $recipientKey |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | private function getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm(array $completeHeaders, $cek, KeyAgreementWrappingInterface $keyEncryptionAlgorithm, ContentEncryptionAlgorithmInterface $contentEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param array $completeHeaders |
||
| 351 | * @param string $cek |
||
| 352 | * @param KeyEncryptionInterface $keyEncryptionAlgorithm |
||
| 353 | * @param JWK $recipientKey |
||
| 354 | * @param array $additionalHeaders |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | private function getEncryptedKeyFromKeyEncryptionAlgorithm(array $completeHeaders, $cek, KeyEncryptionInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param array $completeHeaders |
||
| 365 | * @param string $cek |
||
| 366 | * @param KeyWrappingInterface $keyEncryptionAlgorithm |
||
| 367 | * @param JWK $recipientKey |
||
| 368 | * @param array $additionalHeaders |
||
| 369 | * |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | private function getEncryptedKeyFromKeyWrappingAlgorithm(array $completeHeaders, $cek, KeyWrappingInterface $keyEncryptionAlgorithm, JWK $recipientKey, &$additionalHeaders) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm |
||
| 379 | * @param ContentEncryptionAlgorithmInterface $contentEncryptionAlgorithm |
||
| 380 | * @param JWK $recipientKey |
||
| 381 | */ |
||
| 382 | private function checkKeys(KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, ContentEncryptionAlgorithmInterface $contentEncryptionAlgorithm, JWK $recipientKey) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param JWE $jwe |
||
| 394 | * @param ContentEncryptionAlgorithmInterface $contentEncryptionAlgorithm |
||
| 395 | * @param array $additionalHeaders |
||
| 396 | * |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | private function determineCEK(JWE $jwe, ContentEncryptionAlgorithmInterface $contentEncryptionAlgorithm, array &$additionalHeaders): string |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @param array $completeHeaders |
||
| 431 | * |
||
| 432 | * @return CompressionInterface|null |
||
| 433 | */ |
||
| 434 | private function getCompressionMethod(array $completeHeaders): ?CompressionInterface |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @param string $current |
||
| 445 | * @param string $new |
||
| 446 | * |
||
| 447 | * @return bool |
||
| 448 | */ |
||
| 449 | private function areKeyManagementModesCompatible(string $current, string $new): bool |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param int $size |
||
| 466 | * |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | private function createCEK(int $size): string |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param int $size |
||
| 476 | * |
||
| 477 | * @return string |
||
| 478 | */ |
||
| 479 | private function createIV(int $size): string |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param array $completeHeaders |
||
| 486 | * |
||
| 487 | * @return KeyEncryptionAlgorithmInterface |
||
| 488 | */ |
||
| 489 | private function getKeyEncryptionAlgorithm(array $completeHeaders): KeyEncryptionAlgorithmInterface |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @param array $completeHeaders |
||
| 504 | * |
||
| 505 | * @return ContentEncryptionAlgorithmInterface |
||
| 506 | */ |
||
| 507 | private function getContentEncryptionAlgorithm(array $completeHeaders): ContentEncryptionAlgorithmInterface |
||
| 519 | } |
||
| 520 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: