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 $sharedHeaders = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private $sharedProtectedKeaders = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string|null |
||
| 63 | */ |
||
| 64 | private $encodedSharedProtectedKeaders = null; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var mixed|null |
||
| 68 | */ |
||
| 69 | private $payload = null; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * JWE constructor. |
||
| 73 | * |
||
| 74 | * @param null|string $ciphertext |
||
| 75 | * @param null|string $iv |
||
| 76 | * @param null|string $aad |
||
| 77 | * @param null|string $tag |
||
| 78 | * @param array $sharedHeaders |
||
| 79 | * @param array $sharedProtectedKeaders |
||
| 80 | * @param null|string $encodedSharedProtectedKeaders |
||
| 81 | * @param mixed|null $payload |
||
| 82 | * @param array $recipients |
||
| 83 | */ |
||
| 84 | private function __construct(?string $ciphertext = null, ?string $iv = null, ?string $aad = null, ?string $tag = null, array $sharedHeaders = [], array $sharedProtectedKeaders = [], ?string $encodedSharedProtectedKeaders = null, $payload = null, array $recipients = []) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return JWE |
||
| 99 | */ |
||
| 100 | public static function createEmpty(): JWE |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param null|string $ciphertext |
||
| 107 | * @param null|string $iv |
||
| 108 | * @param null|string $aad |
||
| 109 | * @param null|string $tag |
||
| 110 | * @param array $sharedHeaders |
||
| 111 | * @param array $sharedProtectedKeaders |
||
| 112 | * @param null|string $encodedSharedProtectedKeaders |
||
| 113 | * @param array $recipients |
||
| 114 | * |
||
| 115 | * @return JWE |
||
| 116 | */ |
||
| 117 | public static function create(?string $ciphertext = null, ?string $iv = null, ?string $aad = null, ?string $tag = null, array $sharedHeaders = [], array $sharedProtectedKeaders = [], ?string $encodedSharedProtectedKeaders = null, array $recipients = []): JWE |
||
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | public function getPayload() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param mixed $payload |
||
| 132 | * |
||
| 133 | * @return JWE |
||
| 134 | */ |
||
| 135 | public function withPayload($payload): JWE |
||
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritdoc} |
||
| 145 | */ |
||
| 146 | public function getClaim(string $key) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritdoc} |
||
| 156 | */ |
||
| 157 | public function getClaims(): array |
||
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | public function hasClaim(string $key): bool |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | public function hasClaims(): bool |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Returns the number of recipients associated with the JWS. |
||
| 184 | * |
||
| 185 | * @return int |
||
| 186 | */ |
||
| 187 | public function countRecipients(): int |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | public function isEncrypted(): bool |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @param array $recipientHeaders |
||
| 202 | * @param string|null $encryptedKey |
||
| 203 | * |
||
| 204 | * @return JWE |
||
| 205 | */ |
||
| 206 | public function addRecipient(array $recipientHeaders = [], ?string $encryptedKey = null): JWE |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Returns the recipients associated with the JWS. |
||
| 216 | * |
||
| 217 | * @return Recipient[] |
||
| 218 | */ |
||
| 219 | public function getRecipients(): array |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param int $id |
||
| 226 | * |
||
| 227 | * @return Recipient |
||
| 228 | */ |
||
| 229 | public function getRecipient(int $id): Recipient |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return string|null The cyphertext |
||
| 238 | */ |
||
| 239 | public function getCiphertext(): ?string |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $ciphertext |
||
| 246 | * |
||
| 247 | * @return JWE |
||
| 248 | */ |
||
| 249 | public function withCiphertext(string $ciphertext): JWE |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return string|null |
||
| 259 | */ |
||
| 260 | public function getAAD(): ?string |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $aad |
||
| 267 | * |
||
| 268 | * @return JWE |
||
| 269 | */ |
||
| 270 | public function withAAD(string $aad): JWE |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @return string|null |
||
| 280 | */ |
||
| 281 | public function getIV(): ?string |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $iv |
||
| 288 | * |
||
| 289 | * @return JWE |
||
| 290 | */ |
||
| 291 | public function withIV(string $iv): JWE |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return string|null |
||
| 301 | */ |
||
| 302 | public function getTag(): ?string |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param string $tag |
||
| 309 | * |
||
| 310 | * @return JWE |
||
| 311 | */ |
||
| 312 | public function withTag(string $tag): JWE |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | public function getEncodedSharedProtectedHeaders(): string |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param string $encodedSharedProtectedKeaders |
||
| 330 | * |
||
| 331 | * @return JWE |
||
| 332 | */ |
||
| 333 | public function withEncodedSharedProtectedHeaders(string $encodedSharedProtectedKeaders): JWE |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @return array |
||
| 343 | */ |
||
| 344 | public function getSharedProtectedHeaders(): array |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param array $sharedProtectedKeaders |
||
| 351 | * |
||
| 352 | * @return JWE |
||
| 353 | */ |
||
| 354 | public function withSharedProtectedHeaders(array $sharedProtectedKeaders): JWE |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param string $key |
||
| 364 | * @param mixed|null $value |
||
| 365 | * |
||
| 366 | * @return JWE |
||
| 367 | */ |
||
| 368 | public function withSharedProtectedHeader(string $key, $value): JWE |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param string $key The key |
||
| 378 | * |
||
| 379 | * @return mixed|null Header value |
||
| 380 | */ |
||
| 381 | public function getSharedProtectedHeader(string $key) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param string $key The key |
||
| 391 | * |
||
| 392 | * @return bool |
||
| 393 | */ |
||
| 394 | public function hasSharedProtectedHeader(string $key): bool |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param array $sharedHeaders |
||
| 401 | * |
||
| 402 | * @return JWE |
||
| 403 | */ |
||
| 404 | public function withSharedHeaders(array $sharedHeaders): JWE |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @param string $key |
||
| 414 | * @param mixed|null $value |
||
| 415 | * |
||
| 416 | * @return JWE |
||
| 417 | */ |
||
| 418 | public function withSharedHeader(string $key, $value): JWE |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @return array |
||
| 428 | */ |
||
| 429 | public function getSharedHeaders(): array |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param string $key The key |
||
| 436 | * |
||
| 437 | * @return mixed|null Header value |
||
| 438 | */ |
||
| 439 | public function getSharedHeader(string $key) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param string $key The key |
||
| 449 | * |
||
| 450 | * @return bool |
||
| 451 | */ |
||
| 452 | public function hasSharedHeader(string $key): bool |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param int $id |
||
| 459 | * |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | public function toCompactJSON(int $id): string |
||
| 479 | |||
| 480 | private function checkHasNoAAD() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @param int $id |
||
| 487 | */ |
||
| 488 | private function checkRecipientHasNoHeaders(int $id) |
||
| 495 | |||
| 496 | private function checkHasSharedProtectedHeaders() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @param int $id |
||
| 506 | * |
||
| 507 | * @return string |
||
| 508 | */ |
||
| 509 | public function toFlattenedJSON(int $id): string |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @return string |
||
| 527 | */ |
||
| 528 | public function toJSON(): string |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @return array |
||
| 549 | */ |
||
| 550 | private function getJSONBase(): array |
||
| 573 | } |
||
| 574 |