Complex classes like JWT 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 JWT, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class JWT |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Type identifier for signed JWT. |
||
| 31 | * |
||
| 32 | * @internal |
||
| 33 | * |
||
| 34 | * @var int |
||
| 35 | */ |
||
| 36 | const TYPE_JWS = 0; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Type identifier for encrypted JWT. |
||
| 40 | * |
||
| 41 | * @internal |
||
| 42 | * |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | const TYPE_JWE = 1; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * JWT parts. |
||
| 49 | * |
||
| 50 | * @var string[] $_parts |
||
| 51 | */ |
||
| 52 | protected $_parts; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * JWT type. |
||
| 56 | * |
||
| 57 | * @var int $_type |
||
| 58 | */ |
||
| 59 | protected $_type; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Constructor |
||
| 63 | * |
||
| 64 | * @param string $token JWT string |
||
| 65 | * @throws \UnexpectedValueException |
||
| 66 | */ |
||
| 67 | 14 | public function __construct($token) { |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Convert claims set to an unsecured JWT. |
||
| 83 | * |
||
| 84 | * @param Claims $claims Claims set |
||
| 85 | * @param Header|null $header Optional header |
||
| 86 | * @throws \RuntimeException For generic errors |
||
| 87 | * @return self |
||
| 88 | */ |
||
| 89 | 3 | public static function unsecuredFromClaims(Claims $claims, |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Convert claims set to a signed JWS token. |
||
| 96 | * |
||
| 97 | * @param Claims $claims Claims set |
||
| 98 | * @param SignatureAlgorithm $algo Signature algorithm |
||
| 99 | * @param Header|null $header Optional header |
||
| 100 | * @throws \RuntimeException For generic errors |
||
| 101 | * @return self |
||
| 102 | */ |
||
| 103 | 5 | public static function signedFromClaims(Claims $claims, |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Convert claims set to an encrypted JWE token. |
||
| 112 | * |
||
| 113 | * @param Claims $claims Claims set |
||
| 114 | * @param KeyManagementAlgorithm $key_algo Key management algorithm |
||
| 115 | * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
||
| 116 | * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
||
| 117 | * @param Header|null $header Optional header |
||
| 118 | * @throws \RuntimeException For generic errors |
||
| 119 | * @return self |
||
| 120 | */ |
||
| 121 | 2 | public static function encryptedFromClaims(Claims $claims, |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Sign self producing a nested JWT. |
||
| 132 | * |
||
| 133 | * Note that if JWT is to be signed and encrypted, it should be done in |
||
| 134 | * sign-then-encrypt order. Please refer to links for security information. |
||
| 135 | * |
||
| 136 | * @link https://tools.ietf.org/html/rfc7519#section-11.2 |
||
| 137 | * @param SignatureAlgorithm $algo Signature algorithm |
||
| 138 | * @param Header|null $header Optional header |
||
| 139 | * @throws \RuntimeException For generic errors |
||
| 140 | * @return self |
||
| 141 | */ |
||
| 142 | 1 | public function signNested(SignatureAlgorithm $algo, Header $header = null) { |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Encrypt self producing a nested JWT. |
||
| 155 | * |
||
| 156 | * This JWT should be a JWS, that is, the order of nesting should be |
||
| 157 | * sign-then-encrypt. |
||
| 158 | * |
||
| 159 | * @link https://tools.ietf.org/html/rfc7519#section-11.2 |
||
| 160 | * @param KeyManagementAlgorithm $key_algo Key management algorithm |
||
| 161 | * @param ContentEncryptionAlgorithm $enc_algo Content encryption algorithm |
||
| 162 | * @param CompressionAlgorithm|null $zip_algo Optional compression algorithm |
||
| 163 | * @param Header|null $header Optional header |
||
| 164 | * @throws \RuntimeException For generic errors |
||
| 165 | * @return self |
||
| 166 | */ |
||
| 167 | 1 | public function encryptNested(KeyManagementAlgorithm $key_algo, |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Whether JWT is a JWS. |
||
| 183 | * |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | 16 | public function isJWS() { |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Get JWT as a JWS. |
||
| 192 | * |
||
| 193 | * @throws \LogicException |
||
| 194 | * @return JWS |
||
| 195 | */ |
||
| 196 | 12 | public function JWS() { |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Whether JWT is a JWE. |
||
| 205 | * |
||
| 206 | * @return bool |
||
| 207 | */ |
||
| 208 | 11 | public function isJWE() { |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Get JWT as a JWE. |
||
| 214 | * |
||
| 215 | * @throws \LogicException |
||
| 216 | * @return JWE |
||
| 217 | */ |
||
| 218 | 7 | public function JWE() { |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Check whether JWT contains another nested JWT. |
||
| 227 | * |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | 9 | public function isNested() { |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Check whether JWT is unsecured, that is, it's neither integrity protected |
||
| 244 | * nor encrypted. |
||
| 245 | * |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | 4 | public function isUnsecured() { |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Get JWT header. |
||
| 259 | * |
||
| 260 | * @return JOSE |
||
| 261 | */ |
||
| 262 | 11 | public function header() { |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Get JWT as a string. |
||
| 269 | * |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | 6 | public function token() { |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Get claims from the JWT. |
||
| 278 | * |
||
| 279 | * Claims shall be validated according to given validation context. |
||
| 280 | * Validation context must contain all the necessary keys for the signature |
||
| 281 | * validation and/or content decryption. |
||
| 282 | * |
||
| 283 | * @param ValidationContext $ctx |
||
| 284 | * @throws ValidationException If signature is invalid, or decryption fails, |
||
| 285 | * or claims validation fails. |
||
| 286 | * @throws \RuntimeException For generic errors |
||
| 287 | * @return Claims |
||
| 288 | */ |
||
| 289 | 11 | public function claims(ValidationContext $ctx) { |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Get claims from a nested payload. |
||
| 308 | * |
||
| 309 | * @param string $payload JWT payload |
||
| 310 | * @param ValidationContext $ctx Validation context |
||
| 311 | * @return Claims |
||
| 312 | */ |
||
| 313 | 1 | private function _claimsFromNestedPayload($payload, ValidationContext $ctx) { |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Get payload from JWS. |
||
| 324 | * |
||
| 325 | * @param JWS $jws JWS |
||
| 326 | * @param ValidationContext $ctx Validation context |
||
| 327 | * @throws ValidationException If signature validation fails |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | 8 | private static function _validatedPayloadFromJWS(JWS $jws, |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Get validated payload from an unsecured JWS. |
||
| 341 | * |
||
| 342 | * @param JWS $jws |
||
| 343 | * @param ValidationContext $ctx |
||
| 344 | * @throws ValidationException |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | 3 | private static function _validatedPayloadFromUnsecuredJWS(JWS $jws, |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Get validated payload from a signed JWS. |
||
| 360 | * |
||
| 361 | * @param JWS $jws |
||
| 362 | * @param JWKSet $keys |
||
| 363 | * @throws ValidationException |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | 5 | private static function _validatedPayloadFromSignedJWS(JWS $jws, |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Get validated payload from an encrypted JWE. |
||
| 385 | * |
||
| 386 | * @param JWE $jwe JWE |
||
| 387 | * @param ValidationContext $ctx Validation context |
||
| 388 | * @throws ValidationException If decryption fails |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | 4 | private static function _validatedPayloadFromJWE(JWE $jwe, |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Convert JWT to string. |
||
| 406 | * |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | 1 | public function __toString() { |
|
| 412 | } |
||
| 413 |