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 = []) |
||
32 | { |
||
33 | if (array_key_exists('keys', $keys)) { |
||
34 | foreach ($keys['keys'] as $value) { |
||
35 | $this->addKey(new JWK($value)); |
||
36 | } |
||
37 | } |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | public function hasKey($index) |
||
44 | { |
||
45 | return array_key_exists($index, $this->keys); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | public function getKey($index) |
||
52 | { |
||
53 | Assertion::keyExists($this->keys, $index, 'Undefined index.'); |
||
54 | |||
55 | return $this->keys[$index]; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | public function getKeys() |
||
62 | { |
||
63 | return $this->keys; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | public function addKey(JWKInterface $key) |
||
70 | { |
||
71 | $this->keys[] = $key; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | public function removeKey($key) |
||
78 | { |
||
79 | if (isset($this->keys[$key])) { |
||
80 | unset($this->keys[$key]); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | public function jsonSerialize() |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | public function count($mode = COUNT_NORMAL) |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function current() |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function key() |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function next() |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | public function rewind() |
||
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | public function valid() |
||
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | public function countKeys() |
||
144 | { |
||
145 | return count($this->keys); |
||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | public function offsetExists($offset) |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | public function offsetGet($offset) |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function offsetSet($offset, $value) |
||
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | public function offsetUnset($offset) |
||
178 | |||
179 | /** |
||
180 | * @param string $type |
||
181 | * @param string|null $algorithm |
||
182 | * @param array $restrictions |
||
183 | * |
||
184 | * @return \Jose\Object\JWKInterface|null |
||
185 | */ |
||
186 | public function selectKey($type, $algorithm = null, array $restrictions = []) |
||
228 | |||
229 | /** |
||
230 | * @param string $type |
||
231 | * @param \Jose\Object\JWKInterface $key |
||
232 | * |
||
233 | * @return bool|int |
||
234 | */ |
||
235 | private function canKeyBeUsedFor($type, JWKInterface $key) |
||
246 | |||
247 | /** |
||
248 | * @param null|string $algorithm |
||
249 | * @param \Jose\Object\JWKInterface $key |
||
250 | * |
||
251 | * @return bool|int |
||
252 | */ |
||
253 | private function canKeyBeUsedWithAlgorithm($algorithm, JWKInterface $key) |
||
264 | |||
265 | /** |
||
266 | * @param array $restrictions |
||
267 | * @param \Jose\Object\JWKInterface $key |
||
268 | * |
||
269 | * @return bool |
||
270 | */ |
||
271 | private function doesKeySatisfyRestrictions(array $restrictions, JWKInterface $key) |
||
281 | |||
282 | /** |
||
283 | * @param string $key_ops |
||
284 | * |
||
285 | * @return string |
||
286 | */ |
||
287 | private static function convertKeyOpsToKeyUse($key_ops) |
||
302 | |||
303 | /** |
||
304 | * @param array $a |
||
305 | * @param array $b |
||
306 | * |
||
307 | * @return int |
||
308 | */ |
||
309 | public function sortKeys($a, $b) |
||
316 | } |
||
317 |