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 |
||
23 | class JWKSet implements Countable, IteratorAggregate, JsonSerializable |
||
24 | { |
||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | private $keys = []; |
||
29 | |||
30 | /** |
||
31 | * @param JWK[] $keys |
||
32 | * |
||
33 | * @throws InvalidArgumentException if the list is invalid |
||
34 | */ |
||
35 | public function __construct(array $keys) |
||
50 | |||
51 | /** |
||
52 | * Creates a JWKSet object using the given values. |
||
53 | * |
||
54 | * @throws InvalidArgumentException if the keyset is not valid |
||
55 | * |
||
56 | * @return JWKSet |
||
57 | */ |
||
58 | public static function createFromKeyData(array $data): self |
||
79 | |||
80 | /** |
||
81 | * Creates a JWKSet object using the given Json string. |
||
82 | * |
||
83 | * @throws InvalidArgumentException if the data is not valid |
||
84 | * |
||
85 | * @return JWKSet |
||
86 | */ |
||
87 | public static function createFromJson(string $json): self |
||
96 | |||
97 | /** |
||
98 | * Returns an array of keys stored in the key set. |
||
99 | * |
||
100 | * @return JWK[] |
||
101 | */ |
||
102 | public function all(): array |
||
106 | |||
107 | /** |
||
108 | * Add key to store in the key set. |
||
109 | * This method is immutable and will return a new object. |
||
110 | * |
||
111 | * @return JWKSet |
||
112 | */ |
||
113 | public function with(JWK $jwk): self |
||
125 | |||
126 | /** |
||
127 | * Remove key from the key set. |
||
128 | * This method is immutable and will return a new object. |
||
129 | * |
||
130 | * @param int|string $key Key to remove from the key set |
||
131 | * |
||
132 | * @return JWKSet |
||
133 | */ |
||
134 | public function without($key): self |
||
145 | |||
146 | /** |
||
147 | * Returns true if the key set contains a key with the given index. |
||
148 | * |
||
149 | * @param int|string $index |
||
150 | */ |
||
151 | public function has($index): bool |
||
155 | |||
156 | /** |
||
157 | * Returns the key with the given index. Throws an exception if the index is not present in the key store. |
||
158 | * |
||
159 | * @param int|string $index |
||
160 | * |
||
161 | * @throws InvalidArgumentException if the index is not defined |
||
162 | */ |
||
163 | public function get($index): JWK |
||
171 | |||
172 | /** |
||
173 | * Returns the values to be serialized. |
||
174 | */ |
||
175 | public function jsonSerialize(): array |
||
179 | |||
180 | /** |
||
181 | * Returns the number of keys in the key set. |
||
182 | * |
||
183 | * @param int $mode |
||
184 | */ |
||
185 | public function count($mode = COUNT_NORMAL): int |
||
189 | |||
190 | /** |
||
191 | * Try to find a key that fits on the selected requirements. |
||
192 | * Returns null if not found. |
||
193 | * |
||
194 | * @param string $type Must be 'sig' (signature) or 'enc' (encryption) |
||
195 | * @param null|Algorithm $algorithm Specifies the algorithm to be used |
||
196 | * @param array $restrictions More restrictions such as 'kid' or 'kty' |
||
197 | * |
||
198 | * @throws InvalidArgumentException if the key type is not valid (must be "sig" or "enc") |
||
199 | */ |
||
200 | public function selectKey(string $type, ?Algorithm $algorithm = null, array $restrictions = []): ?JWK |
||
237 | |||
238 | /** |
||
239 | * Internal method only. Should not be used. |
||
240 | * |
||
241 | * @internal |
||
242 | * @internal |
||
243 | */ |
||
244 | public static function sortKeys(array $a, array $b): int |
||
252 | |||
253 | /** |
||
254 | * Internal method only. Should not be used. |
||
255 | * |
||
256 | * @internal |
||
257 | */ |
||
258 | public function getIterator(): Traversable |
||
262 | |||
263 | /** |
||
264 | * @throws InvalidArgumentException if the key does not fulfill with the "key_ops" constraint |
||
265 | * |
||
266 | * @return bool|int |
||
267 | */ |
||
268 | private function canKeyBeUsedFor(string $type, JWK $key) |
||
284 | |||
285 | /** |
||
286 | * @return bool|int |
||
287 | */ |
||
288 | private function canKeyBeUsedWithAlgorithm(?Algorithm $algorithm, JWK $key) |
||
302 | |||
303 | private function doesKeySatisfyRestrictions(array $restrictions, JWK $key): bool |
||
313 | |||
314 | /** |
||
315 | * @throws InvalidArgumentException if the key operation is not supported |
||
316 | */ |
||
317 | private static function convertKeyOpsToKeyUse(array $key_ops): string |
||
334 | } |
||
335 |