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 | 52 | * @var ScriptAndSignData|null |
|
| 62 | */ |
||
| 63 | 52 | private $scriptAndSignData; |
|
| 64 | |||
| 65 | /** |
||
| 66 | * @param EcAdapterInterface $ecAdapter |
||
| 67 | 52 | * @param ScriptDataFactory $scriptDataFactory |
|
| 68 | * @param int $depth |
||
| 69 | * @param int $parentFingerprint |
||
| 70 | * @param int $sequence |
||
| 71 | 52 | * @param BufferInterface $chainCode |
|
| 72 | * @param KeyInterface $key |
||
| 73 | */ |
||
| 74 | public function __construct(EcAdapterInterface $ecAdapter, ScriptDataFactory $scriptDataFactory, int $depth, int $parentFingerprint, int $sequence, BufferInterface $chainCode, KeyInterface $key) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Return the depth of this key. This is limited to 256 sequential derivations. |
||
| 107 | * |
||
| 108 | 12 | * @return int |
|
| 109 | */ |
||
| 110 | 12 | public function getDepth(): int |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Get the sequence number for this address. Hardened keys are |
||
| 117 | * created with sequence > 0x80000000. a sequence number lower |
||
| 118 | 13 | * than this can be derived with the public key. |
|
| 119 | * |
||
| 120 | 13 | * @return int |
|
| 121 | 11 | */ |
|
| 122 | public function getSequence(): int |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get the fingerprint of the parent key. For master keys, this is 00000000. |
||
| 129 | * |
||
| 130 | * @return int |
||
| 131 | 14 | */ |
|
| 132 | public function getFingerprint(): int |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Return the fingerprint to be used for child keys. |
||
| 143 | 11 | * @return int |
|
| 144 | */ |
||
| 145 | 11 | public function getChildFingerprint(): int |
|
| 150 | |||
| 151 | 23 | /** |
|
| 152 | * Return the chain code - a deterministic 'salt' for HMAC-SHA512 |
||
| 153 | 23 | * in child derivations |
|
| 154 | 21 | * |
|
| 155 | * @return BufferInterface |
||
| 156 | */ |
||
| 157 | 2 | public function getChainCode(): BufferInterface |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @return PrivateKeyInterface |
||
| 164 | */ |
||
| 165 | 25 | public function getPrivateKey(): PrivateKeyInterface |
|
| 175 | |||
| 176 | /** |
||
| 177 | 9 | * Get the public key the private key or public key. |
|
| 178 | * |
||
| 179 | 9 | * @return PublicKeyInterface |
|
| 180 | 9 | */ |
|
| 181 | 9 | public function getPublicKey(): PublicKeyInterface |
|
| 191 | 29 | ||
| 192 | /** |
||
| 193 | * @return HierarchicalKey |
||
| 194 | */ |
||
| 195 | public function withoutPrivateKey(): HierarchicalKey |
||
| 201 | 2 | ||
| 202 | /** |
||
| 203 | * @param ScriptDataFactory $factory |
||
| 204 | * @return HierarchicalKey |
||
| 205 | */ |
||
| 206 | public function withScriptFactory(ScriptDataFactory $factory) |
||
| 212 | |||
| 213 | 20 | /** |
|
| 214 | 6 | * @return ScriptDataFactory |
|
| 215 | */ |
||
| 216 | public function getScriptDataFactory() |
||
| 220 | |||
| 221 | /** |
||
| 222 | 9 | * @return \BitWasp\Bitcoin\Key\KeyToScript\ScriptAndSignData |
|
| 223 | */ |
||
| 224 | 12 | public function getScriptAndSignData() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @param BaseAddressCreator $addressCreator |
||
| 235 | * @return \BitWasp\Bitcoin\Address\Address |
||
| 236 | */ |
||
| 237 | 17 | public function getAddress(BaseAddressCreator $addressCreator) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Return whether this is a private key |
||
| 244 | 17 | * |
|
| 245 | 12 | * @return bool |
|
| 246 | 12 | */ |
|
| 247 | public function isPrivate(): bool |
||
| 251 | |||
| 252 | 12 | /** |
|
| 253 | 12 | * Return whether the key is hardened |
|
| 254 | * |
||
| 255 | 12 | * @return bool |
|
| 256 | 12 | */ |
|
| 257 | 12 | public function isHardened(): bool |
|
| 261 | 12 | ||
| 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 | 7 | public function getHmacSeed(int $sequence): BufferInterface |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Derive a child key |
||
| 290 | 7 | * |
|
| 291 | * @param int $sequence |
||
| 292 | 7 | * @return HierarchicalKey |
|
| 293 | 7 | * @throws \Exception |
|
| 294 | */ |
||
| 295 | public function deriveChild(int $sequence): HierarchicalKey |
||
| 323 | 9 | ||
| 324 | /** |
||
| 325 | * @param array|\stdClass|\Traversable $list |
||
| 326 | * @return HierarchicalKey |
||
| 327 | * @throws \Exception |
||
| 328 | */ |
||
| 329 | public function deriveFromList($list): HierarchicalKey |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Decodes a BIP32 path into actual 32bit sequence numbers and derives the child key |
||
| 345 | * |
||
| 346 | * @param string $path |
||
| 347 | * @return HierarchicalKey |
||
| 348 | * @throws \Exception |
||
| 349 | */ |
||
| 350 | public function derivePath(string $path): HierarchicalKey |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Serializes the instance according to whether it wraps a private or public key. |
||
| 358 | * @param NetworkInterface $network |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | public function toExtendedKey(NetworkInterface $network = null): string |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Explicitly serialize as a private key. Throws an exception if |
||
| 372 | * the key isn't private. |
||
| 373 | * |
||
| 374 | * @param NetworkInterface $network |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public function toExtendedPrivateKey(NetworkInterface $network = null): string |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Explicitly serialize as a public key. This will always work. |
||
| 388 | * |
||
| 389 | * @param NetworkInterface $network |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | public function toExtendedPublicKey(NetworkInterface $network = null): string |
||
| 396 | } |
||
| 397 |
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.