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 PublicKeyInterface|PrivateKeyInterface |
||
| 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() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Get the public key the private key or public key. |
||
| 175 | * |
||
| 176 | * @return PublicKeyInterface |
||
| 177 | */ |
||
| 178 | 48 | public function getPublicKey() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @return HierarchicalKey |
||
| 189 | */ |
||
| 190 | 25 | public function withoutPrivateKey() |
|
| 196 | |||
| 197 | /** |
||
| 198 | public function withScriptFactory(ScriptDataFactory $factory) |
||
| 199 | { |
||
| 200 | $clone = clone $this; |
||
| 201 | $clone->scriptDataFactory = $factory; |
||
| 202 | return $clone; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return ScriptDataFactory |
||
| 207 | */ |
||
| 208 | 28 | public function getScriptDataFactory() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @return \BitWasp\Bitcoin\Key\KeyToScript\ScriptAndSignData |
||
| 215 | */ |
||
| 216 | 22 | public function getScriptAndSignData() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @param BaseAddressCreator $addressCreator |
||
| 227 | * @return \BitWasp\Bitcoin\Address\Address |
||
| 228 | * @throws \BitWasp\Bitcoin\Exceptions\UnrecognizedScriptForAddressException |
||
| 229 | */ |
||
| 230 | 16 | public function getAddress(BaseAddressCreator $addressCreator) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Return whether this is a private key |
||
| 237 | * |
||
| 238 | * @return bool |
||
| 239 | */ |
||
| 240 | 52 | public function isPrivate() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Return whether the key is hardened |
||
| 247 | * |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | 2 | public function isHardened() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Create a buffer containing data to be hashed hashed to yield the child offset |
||
| 257 | * |
||
| 258 | * @param int $sequence |
||
| 259 | * @return BufferInterface |
||
| 260 | * @throws \Exception |
||
| 261 | */ |
||
| 262 | 43 | public function getHmacSeed($sequence) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * @param $nextDepth |
||
| 285 | * @param $sequence |
||
| 286 | * @param BufferInterface $chainCode |
||
| 287 | * @param KeyInterface $key |
||
| 288 | * @return HierarchicalKey |
||
| 289 | * @throws \Exception |
||
| 290 | */ |
||
| 291 | 35 | protected function childKey($nextDepth, $sequence, BufferInterface $chainCode, KeyInterface $key) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Derive a child key |
||
| 306 | * |
||
| 307 | * @param int $sequence |
||
| 308 | * @return HierarchicalKey |
||
| 309 | * @throws \Exception |
||
| 310 | */ |
||
| 311 | 40 | public function deriveChild($sequence) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @param array|\stdClass|\Traversable $list |
||
| 334 | * @return HierarchicalKey |
||
| 335 | */ |
||
| 336 | 30 | public function deriveFromList($list) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Decodes a BIP32 path into actual 32bit sequence numbers and derives the child key |
||
| 352 | * |
||
| 353 | * @param string $path |
||
| 354 | * @return HierarchicalKey |
||
| 355 | * @throws \Exception |
||
| 356 | */ |
||
| 357 | 30 | public function derivePath($path) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Serializes the instance according to whether it wraps a private or public key. |
||
| 365 | * @param NetworkInterface $network |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | 16 | public function toExtendedKey(NetworkInterface $network = null) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Explicitly serialize as a private key. Throws an exception if |
||
| 379 | * the key isn't private. |
||
| 380 | * |
||
| 381 | * @param NetworkInterface $network |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | 16 | public function toExtendedPrivateKey(NetworkInterface $network = null) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Explicitly serialize as a public key. This will always work. |
||
| 395 | * |
||
| 396 | * @param NetworkInterface $network |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | 13 | public function toExtendedPublicKey(NetworkInterface $network = null) |
|
| 403 | } |
||
| 404 |
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..