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 |
||
| 20 | final class JWKSet implements \Countable, \IteratorAggregate, \JsonSerializable |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | private $keys = []; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * JWKSet constructor. |
||
| 29 | * |
||
| 30 | * @param JWK[] $keys |
||
| 31 | */ |
||
| 32 | private function __construct(array $keys) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param array $data |
||
| 39 | * |
||
| 40 | * @return JWKSet |
||
| 41 | */ |
||
| 42 | public static function createFromKeyData(array $data): self |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param JWK[] $keys |
||
| 64 | * |
||
| 65 | * @return JWKSet |
||
| 66 | */ |
||
| 67 | public static function createFromKeys(array $keys): self |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param string $json |
||
| 84 | * |
||
| 85 | * @return JWKSet |
||
| 86 | */ |
||
| 87 | public static function createFromJson(string $json): self |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Returns all keys in the key set. |
||
| 99 | * |
||
| 100 | * @return JWK[] An array of keys stored in the key set |
||
| 101 | */ |
||
| 102 | public function all(): array |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Add key in the key set. |
||
| 109 | * |
||
| 110 | * @param JWK $jwk A key to store in the key set |
||
| 111 | * |
||
| 112 | * @return JWKSet |
||
| 113 | */ |
||
| 114 | public function with(JWK $jwk): self |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Remove key from the key set. |
||
| 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 | * @param int|string $index |
||
| 148 | * |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | public function has($index): bool |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param int|string $index |
||
| 158 | * |
||
| 159 | * @return JWK |
||
| 160 | */ |
||
| 161 | public function get($index): JWK |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | public function jsonSerialize(): array |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param int $mode |
||
| 180 | * |
||
| 181 | * @return int |
||
| 182 | */ |
||
| 183 | public function count($mode = COUNT_NORMAL): int |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param string $type Must be 'sig' (signature) or 'enc' (encryption) |
||
| 190 | * @param Algorithm|null $algorithm Specifies the algorithm to be used |
||
| 191 | * @param array $restrictions More restrictions such as 'kid' or 'kty' |
||
| 192 | * |
||
| 193 | * @return JWK|null |
||
| 194 | */ |
||
| 195 | public function selectKey(string $type, ?Algorithm $algorithm = null, array $restrictions = []): ?JWK |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $type |
||
| 235 | * @param JWK $key |
||
| 236 | * |
||
| 237 | * @return bool|int |
||
| 238 | */ |
||
| 239 | private function canKeyBeUsedFor(string $type, JWK $key) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param null|Algorithm $algorithm |
||
| 253 | * @param JWK $key |
||
| 254 | * |
||
| 255 | * @return bool|int |
||
| 256 | */ |
||
| 257 | private function canKeyBeUsedWithAlgorithm(?Algorithm $algorithm, JWK $key) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param array $restrictions |
||
| 274 | * @param JWK $key |
||
| 275 | * |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | private function doesKeySatisfyRestrictions(array $restrictions, JWK $key): bool |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param string $key_ops |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | private static function convertKeyOpsToKeyUse(string $key_ops): string |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param array $a |
||
| 312 | * @param array $b |
||
| 313 | * |
||
| 314 | * @return int |
||
| 315 | */ |
||
| 316 | public static function sortKeys(array $a, array $b): int |
||
| 324 | |||
| 325 | /** |
||
| 326 | * {@inheritdoc} |
||
| 327 | */ |
||
| 328 | public function getIterator() |
||
| 332 | } |
||
| 333 |