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 |
||
| 20 | final class JWE implements JWEInterface |
||
| 21 | { |
||
| 22 | use JWT; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var \Jose\Object\RecipientInterface[] |
||
| 26 | */ |
||
| 27 | private $recipients = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string|null |
||
| 31 | */ |
||
| 32 | private $ciphertext = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string|null |
||
| 36 | */ |
||
| 37 | private $iv = null; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string|null |
||
| 41 | */ |
||
| 42 | private $aad = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string|null |
||
| 46 | */ |
||
| 47 | private $tag = null; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private $shared_headers = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | private $shared_protected_headers = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string|null |
||
| 61 | */ |
||
| 62 | private $encoded_shared_protected_headers = null; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string|null |
||
| 66 | */ |
||
| 67 | private $content_encryption_key = null; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | public function countRecipients() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * {@inheritdoc} |
||
| 79 | */ |
||
| 80 | public function addRecipient(JWKInterface $recipient_key, $recipient_headers = []) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritdoc} |
||
| 93 | */ |
||
| 94 | public function addRecipientWithEncryptedKey($encrypted_key = null, $recipient_headers = []) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritdoc} |
||
| 107 | */ |
||
| 108 | public function getRecipients() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | */ |
||
| 116 | public function &getRecipient($id) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | public function getCiphertext() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | public function withCiphertext($ciphertext) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * {@inheritdoc} |
||
| 144 | */ |
||
| 145 | public function getAAD() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * {@inheritdoc} |
||
| 152 | */ |
||
| 153 | public function withAAD($aad) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritdoc} |
||
| 163 | */ |
||
| 164 | public function getIV() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritdoc} |
||
| 171 | */ |
||
| 172 | public function withIV($iv) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * {@inheritdoc} |
||
| 182 | */ |
||
| 183 | public function getTag() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | public function withTag($tag) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * {@inheritdoc} |
||
| 201 | */ |
||
| 202 | public function getEncodedSharedProtectedHeaders() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * {@inheritdoc} |
||
| 209 | */ |
||
| 210 | public function withEncodedSharedProtectedHeaders($encoded_shared_protected_headers) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * {@inheritdoc} |
||
| 220 | */ |
||
| 221 | public function getSharedProtectedHeaders() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * {@inheritdoc} |
||
| 228 | */ |
||
| 229 | public function withSharedProtectedHeaders(array $shared_protected_headers) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * {@inheritdoc} |
||
| 239 | */ |
||
| 240 | public function withSharedProtectedHeader($key, $value) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * {@inheritdoc} |
||
| 250 | */ |
||
| 251 | public function getSharedProtectedHeader($key) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * {@inheritdoc} |
||
| 261 | */ |
||
| 262 | public function hasSharedProtectedHeader($key) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * {@inheritdoc} |
||
| 269 | */ |
||
| 270 | public function withSharedHeaders(array $shared_headers) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * {@inheritdoc} |
||
| 280 | */ |
||
| 281 | public function withSharedHeader($key, $value) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * {@inheritdoc} |
||
| 291 | */ |
||
| 292 | public function getSharedHeaders() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * {@inheritdoc} |
||
| 299 | */ |
||
| 300 | public function getSharedHeader($key) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * {@inheritdoc} |
||
| 310 | */ |
||
| 311 | public function hasSharedHeader($key) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * {@inheritdoc} |
||
| 318 | */ |
||
| 319 | public function toCompactJSON($id) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * |
||
| 339 | */ |
||
| 340 | private function checkHasNoAAD() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param int $id |
||
| 347 | */ |
||
| 348 | private function checkRecipientHasNoHeaders($id) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * |
||
| 358 | */ |
||
| 359 | private function checkHasSharedProtectedHeaders() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * {@inheritdoc} |
||
| 369 | */ |
||
| 370 | public function toFlattenedJSON($id) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * {@inheritdoc} |
||
| 388 | */ |
||
| 389 | public function toJSON() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @return array |
||
| 410 | */ |
||
| 411 | private function getJSONBase() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * {@inheritdoc} |
||
| 437 | */ |
||
| 438 | public function getContentEncryptionKey() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * {@inheritdoc} |
||
| 445 | */ |
||
| 446 | public function withContentEncryptionKey($content_encryption_key) |
||
| 453 | } |
||
| 454 |