Complex classes like SizeEstimation 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 SizeEstimation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class SizeEstimation |
||
| 15 | { |
||
| 16 | const SIZE_DER_SIGNATURE = 72; |
||
| 17 | const SIZE_V0_P2WSH = 36; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @param bool $compressed |
||
| 21 | * @return int |
||
| 22 | */ |
||
| 23 | 3 | public static function getPublicKeySize($compressed = true) { |
|
| 26 | |||
| 27 | /** |
||
| 28 | * @param int $length |
||
| 29 | * @return int |
||
| 30 | */ |
||
| 31 | 41 | public static function getLengthOfScriptLengthElement($length) { |
|
| 44 | |||
| 45 | /** |
||
| 46 | * @param int $length |
||
| 47 | * @return int |
||
| 48 | */ |
||
| 49 | 48 | public static function getLengthOfVarInt($length) { |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @param array $vectorSizes |
||
| 66 | * @return int|mixed |
||
| 67 | */ |
||
| 68 | 13 | public static function getLengthOfVector(array $vectorSizes) { |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param Multisig $multisig |
||
| 78 | * @return array - first is array of stack sizes, second is script len |
||
| 79 | */ |
||
| 80 | 35 | public static function estimateMultisigStackSize(Multisig $multisig) { |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @param PayToPubKey $info |
||
| 98 | * @return array - first is array of stack sizes, second is script len |
||
| 99 | */ |
||
| 100 | public static function estimateP2PKStackSize(PayToPubKey $info) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param bool $isCompressed |
||
| 110 | * @return array - first is array of stack sizes, second is script len |
||
| 111 | */ |
||
| 112 | 3 | public static function estimateP2PKHStackSize($isCompressed = true) { |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @param array $stackSizes - array of integer size of a value (for scriptSig or witness) |
||
| 122 | * @param bool $isWitness |
||
| 123 | * @param ScriptInterface $redeemScript |
||
| 124 | * @param ScriptInterface $witnessScript |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | 35 | public static function estimateSizeForStack(array $stackSizes, $isWitness, ScriptInterface $redeemScript = null, ScriptInterface $witnessScript = null) { |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @param ScriptInterface $script - not the scriptPubKey, might be SPK,RS,WS |
||
| 169 | * @param ScriptInterface|null $redeemScript |
||
| 170 | * @param ScriptInterface $witnessScript |
||
| 171 | * @param bool $isWitness |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | 31 | public static function estimateInputFromScripts(ScriptInterface $script, ScriptInterface $redeemScript = null, ScriptInterface $witnessScript = null, $isWitness) { |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @param UTXO $utxo |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | 31 | public static function estimateUtxo(UTXO $utxo) { |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param ScriptInterface $scriptPubKey |
||
| 201 | * @param ScriptInterface $redeemScript |
||
| 202 | * @param ScriptInterface $witnessScript |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | 31 | public static function estimateUtxoFromScripts( |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @param UTXO[] $utxos |
||
| 247 | * @param bool $withWitness |
||
| 248 | * @return integer |
||
| 249 | */ |
||
| 250 | 26 | public static function estimateInputsSize(array $utxos, $withWitness) { |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @param array $outputs |
||
| 272 | * @return int |
||
| 273 | */ |
||
| 274 | 26 | public static function estimateOutputsSize(array $outputs) { |
|
| 275 | 26 | $outputSize = 0; |
|
| 276 | 26 | foreach ($outputs as $output) { |
|
| 277 | 24 | $size = 8; |
|
| 278 | |||
| 279 | 24 | $scriptSize = null; |
|
| 280 | 24 | if ($output instanceof TransactionOutputInterface) { |
|
| 281 | 9 | $scriptSize = $output->getScript()->getBuffer()->getSize(); |
|
| 282 | } else { |
||
| 283 | 19 | if (isset($output['scriptPubKey'])) { |
|
| 284 | 19 | if ($output['scriptPubKey'] instanceof ScriptInterface) { |
|
| 285 | 19 | $scriptSize = $output['scriptPubKey']->getBuffer()->getSize(); |
|
| 286 | } else { |
||
| 287 | 19 | $scriptSize = strlen($output['scriptPubKey']) / 2; |
|
| 288 | } |
||
| 289 | } else { |
||
| 290 | 12 | $scriptSize = 25; |
|
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | 24 | $size += SizeEstimation::getLengthOfVarInt($scriptSize) + $scriptSize; |
|
| 295 | 24 | $outputSize += $size; |
|
| 296 | } |
||
| 297 | 26 | return $outputSize; |
|
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param UTXO[] $utxos |
||
| 302 | * @param array $outputs |
||
| 303 | * @return int |
||
| 304 | */ |
||
| 305 | 26 | public static function estimateVsize(array $utxos, array $outputs) { |
|
| 308 | |||
| 309 | /** |
||
| 310 | * @param UTXO[] $utxos |
||
| 311 | * @param array $outputs |
||
| 312 | * @return int |
||
| 313 | */ |
||
| 314 | 26 | public static function estimateWeight(array $utxos, array $outputs) { |
|
| 328 | } |
||
| 329 |
If you have a function call in the test part of a
forloop, this function is executed on each iteration. Often such a function, can be moved to the initialization part and be cached.