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) |
||
| 32 | { |
||
| 33 | $this->keys = $keys; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param array $data |
||
| 38 | * |
||
| 39 | * @return JWKSet |
||
| 40 | */ |
||
| 41 | public static function createFromKeyData(array $data): JWKSet |
||
| 42 | { |
||
| 43 | if (!array_key_exists('keys', $data) || !is_array($data['keys'])) { |
||
| 44 | throw new \InvalidArgumentException('Invalid data.'); |
||
| 45 | } |
||
| 46 | |||
| 47 | $keys = []; |
||
| 48 | foreach ($data['keys'] as $key) { |
||
| 49 | $jwk = JWK::create($key); |
||
| 50 | if ($jwk->has('kid')) { |
||
| 51 | $keys[$jwk->get('kid')] = $jwk; |
||
| 52 | |||
| 53 | continue; |
||
| 54 | } |
||
| 55 | $keys[] = $jwk; |
||
| 56 | } |
||
| 57 | |||
| 58 | return new self($keys); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param JWK[] $keys |
||
| 63 | * |
||
| 64 | * @return JWKSet |
||
| 65 | */ |
||
| 66 | public static function createFromKeys(array $keys): JWKSet |
||
| 67 | { |
||
| 68 | $keys = array_filter($keys, function () { |
||
| 69 | return true; |
||
| 70 | }); |
||
| 71 | foreach ($keys as $k => $v) { |
||
| 72 | if ($v->has('kid')) { |
||
| 73 | unset($keys[$k]); |
||
| 74 | $keys[$v->get('kid')] = $v; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | return new self($keys); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Returns all keys in the key set. |
||
| 83 | * |
||
| 84 | * @return JWK[] An array of keys stored in the key set |
||
| 85 | */ |
||
| 86 | public function all(): array |
||
| 87 | { |
||
| 88 | return $this->keys; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Add key in the key set. |
||
| 93 | * |
||
| 94 | * @param JWK $jwk A key to store in the key set |
||
| 95 | * |
||
| 96 | * @return JWKSet |
||
| 97 | */ |
||
| 98 | public function with(JWK $jwk): JWKSet |
||
| 99 | { |
||
| 100 | $clone = clone $this; |
||
| 101 | |||
| 102 | if ($jwk->has('kid')) { |
||
| 103 | $clone->keys[$jwk->get('kid')] = $jwk; |
||
| 104 | } else { |
||
| 105 | $clone->keys[] = $jwk; |
||
| 106 | } |
||
| 107 | |||
| 108 | return $clone; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Remove key from the key set. |
||
| 113 | * |
||
| 114 | * @param int|string $key Key to remove from the key set |
||
| 115 | * |
||
| 116 | * @return JWKSet |
||
| 117 | */ |
||
| 118 | public function without($key): JWKSet |
||
| 119 | { |
||
| 120 | if (!$this->has($key)) { |
||
| 121 | return $this; |
||
| 122 | } |
||
| 123 | |||
| 124 | $clone = clone $this; |
||
| 125 | unset($clone->keys[$key]); |
||
| 126 | |||
| 127 | return $clone; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param int|string $index |
||
| 132 | * |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | public function has($index): bool |
||
| 136 | { |
||
| 137 | return array_key_exists($index, $this->keys); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param int|string $index |
||
| 142 | * |
||
| 143 | * @return JWK |
||
| 144 | */ |
||
| 145 | public function get($index): JWK |
||
| 146 | { |
||
| 147 | if (!$this->has($index)) { |
||
| 148 | throw new \InvalidArgumentException('Undefined index.'); |
||
| 149 | } |
||
| 150 | |||
| 151 | return $this->keys[$index]; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | public function jsonSerialize(): array |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param int $mode |
||
| 164 | * |
||
| 165 | * @return int |
||
| 166 | */ |
||
| 167 | public function count($mode = COUNT_NORMAL): int |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return JWK|null |
||
| 174 | */ |
||
| 175 | public function current(): ?JWK |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return int|string|null |
||
| 187 | */ |
||
| 188 | public function key() |
||
| 192 | |||
| 193 | public function next() |
||
| 197 | |||
| 198 | public function rewind() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | public function valid(): bool |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $type Must be 'sig' (signature) or 'enc' (encryption) |
||
| 213 | * @param AlgorithmInterface|null $algorithm Specifies the algorithm to be used |
||
| 214 | * @param array $restrictions More restrictions such as 'kid' or 'kty' |
||
| 215 | * |
||
| 216 | * @return JWK|null |
||
| 217 | */ |
||
| 218 | public function selectKey(string $type, ?AlgorithmInterface $algorithm = null, array $restrictions = []): ?JWK |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param string $type |
||
| 258 | * @param JWK $key |
||
| 259 | * |
||
| 260 | * @return bool|int |
||
| 261 | */ |
||
| 262 | private function canKeyBeUsedFor(string $type, JWK $key) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param null|AlgorithmInterface $algorithm |
||
| 276 | * @param JWK $key |
||
| 277 | * |
||
| 278 | * @return bool|int |
||
| 279 | */ |
||
| 280 | private function canKeyBeUsedWithAlgorithm(?AlgorithmInterface $algorithm, JWK $key) |
||
| 281 | { |
||
| 282 | if (null === $algorithm || $algorithm->keyType() !== $key->get('kty')) { |
||
| 283 | return 0; |
||
| 284 | } |
||
| 285 | if ($key->has('alg')) { |
||
| 286 | return $algorithm->name() === $key->get('alg') ? 2 : false; |
||
| 287 | } |
||
| 288 | |||
| 289 | return 1; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param array $restrictions |
||
| 294 | * @param JWK $key |
||
| 295 | * |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | private function doesKeySatisfyRestrictions(array $restrictions, JWK $key): bool |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param string $key_ops |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | private static function convertKeyOpsToKeyUse(string $key_ops): string |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param array $a |
||
| 332 | * @param array $b |
||
| 333 | * |
||
| 334 | * @return int |
||
| 335 | */ |
||
| 336 | public function sortKeys(array $a, array $b): int |
||
| 344 | } |
||
| 345 |