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 JWKSetInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var int |
||
| 23 | */ |
||
| 24 | private $position = 0; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $keys = []; |
||
| 30 | |||
| 31 | public function __construct(array $keys = []) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritdoc} |
||
| 42 | */ |
||
| 43 | public function hasKey($index) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritdoc} |
||
| 50 | */ |
||
| 51 | public function getKey($index) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * {@inheritdoc} |
||
| 60 | */ |
||
| 61 | public function getKeys() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | public function addKey(JWKInterface $key) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | public function removeKey($key) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | public function jsonSerialize() |
||
| 88 | { |
||
| 89 | return ['keys' => array_values($this->getKeys())]; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritdoc} |
||
| 94 | */ |
||
| 95 | public function count($mode = COUNT_NORMAL) |
||
| 96 | { |
||
| 97 | return count($this->getKeys(), $mode); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritdoc} |
||
| 102 | */ |
||
| 103 | public function current() |
||
| 104 | { |
||
| 105 | return isset($this->keys[$this->position]) ? $this->keys[$this->position] : null; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | */ |
||
| 111 | public function key() |
||
| 112 | { |
||
| 113 | return $this->position; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritdoc} |
||
| 118 | */ |
||
| 119 | public function next() |
||
| 120 | { |
||
| 121 | $this->position++; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | public function rewind() |
||
| 128 | { |
||
| 129 | $this->position = 0; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | public function valid() |
||
| 136 | { |
||
| 137 | return $this->current() instanceof JWKInterface; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | public function countKeys() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritdoc} |
||
| 150 | */ |
||
| 151 | public function offsetExists($offset) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | */ |
||
| 159 | public function offsetGet($offset) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | public function offsetSet($offset, $value) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | public function offsetUnset($offset) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * {@inheritdoc} |
||
| 182 | */ |
||
| 183 | public function selectKey($type, $algorithm = null, array $restrictions = []) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $type |
||
| 228 | * @param \Jose\Object\JWKInterface $key |
||
| 229 | * |
||
| 230 | * @return bool|int |
||
| 231 | */ |
||
| 232 | private function canKeyBeUsedFor($type, JWKInterface $key) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param null|string $algorithm |
||
| 246 | * @param \Jose\Object\JWKInterface $key |
||
| 247 | * |
||
| 248 | * @return bool|int |
||
| 249 | */ |
||
| 250 | private function canKeyBeUsedWithAlgorithm($algorithm, JWKInterface $key) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param array $restrictions |
||
| 264 | * @param \Jose\Object\JWKInterface $key |
||
| 265 | * |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | private function doesKeySatisfyRestrictions(array $restrictions, JWKInterface $key) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $key_ops |
||
| 281 | * |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | private static function convertKeyOpsToKeyUse($key_ops) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param array $a |
||
| 302 | * @param array $b |
||
| 303 | * |
||
| 304 | * @return int |
||
| 305 | */ |
||
| 306 | public function sortKeys($a, $b) |
||
| 314 | } |
||
| 315 |