Complex classes like JWE 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 JWE, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | final class JWE implements JWTInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var Recipient[] |
||
| 28 | */ |
||
| 29 | private $recipients = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string|null |
||
| 33 | */ |
||
| 34 | private $ciphertext = null; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string|null |
||
| 38 | */ |
||
| 39 | private $iv = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string|null |
||
| 43 | */ |
||
| 44 | private $aad = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string|null |
||
| 48 | */ |
||
| 49 | private $tag = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private $shared_headers = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private $shared_protected_headers = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string|null |
||
| 63 | */ |
||
| 64 | private $encoded_shared_protected_headers = null; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var mixed|null |
||
| 68 | */ |
||
| 69 | private $payload = null; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * JWE constructor. |
||
| 73 | * @param null|string $ciphertext |
||
| 74 | * @param null|string $iv |
||
| 75 | * @param null|string $aad |
||
| 76 | * @param null|string $tag |
||
| 77 | * @param array $shared_headers |
||
| 78 | * @param array $shared_protected_headers |
||
| 79 | * @param null|string $encoded_shared_protected_headers |
||
| 80 | * @param mixed|null $payload |
||
| 81 | * @param array $recipients |
||
| 82 | */ |
||
| 83 | private function __construct(?string $ciphertext = null, ?string $iv = null, ?string $aad = null, ?string $tag = null, array $shared_headers = [], array $shared_protected_headers = [], ?string $encoded_shared_protected_headers = null, $payload = null, array $recipients = []) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @return JWE |
||
| 98 | */ |
||
| 99 | public static function createEmpty(): JWE |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param $payload |
||
| 106 | * @param array $shared_protected_headers |
||
| 107 | * @param null|string $aad |
||
| 108 | * @param array $shared_headers |
||
| 109 | * |
||
| 110 | * @return JWE |
||
| 111 | */ |
||
| 112 | public static function create($payload, array $shared_protected_headers = [], array $shared_headers = [], ?string $aad = null): JWE |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param null|string $ciphertext |
||
| 119 | * @param null|string $iv |
||
| 120 | * @param null|string $aad |
||
| 121 | * @param null|string $tag |
||
| 122 | * @param array $shared_headers |
||
| 123 | * @param array $shared_protected_headers |
||
| 124 | * @param null|string $encoded_shared_protected_headers |
||
| 125 | * @param array $recipients |
||
| 126 | * |
||
| 127 | * @return JWE |
||
| 128 | */ |
||
| 129 | public static function createFromLoadedData(?string $ciphertext = null, ?string $iv = null, ?string $aad = null, ?string $tag = null, array $shared_headers = [], array $shared_protected_headers = [], ?string $encoded_shared_protected_headers = null, array $recipients = []): JWE |
||
| 133 | |||
| 134 | /** |
||
| 135 | * {@inheritdoc} |
||
| 136 | */ |
||
| 137 | public function getPayload() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param mixed $payload |
||
| 144 | * |
||
| 145 | * @return JWE |
||
| 146 | */ |
||
| 147 | public function withPayload($payload): JWE |
||
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritdoc} |
||
| 157 | */ |
||
| 158 | public function getClaim(string $key) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | public function getClaims(): array |
||
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | */ |
||
| 181 | public function hasClaim(string $key): bool |
||
| 185 | |||
| 186 | /** |
||
| 187 | * {@inheritdoc} |
||
| 188 | */ |
||
| 189 | public function hasClaims(): bool |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Returns the number of recipients associated with the JWS. |
||
| 196 | * |
||
| 197 | * @return int |
||
| 198 | */ |
||
| 199 | public function countRecipients(): int |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | public function isEncrypted(): bool |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param JWK $recipient_key |
||
| 214 | * @param array $recipient_headers |
||
| 215 | * |
||
| 216 | * @return JWE |
||
| 217 | */ |
||
| 218 | public function addRecipientInformation(JWK $recipient_key, array $recipient_headers = []): JWE |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string|null $encrypted_key |
||
| 229 | * @param array $recipient_headers |
||
| 230 | * |
||
| 231 | * @return JWE |
||
| 232 | */ |
||
| 233 | public function addRecipientWithEncryptedKey(?string $encrypted_key, array $recipient_headers): JWE |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Returns the recipients associated with the JWS. |
||
| 243 | * |
||
| 244 | * @return Recipient[] |
||
| 245 | */ |
||
| 246 | public function getRecipients(): array |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param int $id |
||
| 253 | * |
||
| 254 | * @return Recipient |
||
| 255 | */ |
||
| 256 | public function &getRecipient(int $id): Recipient |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return string|null The cyphertext |
||
| 265 | */ |
||
| 266 | public function getCiphertext(): ?string |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param string $ciphertext |
||
| 273 | * |
||
| 274 | * @return JWE |
||
| 275 | */ |
||
| 276 | public function withCiphertext(string $ciphertext): JWE |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @return string|null |
||
| 286 | */ |
||
| 287 | public function getAAD(): ?string |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param string $aad |
||
| 294 | * |
||
| 295 | * @return JWE |
||
| 296 | */ |
||
| 297 | public function withAAD(string $aad): JWE |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @return string|null |
||
| 307 | */ |
||
| 308 | public function getIV(): ?string |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $iv |
||
| 315 | * |
||
| 316 | * @return JWE |
||
| 317 | */ |
||
| 318 | public function withIV(string $iv): JWE |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return string|null |
||
| 328 | */ |
||
| 329 | public function getTag(): ?string |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param string $tag |
||
| 336 | * |
||
| 337 | * @return JWE |
||
| 338 | */ |
||
| 339 | public function withTag(string $tag): JWE |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | public function getEncodedSharedProtectedHeaders(): string |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param string $encoded_shared_protected_headers |
||
| 357 | * |
||
| 358 | * @return JWE |
||
| 359 | */ |
||
| 360 | public function withEncodedSharedProtectedHeaders(string $encoded_shared_protected_headers): JWE |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | public function getSharedProtectedHeaders(): array |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param array $shared_protected_headers |
||
| 378 | * |
||
| 379 | * @return JWE |
||
| 380 | */ |
||
| 381 | public function withSharedProtectedHeaders(array $shared_protected_headers): JWE |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param string $key |
||
| 391 | * @param mixed|null $value |
||
| 392 | * |
||
| 393 | * @return JWE |
||
| 394 | */ |
||
| 395 | public function withSharedProtectedHeader(string $key, $value): JWE |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param string $key The key |
||
| 405 | * |
||
| 406 | * @return mixed|null Header value |
||
| 407 | */ |
||
| 408 | public function getSharedProtectedHeader(string $key) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @param string $key The key |
||
| 418 | * |
||
| 419 | * @return bool |
||
| 420 | */ |
||
| 421 | public function hasSharedProtectedHeader(string $key): bool |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @param array $shared_headers |
||
| 428 | * |
||
| 429 | * @return JWE |
||
| 430 | */ |
||
| 431 | public function withSharedHeaders(array $shared_headers): JWE |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @param string $key |
||
| 441 | * @param mixed|null $value |
||
| 442 | * |
||
| 443 | * @return JWE |
||
| 444 | */ |
||
| 445 | public function withSharedHeader(string $key, $value): JWE |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @return array |
||
| 455 | */ |
||
| 456 | public function getSharedHeaders(): array |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @param string $key The key |
||
| 463 | * |
||
| 464 | * @return mixed|null Header value |
||
| 465 | */ |
||
| 466 | public function getSharedHeader(string $key) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param string $key The key |
||
| 476 | * |
||
| 477 | * @return bool |
||
| 478 | */ |
||
| 479 | public function hasSharedHeader(string $key): bool |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param int $id |
||
| 486 | * |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | public function toCompactJSON(int $id): string |
||
| 506 | |||
| 507 | private function checkHasNoAAD() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param int $id |
||
| 514 | */ |
||
| 515 | private function checkRecipientHasNoHeaders(int $id) |
||
| 522 | |||
| 523 | private function checkHasSharedProtectedHeaders() |
||
| 530 | |||
| 531 | /** |
||
| 532 | * @param int $id |
||
| 533 | * |
||
| 534 | * @return string |
||
| 535 | */ |
||
| 536 | public function toFlattenedJSON(int $id): string |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @return string |
||
| 554 | */ |
||
| 555 | public function toJSON(): string |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @return array |
||
| 576 | */ |
||
| 577 | private function getJSONBase(): array |
||
| 600 | } |
||
| 601 |