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 |
||
20 | class HierarchicalKey |
||
21 | { |
||
22 | /** |
||
23 | * @var EcAdapterInterface |
||
24 | */ |
||
25 | private $ecAdapter; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | private $depth; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | private $parentFingerprint; |
||
36 | |||
37 | /** |
||
38 | * @var int |
||
39 | */ |
||
40 | private $sequence; |
||
41 | |||
42 | /** |
||
43 | * @var BufferInterface |
||
44 | */ |
||
45 | private $chainCode; |
||
46 | |||
47 | /** |
||
48 | * @var PublicKeyInterface|PrivateKeyInterface |
||
49 | */ |
||
50 | private $key; |
||
51 | |||
52 | /** |
||
53 | * @param EcAdapterInterface $ecAdapter |
||
54 | * @param int $depth |
||
55 | * @param int $parentFingerprint |
||
56 | * @param int $sequence |
||
57 | * @param BufferInterface $chainCode |
||
58 | * @param KeyInterface $key |
||
59 | * @throws \Exception |
||
60 | 150 | */ |
|
61 | public function __construct(EcAdapterInterface $ecAdapter, $depth, $parentFingerprint, $sequence, BufferInterface $chainCode, KeyInterface $key) |
||
90 | |||
91 | /** |
||
92 | * Return the depth of this key. This is limited to 256 sequential derivations. |
||
93 | * |
||
94 | * @return int |
||
95 | 36 | */ |
|
96 | public function getDepth() |
||
100 | |||
101 | /** |
||
102 | * Get the sequence number for this address. Hardened keys are |
||
103 | * created with sequence > 0x80000000. a sequence number lower |
||
104 | * than this can be derived with the public key. |
||
105 | 36 | * |
|
106 | * @return int |
||
107 | 36 | */ |
|
108 | 18 | public function getSequence() |
|
112 | |||
113 | /** |
||
114 | * Get the fingerprint of the parent key. For master keys, this is 00000000. |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | 42 | public function getFingerprint() |
|
126 | |||
127 | /** |
||
128 | * Return the fingerprint to be used for child keys. |
||
129 | 48 | * @return int |
|
130 | */ |
||
131 | 48 | public function getChildFingerprint() |
|
135 | |||
136 | /** |
||
137 | 78 | * Return the chain code - a deterministic 'salt' for HMAC-SHA512 |
|
138 | * in child derivations |
||
139 | 78 | * |
|
140 | 72 | * @return BufferInterface |
|
141 | */ |
||
142 | public function getChainCode() |
||
146 | |||
147 | /** |
||
148 | * @return PrivateKeyInterface |
||
149 | */ |
||
150 | public function getPrivateKey() |
||
158 | 3 | ||
159 | /** |
||
160 | * Get the public key the private key or public key. |
||
161 | * |
||
162 | * @return PublicKeyInterface |
||
163 | 24 | */ |
|
164 | public function getPublicKey() |
||
172 | |||
173 | /** |
||
174 | * @return HierarchicalKey |
||
175 | */ |
||
176 | public function toPublic() |
||
184 | |||
185 | /** |
||
186 | * Return whether this is a private key |
||
187 | 6 | * |
|
188 | * @return bool |
||
189 | 6 | */ |
|
190 | public function isPrivate() |
||
194 | |||
195 | /** |
||
196 | * Return whether the key is hardened |
||
197 | * |
||
198 | * @return bool |
||
199 | 42 | */ |
|
200 | public function isHardened() |
||
204 | |||
205 | /** |
||
206 | 24 | * Create a buffer containing data to be hashed hashed to yield the child offset |
|
207 | 12 | * |
|
208 | 36 | * @param int $sequence |
|
209 | * @return BufferInterface |
||
210 | * @throws \Exception |
||
211 | 36 | */ |
|
212 | 36 | public function getHmacSeed($sequence) |
|
232 | |||
233 | 36 | /** |
|
234 | 36 | * Derive a child key |
|
235 | * |
||
236 | 36 | * @param int $sequence |
|
237 | 36 | * @return HierarchicalKey |
|
238 | 36 | * @throws \Exception |
|
239 | 36 | */ |
|
240 | 18 | public function deriveChild($sequence) |
|
267 | |||
268 | /** |
||
269 | * @param array|\stdClass|\Traversable $list |
||
270 | * @return HierarchicalKey |
||
271 | 18 | */ |
|
272 | public function deriveFromList($list) |
||
285 | |||
286 | 30 | /** |
|
287 | 30 | * Decodes a BIP32 path into actual 32bit sequence numbers and derives the child key |
|
288 | 30 | * |
|
289 | * @param string $path |
||
290 | * @return HierarchicalKey |
||
291 | * @throws \Exception |
||
292 | */ |
||
293 | public function derivePath($path) |
||
298 | 30 | ||
299 | |||
300 | 30 | /** |
|
301 | 6 | * Serializes the instance according to whether it wraps a private or public key. |
|
302 | * @param NetworkInterface $network |
||
303 | * @return string |
||
304 | 24 | */ |
|
305 | public function toExtendedKey(NetworkInterface $network = null) |
||
313 | 24 | ||
314 | /** |
||
315 | 24 | * Explicitly serialize as a private key. Throws an exception if |
|
316 | 24 | * the key isn't private. |
|
317 | * |
||
318 | * @param NetworkInterface $network |
||
319 | * @return string |
||
320 | */ |
||
321 | public function toExtendedPrivateKey(NetworkInterface $network = null) |
||
329 | |||
330 | /** |
||
331 | * Explicitly serialize as a public key. This will always work. |
||
332 | * |
||
333 | * @param NetworkInterface $network |
||
334 | * @return string |
||
335 | */ |
||
336 | public function toExtendedPublicKey(NetworkInterface $network = null) |
||
341 | } |
||
342 |
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..