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 string|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(): ?string |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $payload |
||
| 121 | * |
||
| 122 | * @return JWE |
||
| 123 | */ |
||
| 124 | public function withPayload(string $payload): JWE |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Returns the number of recipients associated with the JWS. |
||
| 134 | * |
||
| 135 | * @return int |
||
| 136 | */ |
||
| 137 | public function countRecipients(): int |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | public function isEncrypted(): bool |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Returns the recipients associated with the JWS. |
||
| 152 | * |
||
| 153 | * @return Recipient[] |
||
| 154 | */ |
||
| 155 | public function getRecipients(): array |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param int $id |
||
| 162 | * |
||
| 163 | * @return Recipient |
||
| 164 | */ |
||
| 165 | public function getRecipient(int $id): Recipient |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return string|null The cyphertext |
||
| 174 | */ |
||
| 175 | public function getCiphertext(): ?string |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @return string|null |
||
| 182 | */ |
||
| 183 | public function getAAD(): ?string |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return string|null |
||
| 190 | */ |
||
| 191 | public function getIV(): ?string |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return string|null |
||
| 198 | */ |
||
| 199 | public function getTag(): ?string |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function getEncodedSharedProtectedHeaders(): string |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | public function getSharedProtectedHeaders(): array |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $key The key |
||
| 222 | * |
||
| 223 | * @return mixed|null Header value |
||
| 224 | */ |
||
| 225 | public function getSharedProtectedHeader(string $key) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $key The key |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | public function hasSharedProtectedHeader(string $key): bool |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | public function getSharedHeaders(): array |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param string $key The key |
||
| 253 | * |
||
| 254 | * @return mixed|null Header value |
||
| 255 | */ |
||
| 256 | public function getSharedHeader(string $key) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $key The key |
||
| 266 | * |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | public function hasSharedHeader(string $key): bool |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param int $id |
||
| 276 | * |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | public function toCompactJSON(int $id): string |
||
| 296 | |||
| 297 | private function checkHasNoAAD() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param int $id |
||
| 304 | */ |
||
| 305 | private function checkRecipientHasNoHeaders(int $id) |
||
| 312 | |||
| 313 | private function checkHasSharedProtectedHeaders() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param int $id |
||
| 323 | * |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | public function toFlattenedJSON(int $id): string |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | public function toJSON(): string |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @return array |
||
| 366 | */ |
||
| 367 | private function getJSONBase(): array |
||
| 390 | } |
||
| 391 |