Complex classes like HierarchicalKey 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 HierarchicalKey, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class HierarchicalKey |
||
23 | { |
||
24 | /** |
||
25 | * @var EcAdapterInterface |
||
26 | */ |
||
27 | private $ecAdapter; |
||
28 | |||
29 | /** |
||
30 | * @var int |
||
31 | */ |
||
32 | private $depth; |
||
33 | |||
34 | /** |
||
35 | * @var int |
||
36 | */ |
||
37 | private $parentFingerprint; |
||
38 | |||
39 | /** |
||
40 | * @var int |
||
41 | */ |
||
42 | private $sequence; |
||
43 | |||
44 | /** |
||
45 | * @var BufferInterface |
||
46 | */ |
||
47 | private $chainCode; |
||
48 | |||
49 | /** |
||
50 | * @var PublicKeyInterface|PrivateKeyInterface |
||
51 | */ |
||
52 | private $key; |
||
53 | |||
54 | /** |
||
55 | * @param EcAdapterInterface $ecAdapter |
||
56 | * @param int $depth |
||
57 | * @param int $parentFingerprint |
||
58 | * @param int $sequence |
||
59 | * @param BufferInterface $chainCode |
||
60 | * @param KeyInterface $key |
||
61 | * @throws \Exception |
||
62 | */ |
||
63 | 32 | public function __construct(EcAdapterInterface $ecAdapter, int $depth, int $parentFingerprint, int $sequence, BufferInterface $chainCode, KeyInterface $key) |
|
92 | |||
93 | /** |
||
94 | * Return the depth of this key. This is limited to 256 sequential derivations. |
||
95 | * |
||
96 | * @return int |
||
97 | */ |
||
98 | 9 | public function getDepth(): int |
|
102 | |||
103 | /** |
||
104 | * Get the sequence number for this address. Hardened keys are |
||
105 | * created with sequence > 0x80000000. a sequence number lower |
||
106 | * than this can be derived with the public key. |
||
107 | * |
||
108 | * @return int |
||
109 | */ |
||
110 | 7 | public function getSequence(): int |
|
114 | |||
115 | /** |
||
116 | * Get the fingerprint of the parent key. For master keys, this is 00000000. |
||
117 | * |
||
118 | * @return int |
||
119 | */ |
||
120 | 7 | public function getFingerprint(): int |
|
128 | |||
129 | /** |
||
130 | * Return the fingerprint to be used for child keys. |
||
131 | * @return int |
||
132 | */ |
||
133 | 8 | public function getChildFingerprint(): int |
|
138 | |||
139 | /** |
||
140 | * Return the chain code - a deterministic 'salt' for HMAC-SHA512 |
||
141 | * in child derivations |
||
142 | * |
||
143 | * @return BufferInterface |
||
144 | */ |
||
145 | 6 | public function getChainCode() |
|
149 | |||
150 | /** |
||
151 | * @return PrivateKeyInterface |
||
152 | */ |
||
153 | 14 | public function getPrivateKey() |
|
161 | |||
162 | /** |
||
163 | * Get the public key the private key or public key. |
||
164 | * |
||
165 | * @return PublicKeyInterface |
||
166 | */ |
||
167 | 15 | public function getPublicKey() |
|
175 | |||
176 | /** |
||
177 | * @return HierarchicalKey |
||
178 | */ |
||
179 | 5 | public function toPublic() |
|
187 | |||
188 | /** |
||
189 | * Return whether this is a private key |
||
190 | * |
||
191 | * @return bool |
||
192 | */ |
||
193 | 17 | public function isPrivate() |
|
197 | |||
198 | /** |
||
199 | * Return whether the key is hardened |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | 1 | public function isHardened() |
|
207 | |||
208 | /** |
||
209 | * Create a buffer containing data to be hashed hashed to yield the child offset |
||
210 | * |
||
211 | * @param int $sequence |
||
212 | * @return BufferInterface |
||
213 | * @throws \Exception |
||
214 | */ |
||
215 | 14 | public function getHmacSeed($sequence) |
|
235 | |||
236 | /** |
||
237 | * Derive a child key |
||
238 | * |
||
239 | * @param int $sequence |
||
240 | * @return HierarchicalKey |
||
241 | * @throws \Exception |
||
242 | */ |
||
243 | 11 | public function deriveChild(int $sequence) |
|
270 | |||
271 | /** |
||
272 | * @param array|\stdClass|\Traversable $list |
||
273 | * @return HierarchicalKey |
||
274 | */ |
||
275 | 4 | public function deriveFromList($list) |
|
288 | |||
289 | /** |
||
290 | * Decodes a BIP32 path into actual 32bit sequence numbers and derives the child key |
||
291 | * |
||
292 | * @param string $path |
||
293 | * @return HierarchicalKey |
||
294 | * @throws \Exception |
||
295 | */ |
||
296 | 4 | public function derivePath($path) |
|
301 | |||
302 | /** |
||
303 | * Serializes the instance according to whether it wraps a private or public key. |
||
304 | * @param NetworkInterface $network |
||
305 | * @return string |
||
306 | */ |
||
307 | 6 | public function toExtendedKey(NetworkInterface $network = null) |
|
315 | |||
316 | /** |
||
317 | * Explicitly serialize as a private key. Throws an exception if |
||
318 | * the key isn't private. |
||
319 | * |
||
320 | * @param NetworkInterface $network |
||
321 | * @return string |
||
322 | */ |
||
323 | 6 | public function toExtendedPrivateKey(NetworkInterface $network = null) |
|
331 | |||
332 | /** |
||
333 | * Explicitly serialize as a public key. This will always work. |
||
334 | * |
||
335 | * @param NetworkInterface $network |
||
336 | * @return string |
||
337 | */ |
||
338 | 5 | public function toExtendedPublicKey(NetworkInterface $network = null) |
|
343 | } |
||
344 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..