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 |
||
22 | class Jwt |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * Extra leeway time when checking nbf, iat or expiration times |
||
27 | */ |
||
28 | protected $leeway = 0; |
||
29 | |||
30 | /** |
||
31 | * The current time |
||
32 | */ |
||
33 | protected $timestamp; |
||
34 | |||
35 | /** |
||
36 | * An array of supported algorithms |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $algs; |
||
40 | |||
41 | /** |
||
42 | * Jwt constructor |
||
43 | */ |
||
44 | public function __construct() |
||
51 | |||
52 | /** |
||
53 | * Sets leeway time |
||
54 | * @param int $time |
||
55 | * @return $this |
||
56 | */ |
||
57 | public function setLeeway($time) |
||
62 | |||
63 | /** |
||
64 | * Sets the current timestamp |
||
65 | * @param $timestamp |
||
66 | * @return $this |
||
67 | */ |
||
68 | public function setTimestamp($timestamp) |
||
73 | |||
74 | /** |
||
75 | * Sets an algorithm |
||
76 | * @param string $name |
||
77 | * @param string $hash_method |
||
78 | * @param callable $signer |
||
79 | * @param callable $verifier |
||
80 | * @return $this |
||
81 | */ |
||
82 | public function setAlg($name, $hash_method, callable $signer, callable $verifier) |
||
87 | |||
88 | /** |
||
89 | * Returns an array of supported algorithms |
||
90 | * @return array |
||
91 | */ |
||
92 | public function getAlgs() |
||
96 | |||
97 | /** |
||
98 | * Decodes a JWT string into a PHP object |
||
99 | * @param string $jwt |
||
100 | * @param string $key |
||
101 | * @param array $allowed_algs |
||
102 | * @return object |
||
103 | * @throws RuntimeException |
||
104 | * @throws InvalidArgumentException |
||
105 | * @throws UnexpectedValueException |
||
106 | */ |
||
107 | public function decode($jwt, $key, array $allowed_algs = array()) |
||
173 | |||
174 | /** |
||
175 | * Converts and signs a PHP object or array into a JWT string |
||
176 | * @param object|array $payload |
||
177 | * @param string $key |
||
178 | * @param string $alg |
||
179 | * @param null|string $key_id |
||
180 | * @param array $head |
||
181 | * @return string |
||
182 | */ |
||
183 | public function encode($payload, $key, $alg = 'HS256', $key_id = null, array $head = array()) |
||
206 | |||
207 | /** |
||
208 | * Sign a string with a given key and algorithm |
||
209 | * @param string $data |
||
210 | * @param string|resource $key |
||
211 | * @param string $alg |
||
212 | * @return string |
||
213 | * @throws OutOfRangeException |
||
214 | * @throws RuntimeException |
||
215 | * @throws LogicException |
||
216 | */ |
||
217 | public function sign($data, $key, $alg = 'HS256') |
||
231 | |||
232 | /** |
||
233 | * Generate signature using HMAC method |
||
234 | * @param string $data |
||
235 | * @param string $key |
||
236 | * @param string $alg |
||
237 | * @return string |
||
238 | * @throws RuntimeException |
||
239 | */ |
||
240 | protected function signHmac($data, $key, $alg) |
||
250 | |||
251 | /** |
||
252 | * Verify a signature with the message, key and method |
||
253 | * @param string $data |
||
254 | * @param string $hash |
||
255 | * @param string|resource $key |
||
256 | * @param string $alg |
||
257 | * @return bool |
||
258 | * @throws OutOfRangeException |
||
259 | * @throws LogicException |
||
260 | */ |
||
261 | public function verify($data, $hash, $key, $alg) |
||
276 | |||
277 | /** |
||
278 | * Verify signature using HMAC method |
||
279 | * @param string $data |
||
280 | * @param string $hash |
||
281 | * @param string $key |
||
282 | * @param string $alg |
||
283 | * @return bool |
||
284 | * @throws RuntimeException |
||
285 | */ |
||
286 | protected function verifyHmac($data, $hash, $key, $alg) |
||
296 | |||
297 | /** |
||
298 | * Compares two hashed strings |
||
299 | * @param string $str1 |
||
300 | * @param string $str2 |
||
301 | * @return boolean |
||
302 | */ |
||
303 | protected function hashEquals($str1, $str2) |
||
322 | |||
323 | /** |
||
324 | * Decode a JSON string into a PHP object |
||
325 | * @param string $input |
||
326 | * @return object |
||
327 | * @throws RuntimeException |
||
328 | */ |
||
329 | protected function jsonDecode($input) |
||
339 | |||
340 | /** |
||
341 | * Encode a PHP object into a JSON string |
||
342 | * @param object|array $input |
||
343 | * @return string |
||
344 | * @throws RuntimeException |
||
345 | */ |
||
346 | protected function jsonEncode($input) |
||
356 | |||
357 | /** |
||
358 | * Decodes data encoded with MIME base64 |
||
359 | * @param string $string |
||
360 | * @return string |
||
361 | */ |
||
362 | protected function decodeBase64($string) |
||
373 | |||
374 | /** |
||
375 | * Safe URL base64 encoding |
||
376 | * @param string $string |
||
377 | * @return string |
||
378 | */ |
||
379 | protected function encodeBase64($string) |
||
383 | } |