Complex classes like JWKSet 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 JWKSet, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | final class JWKSet implements \Countable, \Iterator, \JsonSerializable |
||
20 | { |
||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | private $keys = []; |
||
25 | |||
26 | /** |
||
27 | * JWKSet constructor. |
||
28 | * |
||
29 | * @param JWK[] $keys |
||
30 | */ |
||
31 | private function __construct(array $keys) |
||
35 | |||
36 | /** |
||
37 | * @param array $data |
||
38 | * |
||
39 | * @return JWKSet |
||
40 | */ |
||
41 | public static function createFromKeyData(array $data): JWKSet |
||
60 | |||
61 | /** |
||
62 | * @param JWK[] $keys |
||
63 | * |
||
64 | * @return JWKSet |
||
65 | */ |
||
66 | public static function createFromKeys(array $keys): JWKSet |
||
80 | |||
81 | /** |
||
82 | * @param string $json |
||
83 | * |
||
84 | * @return JWKSet |
||
85 | */ |
||
86 | public static function createFromJson(string $json): JWKSet |
||
95 | |||
96 | /** |
||
97 | * Returns all keys in the key set. |
||
98 | * |
||
99 | * @return JWK[] An array of keys stored in the key set |
||
100 | */ |
||
101 | public function all(): array |
||
105 | |||
106 | /** |
||
107 | * Add key in the key set. |
||
108 | * |
||
109 | * @param JWK $jwk A key to store in the key set |
||
110 | * |
||
111 | * @return JWKSet |
||
112 | */ |
||
113 | public function with(JWK $jwk): self |
||
125 | |||
126 | /** |
||
127 | * Remove key from the key set. |
||
128 | * |
||
129 | * @param int|string $key Key to remove from the key set |
||
130 | * |
||
131 | * @return JWKSet |
||
132 | */ |
||
133 | public function without($key): self |
||
144 | |||
145 | /** |
||
146 | * @param int|string $index |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function has($index): bool |
||
154 | |||
155 | /** |
||
156 | * @param int|string $index |
||
157 | * |
||
158 | * @return JWK |
||
159 | */ |
||
160 | public function get($index): JWK |
||
168 | |||
169 | /** |
||
170 | * @return array |
||
171 | */ |
||
172 | public function jsonSerialize(): array |
||
176 | |||
177 | /** |
||
178 | * @param int $mode |
||
179 | * |
||
180 | * @return int |
||
181 | */ |
||
182 | public function count($mode = COUNT_NORMAL): int |
||
186 | |||
187 | /** |
||
188 | * @return JWK|null |
||
189 | */ |
||
190 | public function current(): ?JWK |
||
199 | |||
200 | /** |
||
201 | * @return int|string|null |
||
202 | */ |
||
203 | public function key() |
||
207 | |||
208 | public function next() |
||
212 | |||
213 | public function rewind() |
||
217 | |||
218 | /** |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function valid(): bool |
||
225 | |||
226 | /** |
||
227 | * @param string $type Must be 'sig' (signature) or 'enc' (encryption) |
||
228 | * @param Algorithm|null $algorithm Specifies the algorithm to be used |
||
229 | * @param array $restrictions More restrictions such as 'kid' or 'kty' |
||
230 | * |
||
231 | * @return JWK|null |
||
232 | */ |
||
233 | public function selectKey(string $type, ?Algorithm $algorithm = null, array $restrictions = []): ?JWK |
||
270 | |||
271 | /** |
||
272 | * @param string $type |
||
273 | * @param JWK $key |
||
274 | * |
||
275 | * @return bool|int |
||
276 | */ |
||
277 | private function canKeyBeUsedFor(string $type, JWK $key) |
||
288 | |||
289 | /** |
||
290 | * @param null|Algorithm $algorithm |
||
291 | * @param JWK $key |
||
292 | * |
||
293 | * @return bool|int |
||
294 | */ |
||
295 | private function canKeyBeUsedWithAlgorithm(?Algorithm $algorithm, JWK $key) |
||
309 | |||
310 | /** |
||
311 | * @param array $restrictions |
||
312 | * @param JWK $key |
||
313 | * |
||
314 | * @return bool |
||
315 | */ |
||
316 | private function doesKeySatisfyRestrictions(array $restrictions, JWK $key): bool |
||
326 | |||
327 | /** |
||
328 | * @param string $key_ops |
||
329 | * |
||
330 | * @return string |
||
331 | */ |
||
332 | private static function convertKeyOpsToKeyUse(string $key_ops): string |
||
347 | |||
348 | /** |
||
349 | * @param array $a |
||
350 | * @param array $b |
||
351 | * |
||
352 | * @return int |
||
353 | */ |
||
354 | public static function sortKeys(array $a, array $b): int |
||
362 | } |
||
363 |