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 |
||
23 | class HierarchicalKey |
||
24 | { |
||
25 | /** |
||
26 | * @var EcAdapterInterface |
||
27 | */ |
||
28 | protected $ecAdapter; |
||
29 | |||
30 | /** |
||
31 | * @var int |
||
32 | */ |
||
33 | private $depth; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | private $parentFingerprint; |
||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | private $sequence; |
||
44 | |||
45 | /** |
||
46 | * @var BufferInterface |
||
47 | */ |
||
48 | private $chainCode; |
||
49 | |||
50 | /** |
||
51 | * @var KeyInterface |
||
52 | */ |
||
53 | private $key; |
||
54 | |||
55 | /** |
||
56 | * @var ScriptDataFactory |
||
57 | */ |
||
58 | private $scriptDataFactory; |
||
59 | |||
60 | /** |
||
61 | * @var ScriptAndSignData|null |
||
62 | */ |
||
63 | private $scriptAndSignData; |
||
64 | |||
65 | /** |
||
66 | * @param EcAdapterInterface $ecAdapter |
||
67 | * @param ScriptDataFactory $scriptDataFactory |
||
68 | * @param int $depth |
||
69 | * @param int $parentFingerprint |
||
70 | * @param int $sequence |
||
71 | * @param BufferInterface $chainCode |
||
72 | * @param KeyInterface $key |
||
73 | */ |
||
74 | 75 | public function __construct(EcAdapterInterface $ecAdapter, ScriptDataFactory $scriptDataFactory, $depth, $parentFingerprint, $sequence, BufferInterface $chainCode, KeyInterface $key) |
|
104 | |||
105 | /** |
||
106 | * Return the depth of this key. This is limited to 256 sequential derivations. |
||
107 | * |
||
108 | * @return int |
||
109 | */ |
||
110 | 34 | public function getDepth() |
|
114 | |||
115 | /** |
||
116 | * Get the sequence number for this address. Hardened keys are |
||
117 | * created with sequence > 0x80000000. a sequence number lower |
||
118 | * than this can be derived with the public key. |
||
119 | * |
||
120 | * @return int |
||
121 | */ |
||
122 | 29 | public function getSequence() |
|
126 | |||
127 | /** |
||
128 | * Get the fingerprint of the parent key. For master keys, this is 00000000. |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | 30 | public function getFingerprint() |
|
140 | |||
141 | /** |
||
142 | * Return the fingerprint to be used for child keys. |
||
143 | * @return int |
||
144 | */ |
||
145 | 37 | public function getChildFingerprint() |
|
149 | |||
150 | /** |
||
151 | * Return the chain code - a deterministic 'salt' for HMAC-SHA512 |
||
152 | * in child derivations |
||
153 | * |
||
154 | * @return BufferInterface |
||
155 | */ |
||
156 | 28 | public function getChainCode() |
|
160 | |||
161 | /** |
||
162 | * @return PrivateKeyInterface |
||
163 | */ |
||
164 | 46 | public function getPrivateKey() |
|
174 | |||
175 | /** |
||
176 | * Get the public key the private key or public key. |
||
177 | * |
||
178 | * @return PublicKeyInterface |
||
179 | */ |
||
180 | 48 | public function getPublicKey() |
|
190 | |||
191 | /** |
||
192 | * @return HierarchicalKey |
||
193 | */ |
||
194 | 25 | public function withoutPrivateKey() |
|
200 | |||
201 | /** |
||
202 | * @param ScriptDataFactory $factory |
||
203 | * @return HierarchicalKey |
||
204 | */ |
||
205 | 1 | public function withScriptFactory(ScriptDataFactory $factory) |
|
206 | { |
||
207 | 1 | $clone = clone $this; |
|
208 | 1 | $clone->scriptDataFactory = $factory; |
|
209 | 1 | $clone->scriptAndSignData = null; // we cache, don't forget to clear |
|
210 | 1 | return $clone; |
|
211 | } |
||
212 | |||
213 | /** |
||
214 | * @return ScriptDataFactory |
||
215 | */ |
||
216 | 28 | public function getScriptDataFactory() |
|
217 | { |
||
218 | 28 | return $this->scriptDataFactory; |
|
219 | } |
||
220 | |||
221 | /** |
||
222 | * @return \BitWasp\Bitcoin\Key\KeyToScript\ScriptAndSignData |
||
223 | */ |
||
224 | 23 | public function getScriptAndSignData() |
|
225 | { |
||
226 | 23 | if (null === $this->scriptAndSignData) { |
|
227 | 23 | $this->scriptAndSignData = $this->scriptDataFactory->convertKey($this->key); |
|
228 | } |
||
229 | |||
230 | 23 | return $this->scriptAndSignData; |
|
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param BaseAddressCreator $addressCreator |
||
235 | * @return \BitWasp\Bitcoin\Address\Address |
||
236 | */ |
||
237 | 17 | public function getAddress(BaseAddressCreator $addressCreator) |
|
238 | { |
||
239 | 17 | return $this->getScriptAndSignData()->getAddress($addressCreator); |
|
240 | } |
||
241 | |||
242 | /** |
||
243 | * Return whether this is a private key |
||
244 | * |
||
245 | * @return bool |
||
246 | */ |
||
247 | 52 | public function isPrivate() |
|
251 | |||
252 | /** |
||
253 | * Return whether the key is hardened |
||
254 | * |
||
255 | * @return bool |
||
256 | */ |
||
257 | 2 | public function isHardened() |
|
261 | |||
262 | /** |
||
263 | * Create a buffer containing data to be hashed hashed to yield the child offset |
||
264 | * |
||
265 | * @param int $sequence |
||
266 | * @return BufferInterface |
||
267 | * @throws \Exception |
||
268 | */ |
||
269 | 43 | public function getHmacSeed($sequence) |
|
289 | |||
290 | /** |
||
291 | * Derive a child key |
||
292 | * |
||
293 | * @param int $sequence |
||
294 | * @return HierarchicalKey |
||
295 | * @throws \Exception |
||
296 | */ |
||
297 | 40 | public function deriveChild($sequence) |
|
325 | |||
326 | /** |
||
327 | * @param array|\stdClass|\Traversable $list |
||
328 | * @return HierarchicalKey |
||
329 | * @throws \Exception |
||
330 | */ |
||
331 | 30 | public function deriveFromList($list) |
|
344 | |||
345 | /** |
||
346 | * Decodes a BIP32 path into actual 32bit sequence numbers and derives the child key |
||
347 | * |
||
348 | * @param string $path |
||
349 | * @return HierarchicalKey |
||
350 | * @throws \Exception |
||
351 | */ |
||
352 | 30 | public function derivePath($path) |
|
357 | |||
358 | /** |
||
359 | * Serializes the instance according to whether it wraps a private or public key. |
||
360 | * @param NetworkInterface $network |
||
361 | * @return string |
||
362 | */ |
||
363 | 16 | public function toExtendedKey(NetworkInterface $network = null) |
|
371 | |||
372 | /** |
||
373 | * Explicitly serialize as a private key. Throws an exception if |
||
374 | * the key isn't private. |
||
375 | * |
||
376 | * @param NetworkInterface $network |
||
377 | * @return string |
||
378 | */ |
||
379 | 16 | public function toExtendedPrivateKey(NetworkInterface $network = null) |
|
387 | |||
388 | /** |
||
389 | * Explicitly serialize as a public key. This will always work. |
||
390 | * |
||
391 | * @param NetworkInterface $network |
||
392 | * @return string |
||
393 | */ |
||
394 | 13 | public function toExtendedPublicKey(NetworkInterface $network = null) |
|
398 | } |
||
399 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.