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 |
||
| 13 | class SizeEstimation |
||
| 14 | { |
||
| 15 | const SIZE_DER_SIGNATURE = 72; |
||
| 16 | const SIZE_V0_P2WSH = 36; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @param bool $compressed |
||
| 20 | * @return int |
||
| 21 | */ |
||
| 22 | public static function getPublicKeySize($compressed = true) { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param int $length |
||
| 28 | * @return int |
||
| 29 | */ |
||
| 30 | 25 | public static function getLengthOfScriptLengthElement($length) { |
|
| 43 | |||
| 44 | /** |
||
| 45 | * @param int $length |
||
| 46 | * @return int |
||
| 47 | */ |
||
| 48 | 29 | public static function getLengthOfVarInt($length) { |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @param array $vectorSizes |
||
| 65 | * @return int|mixed |
||
| 66 | */ |
||
| 67 | 7 | public static function getLengthOfVector(array $vectorSizes) { |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @param Multisig $multisig |
||
| 77 | * @return array - first is array of stack sizes, second is script len |
||
| 78 | */ |
||
| 79 | 19 | public static function estimateMultisigStackSize(Multisig $multisig) { |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @param PayToPubKey $info |
||
| 97 | * @return array - first is array of stack sizes, second is script len |
||
| 98 | */ |
||
| 99 | public static function estimateP2PKStackSize(PayToPubKey $info) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param bool $isCompressed |
||
| 109 | * @return array - first is array of stack sizes, second is script len |
||
| 110 | */ |
||
| 111 | public static function estimateP2PKHStackSize($isCompressed = true) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param array $stackSizes - array of integer size of a value (for scriptSig or witness) |
||
| 121 | * @param bool $isWitness |
||
| 122 | * @param ScriptInterface $redeemScript |
||
| 123 | * @param ScriptInterface $witnessScript |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | 16 | public static function estimateSizeForStack(array $stackSizes, $isWitness, ScriptInterface $redeemScript = null, ScriptInterface $witnessScript = null) { |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @param ScriptInterface $script - not the scriptPubKey, might be SPK,RS,WS |
||
| 168 | * @param ScriptInterface|null $redeemScript |
||
| 169 | * @param ScriptInterface $witnessScript |
||
| 170 | * @param bool $isWitness |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | 12 | public static function estimateInputFromScripts(ScriptInterface $script, ScriptInterface $redeemScript = null, ScriptInterface $witnessScript = null, $isWitness) { |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @param UTXO $utxo |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | 12 | public static function estimateUtxo(UTXO $utxo) { |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @param UTXO[] $utxos |
||
| 232 | * @param bool $withWitness |
||
| 233 | * @return integer |
||
| 234 | */ |
||
| 235 | 7 | public static function estimateInputsSize(array $utxos, $withWitness) { |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @param array $outputs |
||
| 257 | * @return int |
||
| 258 | */ |
||
| 259 | 7 | public static function estimateOutputsSize(array $outputs) { |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @param UTXO[] $utxos |
||
| 278 | * @param array $outputs |
||
| 279 | * @return int |
||
| 280 | */ |
||
| 281 | 7 | public static function estimateVsize(array $utxos, array $outputs) { |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @param UTXO[] $utxos |
||
| 287 | * @param array $outputs |
||
| 288 | * @return int |
||
| 289 | */ |
||
| 290 | 7 | public static function estimateWeight(array $utxos, array $outputs) { |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @param UTXO[] $utxos |
||
| 300 | * @param array $outputs |
||
| 301 | * @return int |
||
| 302 | */ |
||
| 303 | public static function estimateLegacySize(array $utxos, array $outputs) { |
||
| 309 | } |
||
| 310 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.