Complex classes like JWS 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 JWS, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | final class JWS implements JWTInterface |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | private $isPayloadDetached = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string|null |
||
| 32 | */ |
||
| 33 | private $encodedPayload = null; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var Signature[] |
||
| 37 | */ |
||
| 38 | private $signatures = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var mixed|null |
||
| 42 | */ |
||
| 43 | private $payload = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * JWS constructor. |
||
| 47 | * |
||
| 48 | * @param mixed|null $payload |
||
| 49 | * @param mixed|null $encodedPayload |
||
| 50 | * @param bool $isPayloadDetached |
||
| 51 | */ |
||
| 52 | private function __construct($payload = null, $encodedPayload = null, bool $isPayloadDetached = false) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param mixed|null $payload |
||
| 61 | * @param bool $isPayloadDetached |
||
| 62 | * |
||
| 63 | * @return JWS |
||
| 64 | */ |
||
| 65 | public static function create($payload = null, bool $isPayloadDetached = false): JWS |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param mixed|null $encodedPayload |
||
| 72 | * @param bool $isPayloadDetached |
||
| 73 | * |
||
| 74 | * @return JWS |
||
| 75 | */ |
||
| 76 | public static function createFromEncodedPayload($encodedPayload, bool $isPayloadDetached = false): JWS |
||
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | public function getPayload() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param mixed $payload |
||
| 97 | * |
||
| 98 | * @return JWS |
||
| 99 | */ |
||
| 100 | public function withPayload($payload): JWS |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | */ |
||
| 111 | public function getClaim(string $key) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritdoc} |
||
| 121 | */ |
||
| 122 | public function getClaims(): array |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | public function hasClaim(string $key): bool |
||
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | public function hasClaims(): bool |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function isPayloadDetached(): bool |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return JWS |
||
| 157 | */ |
||
| 158 | public function withDetachedPayload(): JWS |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return JWS |
||
| 168 | */ |
||
| 169 | public function withAttachedPayload(): JWS |
||
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritdoc} |
||
| 179 | */ |
||
| 180 | public function withEncodedPayload(string $encoded_payload): JWS |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param Signature $signature |
||
| 190 | * |
||
| 191 | * @return string|null |
||
| 192 | */ |
||
| 193 | public function getEncodedPayload(Signature $signature): ?string |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Returns the signature associated with the JWS. |
||
| 212 | * |
||
| 213 | * @return Signature[] |
||
| 214 | */ |
||
| 215 | public function getSignatures(): array |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param int $id |
||
| 222 | * |
||
| 223 | * @return Signature |
||
| 224 | */ |
||
| 225 | public function &getSignature(int $id): Signature |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $signature |
||
| 235 | * @param string|null $encoded_protected_headers |
||
| 236 | * @param array $headers |
||
| 237 | * |
||
| 238 | * @return JWS |
||
| 239 | */ |
||
| 240 | public function addSignature(string $signature, ?string $encoded_protected_headers, array $headers = []): JWS |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Returns the number of signature associated with the JWS. |
||
| 250 | * |
||
| 251 | * |
||
| 252 | * @return int |
||
| 253 | */ |
||
| 254 | public function countSignatures(): int |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param int $id |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | public function toCompactJSON(int $id): string |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param int $id |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function toFlattenedJSON(int $id): string |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public function toJSON(): string |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param Signature $signature |
||
| 343 | * |
||
| 344 | * @return bool |
||
| 345 | */ |
||
| 346 | private function isPayloadEncoded(Signature $signature): bool |
||
| 350 | |||
| 351 | private function checkPayloadEncoding() |
||
| 363 | } |
||
| 364 |