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 |
||
| 23 | final class JWE implements JWTInterface |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var Recipient[] |
||
| 27 | */ |
||
| 28 | private $recipients = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string|null |
||
| 32 | */ |
||
| 33 | private $ciphertext = null; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string|null |
||
| 37 | */ |
||
| 38 | private $iv = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string|null |
||
| 42 | */ |
||
| 43 | private $aad = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string|null |
||
| 47 | */ |
||
| 48 | private $tag = null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private $sharedHeaders = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | private $sharedProtectedHeaders = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string|null |
||
| 62 | */ |
||
| 63 | private $encodedSharedProtectedHeaders = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var mixed|null |
||
| 67 | */ |
||
| 68 | private $payload = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * JWE constructor. |
||
| 72 | * |
||
| 73 | * @param string $ciphertext |
||
| 74 | * @param null|string $iv |
||
| 75 | * @param null|string $aad |
||
| 76 | * @param null|string $tag |
||
| 77 | * @param array $sharedHeaders |
||
| 78 | * @param array $sharedProtectedHeaders |
||
| 79 | * @param null|string $encodedSharedProtectedHeaders |
||
| 80 | * @param array $recipients |
||
| 81 | */ |
||
| 82 | private function __construct(string $ciphertext, ?string $iv = null, ?string $aad = null, ?string $tag = null, array $sharedHeaders = [], array $sharedProtectedHeaders = [], ?string $encodedSharedProtectedHeaders = null, array $recipients = []) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $ciphertext |
||
| 96 | * @param null|string $iv |
||
| 97 | * @param null|string $aad |
||
| 98 | * @param null|string $tag |
||
| 99 | * @param array $sharedHeaders |
||
| 100 | * @param array $sharedProtectedHeaders |
||
| 101 | * @param null|string $encodedSharedProtectedHeaders |
||
| 102 | * @param array $recipients |
||
| 103 | * |
||
| 104 | * @return JWE |
||
| 105 | */ |
||
| 106 | public static function create(string $ciphertext, ?string $iv = null, ?string $aad = null, ?string $tag = null, array $sharedHeaders = [], array $sharedProtectedHeaders = [], ?string $encodedSharedProtectedHeaders = null, array $recipients = []): JWE |
||
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritdoc} |
||
| 113 | */ |
||
| 114 | public function getPayload() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param $payload |
||
| 121 | * |
||
| 122 | * @return JWE |
||
| 123 | */ |
||
| 124 | public function withPayload($payload): JWE |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | public function getClaim(string $key) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritdoc} |
||
| 145 | */ |
||
| 146 | public function getClaims(): array |
||
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritdoc} |
||
| 157 | */ |
||
| 158 | public function hasClaim(string $key): bool |
||
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritdoc} |
||
| 165 | */ |
||
| 166 | public function hasClaims(): bool |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Returns the number of recipients associated with the JWS. |
||
| 173 | * |
||
| 174 | * @return int |
||
| 175 | */ |
||
| 176 | public function countRecipients(): int |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @return bool |
||
| 183 | */ |
||
| 184 | public function isEncrypted(): bool |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Returns the recipients associated with the JWS. |
||
| 191 | * |
||
| 192 | * @return Recipient[] |
||
| 193 | */ |
||
| 194 | public function getRecipients(): array |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param int $id |
||
| 201 | * |
||
| 202 | * @return Recipient |
||
| 203 | */ |
||
| 204 | public function getRecipient(int $id): Recipient |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return string|null The cyphertext |
||
| 213 | */ |
||
| 214 | public function getCiphertext(): ?string |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @return string|null |
||
| 221 | */ |
||
| 222 | public function getAAD(): ?string |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return string|null |
||
| 229 | */ |
||
| 230 | public function getIV(): ?string |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return string|null |
||
| 237 | */ |
||
| 238 | public function getTag(): ?string |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public function getEncodedSharedProtectedHeaders(): string |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | public function getSharedProtectedHeaders(): array |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param string $key The key |
||
| 261 | * |
||
| 262 | * @return mixed|null Header value |
||
| 263 | */ |
||
| 264 | public function getSharedProtectedHeader(string $key) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param string $key The key |
||
| 274 | * |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | public function hasSharedProtectedHeader(string $key): bool |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | public function getSharedHeaders(): array |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $key The key |
||
| 292 | * |
||
| 293 | * @return mixed|null Header value |
||
| 294 | */ |
||
| 295 | public function getSharedHeader(string $key) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param string $key The key |
||
| 305 | * |
||
| 306 | * @return bool |
||
| 307 | */ |
||
| 308 | public function hasSharedHeader(string $key): bool |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param int $id |
||
| 315 | * |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | public function toCompactJSON(int $id): string |
||
| 335 | |||
| 336 | private function checkHasNoAAD() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param int $id |
||
| 343 | */ |
||
| 344 | private function checkRecipientHasNoHeaders(int $id) |
||
| 351 | |||
| 352 | private function checkHasSharedProtectedHeaders() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param int $id |
||
| 362 | * |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | public function toFlattenedJSON(int $id): string |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | public function toJSON(): string |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | private function getJSONBase(): array |
||
| 429 | } |
||
| 430 |