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 |
||
| 19 | final class JWE implements JWEInterface |
||
| 20 | { |
||
| 21 | use JWT; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var \Jose\Object\RecipientInterface[] |
||
| 25 | */ |
||
| 26 | private $recipients = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string|null |
||
| 30 | */ |
||
| 31 | private $ciphertext = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string|null |
||
| 35 | */ |
||
| 36 | private $iv = null; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string|null |
||
| 40 | */ |
||
| 41 | private $aad = null; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string|null |
||
| 45 | */ |
||
| 46 | private $tag = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | private $shared_headers = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | private $shared_protected_headers = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string|null |
||
| 60 | */ |
||
| 61 | private $encoded_shared_protected_headers = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string|null |
||
| 65 | */ |
||
| 66 | private $content_encryption_key = null; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | public function countRecipients() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritdoc} |
||
| 78 | */ |
||
| 79 | public function addRecipient(RecipientInterface $recipient) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | public function getRecipients() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritdoc} |
||
| 97 | */ |
||
| 98 | public function getRecipient($id) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | public function getCiphertext() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * {@inheritdoc} |
||
| 117 | */ |
||
| 118 | public function withCiphertext($ciphertext) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritdoc} |
||
| 128 | */ |
||
| 129 | public function getAAD() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * {@inheritdoc} |
||
| 136 | */ |
||
| 137 | public function withAAD($aad) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritdoc} |
||
| 147 | */ |
||
| 148 | public function getIV() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * {@inheritdoc} |
||
| 155 | */ |
||
| 156 | public function withIV($iv) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | public function getTag() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | public function withTag($tag) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * {@inheritdoc} |
||
| 185 | */ |
||
| 186 | public function getEncodedSharedProtectedHeaders() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * {@inheritdoc} |
||
| 193 | */ |
||
| 194 | public function withEncodedSharedProtectedHeaders($encoded_shared_protected_headers) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritdoc} |
||
| 204 | */ |
||
| 205 | public function getSharedProtectedHeaders() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * {@inheritdoc} |
||
| 212 | */ |
||
| 213 | public function withSharedProtectedHeaders(array $shared_protected_headers) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * {@inheritdoc} |
||
| 223 | */ |
||
| 224 | public function withSharedProtectedHeader($key, $value) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritdoc} |
||
| 234 | */ |
||
| 235 | public function getSharedProtectedHeader($key) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * {@inheritdoc} |
||
| 245 | */ |
||
| 246 | public function hasSharedProtectedHeader($key) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * {@inheritdoc} |
||
| 253 | */ |
||
| 254 | public function withSharedHeaders(array $shared_headers) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritdoc} |
||
| 264 | */ |
||
| 265 | public function withSharedHeader($key, $value) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | */ |
||
| 276 | public function getSharedHeaders() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * {@inheritdoc} |
||
| 283 | */ |
||
| 284 | public function getSharedHeader($key) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * {@inheritdoc} |
||
| 294 | */ |
||
| 295 | public function hasSharedHeader($key) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * {@inheritdoc} |
||
| 302 | */ |
||
| 303 | public function toCompactJSON($id) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * |
||
| 323 | */ |
||
| 324 | private function checkHasNoAAD() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param int $id |
||
| 333 | */ |
||
| 334 | private function checkRecipientHasNoHeaders($id) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * |
||
| 343 | */ |
||
| 344 | private function checkHasSharedProtectedHeaders() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * {@inheritdoc} |
||
| 353 | */ |
||
| 354 | public function toFlattenedJSON($id) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * {@inheritdoc} |
||
| 372 | */ |
||
| 373 | public function toJSON() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @return array |
||
| 395 | */ |
||
| 396 | private function getJSONBase() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * {@inheritdoc} |
||
| 422 | */ |
||
| 423 | public function getContentEncryptionKey() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * {@inheritdoc} |
||
| 430 | */ |
||
| 431 | public function withContentEncryptionKey($content_encryption_key) |
||
| 438 | } |
||
| 439 |