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 | protected $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 | */ |
||
| 61 | 56 | public function __construct(EcAdapterInterface $ecAdapter, $depth, $parentFingerprint, $sequence, BufferInterface $chainCode, KeyInterface $key) |
|
| 62 | { |
||
| 63 | 56 | if ($depth < 0 || $depth > IntRange::U8_MAX) { |
|
| 64 | throw new \InvalidArgumentException('Invalid depth for BIP32 key, must be in range [0 - 255] inclusive'); |
||
| 65 | } |
||
| 66 | |||
| 67 | 56 | if ($parentFingerprint < 0 || $parentFingerprint > IntRange::U32_MAX) { |
|
| 68 | throw new \InvalidArgumentException('Invalid fingerprint for BIP32 key, must be in range [0 - (2^31)-1] inclusive'); |
||
| 69 | } |
||
| 70 | |||
| 71 | 56 | if ($sequence < 0 || $sequence > IntRange::U32_MAX) { |
|
| 72 | throw new \InvalidArgumentException('Invalid sequence for BIP32 key, must be in range [0 - (2^31)-1] inclusive'); |
||
| 73 | } |
||
| 74 | |||
| 75 | 56 | if ($chainCode->getSize() !== 32) { |
|
| 76 | throw new \RuntimeException('Chaincode should be 32 bytes'); |
||
| 77 | } |
||
| 78 | |||
| 79 | 56 | if (!$key->isCompressed()) { |
|
| 80 | 1 | throw new \InvalidArgumentException('A HierarchicalKey must always be compressed'); |
|
| 81 | } |
||
| 82 | |||
| 83 | 55 | $this->ecAdapter = $ecAdapter; |
|
| 84 | 55 | $this->depth = $depth; |
|
| 85 | 55 | $this->sequence = $sequence; |
|
| 86 | 55 | $this->parentFingerprint = $parentFingerprint; |
|
| 87 | 55 | $this->chainCode = $chainCode; |
|
| 88 | 55 | $this->key = $key; |
|
|
|
|||
| 89 | 55 | } |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Return the depth of this key. This is limited to 256 sequential derivations. |
||
| 93 | * |
||
| 94 | * @return int |
||
| 95 | */ |
||
| 96 | 21 | public function getDepth() |
|
| 97 | { |
||
| 98 | 21 | return $this->depth; |
|
| 99 | } |
||
| 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 | * |
||
| 106 | * @return int |
||
| 107 | */ |
||
| 108 | 16 | 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 | 17 | public function getFingerprint() |
|
| 119 | { |
||
| 120 | 17 | if ($this->getDepth() === 0) { |
|
| 121 | 11 | return 0; |
|
| 122 | } |
||
| 123 | |||
| 124 | 17 | return $this->parentFingerprint; |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Return the fingerprint to be used for child keys. |
||
| 129 | * @return int |
||
| 130 | */ |
||
| 131 | 18 | public function getChildFingerprint() |
|
| 132 | { |
||
| 133 | 18 | return $this->getPublicKey()->getPubKeyHash()->slice(0, 4)->getInt(); |
|
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Return the chain code - a deterministic 'salt' for HMAC-SHA512 |
||
| 138 | * in child derivations |
||
| 139 | * |
||
| 140 | * @return BufferInterface |
||
| 141 | */ |
||
| 142 | 15 | public function getChainCode() |
|
| 143 | { |
||
| 144 | 15 | return $this->chainCode; |
|
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return PrivateKeyInterface |
||
| 149 | */ |
||
| 150 | 27 | public function getPrivateKey() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Get the public key the private key or public key. |
||
| 161 | * |
||
| 162 | * @return PublicKeyInterface |
||
| 163 | */ |
||
| 164 | 29 | public function getPublicKey() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @return HierarchicalKey |
||
| 175 | */ |
||
| 176 | 12 | public function withoutPrivateKey() |
|
| 177 | { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Return whether this is a private key |
||
| 185 | * |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | 33 | public function isPrivate() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Return whether the key is hardened |
||
| 195 | * |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | 2 | public function isHardened() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Create a buffer containing data to be hashed hashed to yield the child offset |
||
| 205 | * |
||
| 206 | * @param int $sequence |
||
| 207 | * @return BufferInterface |
||
| 208 | * @throws \Exception |
||
| 209 | */ |
||
| 210 | 24 | public function getHmacSeed($sequence) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @param $nextDepth |
||
| 233 | * @param $sequence |
||
| 234 | * @param BufferInterface $chainCode |
||
| 235 | * @param KeyInterface $key |
||
| 236 | * @return HierarchicalKey |
||
| 237 | * @throws \Exception |
||
| 238 | */ |
||
| 239 | 16 | protected function childKey($nextDepth, $sequence, BufferInterface $chainCode, KeyInterface $key) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Derive a child key |
||
| 253 | * |
||
| 254 | * @param int $sequence |
||
| 255 | * @return HierarchicalKey |
||
| 256 | * @throws \Exception |
||
| 257 | */ |
||
| 258 | 21 | public function deriveChild($sequence) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @param array|\stdClass|\Traversable $list |
||
| 281 | * @return HierarchicalKey |
||
| 282 | */ |
||
| 283 | 11 | public function deriveFromList($list) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Decodes a BIP32 path into actual 32bit sequence numbers and derives the child key |
||
| 299 | * |
||
| 300 | * @param string $path |
||
| 301 | * @return HierarchicalKey |
||
| 302 | * @throws \Exception |
||
| 303 | */ |
||
| 304 | 11 | public function derivePath($path) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Serializes the instance according to whether it wraps a private or public key. |
||
| 312 | * @param NetworkInterface $network |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | 12 | public function toExtendedKey(NetworkInterface $network = null) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Explicitly serialize as a private key. Throws an exception if |
||
| 326 | * the key isn't private. |
||
| 327 | * |
||
| 328 | * @param NetworkInterface $network |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | 12 | public function toExtendedPrivateKey(NetworkInterface $network = null) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Explicitly serialize as a public key. This will always work. |
||
| 342 | * |
||
| 343 | * @param NetworkInterface $network |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | 9 | public function toExtendedPublicKey(NetworkInterface $network = null) |
|
| 350 | } |
||
| 351 |
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..