Complex classes like TransactionBuilder 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 TransactionBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class TransactionBuilder { |
||
| 20 | |||
| 21 | const OP_RETURN = '6a'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var UTXO[] |
||
| 25 | */ |
||
| 26 | private $utxos = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array[] |
||
| 30 | */ |
||
| 31 | private $outputs = []; |
||
| 32 | |||
| 33 | private $changeAddress = null; |
||
| 34 | private $randomizeChangeOutput = true; |
||
| 35 | |||
| 36 | private $fee = null; |
||
| 37 | |||
| 38 | private $validateFee = null; |
||
| 39 | |||
| 40 | private $feeStrategy = Wallet::FEE_STRATEGY_OPTIMAL; |
||
| 41 | 9 | ||
| 42 | 9 | public function __construct() { |
|
| 44 | |||
| 45 | /** |
||
| 46 | * @param string $txId transactionId (hash) |
||
| 47 | * @param int $index index of the output being spent |
||
| 48 | * @param string $value when NULL we'll use the data API to fetch the value |
||
| 49 | * @param AddressInterface|string $address when NULL we'll use the data API to fetch the address |
||
| 50 | * @param ScriptInterface|string $scriptPubKey as HEX, when NULL we'll use the data API to fetch the scriptpubkey |
||
| 51 | * @param string $path when NULL we'll use the API to determine the path for the specified address |
||
| 52 | * @param ScriptInterface|string $redeemScript when NULL we'll use the path to determine the $redeemScript |
||
| 53 | * @param ScriptInterface|string $witnessScript when NULL we'll use the path to determine the $witnessScript |
||
| 54 | 3 | * @return $this |
|
| 55 | 3 | */ |
|
| 56 | 3 | public function spendOutput($txId, $index, $value = null, $address = null, $scriptPubKey = null, $path = null, $redeemScript = null, $witnessScript = null) { |
|
| 72 | 3 | ||
| 73 | /** |
||
| 74 | * @return UTXO[] |
||
| 75 | */ |
||
| 76 | public function getUtxos() { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * replace the currently set UTXOs with a new set |
||
| 82 | * |
||
| 83 | * @param UTXO[] $utxos |
||
| 84 | * @return $this |
||
| 85 | */ |
||
| 86 | public function setUtxos(array $utxos) { |
||
| 91 | |||
| 92 | /** |
||
| 93 | 9 | * @param string $address |
|
| 94 | 9 | * @param int $value |
|
| 95 | * @return $this |
||
| 96 | * @throws \Exception |
||
| 97 | */ |
||
| 98 | public function addRecipient($address, $value) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * add a 'raw' output, normally addRecipient or addOpReturn should be used |
||
| 130 | * |
||
| 131 | * @param array $output [value => int, address => string] |
||
| 132 | * or [value => int, scriptPubKey => string] (scriptPubKey should be hex) |
||
| 133 | * @return $this |
||
| 134 | */ |
||
| 135 | public function addOutput($output) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param $idx |
||
| 143 | * @param $output |
||
| 144 | * @return $this |
||
| 145 | */ |
||
| 146 | public function replaceOutput($idx, $output) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param $idx |
||
| 154 | * @param $value |
||
| 155 | * @return $this |
||
| 156 | * @throws \Exception |
||
| 157 | */ |
||
| 158 | public function updateOutputValue($idx, $value) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * add OP_RETURN output |
||
| 179 | 1 | * |
|
| 180 | 1 | * $data will be bin2hex and will be prefixed with a proper OP_PUSHDATA |
|
| 181 | 1 | * |
|
| 182 | 1 | * @param string $data |
|
| 183 | * @param bool $allowNonStandard when TRUE will allow scriptPubKey > 80 bytes (so $data > 80 bytes) |
||
| 184 | * @return $this |
||
| 185 | 1 | * @throws BlocktrailSDKException |
|
| 186 | 1 | */ |
|
| 187 | 1 | public function addOpReturn($data, $allowNonStandard = false) { |
|
| 205 | 8 | ||
| 206 | /** |
||
| 207 | * @param bool $json return data for JSON return (so objects -> string) |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | 9 | public function getOutputs($json = false) { |
|
| 226 | |||
| 227 | /** |
||
| 228 | * set change address |
||
| 229 | 3 | * |
|
| 230 | 3 | * @param string $address |
|
| 231 | * @return $this |
||
| 232 | */ |
||
| 233 | public function setChangeAddress($address) { |
||
| 238 | 9 | ||
| 239 | 9 | /** |
|
| 240 | * @return string|null |
||
| 241 | 9 | */ |
|
| 242 | public function getChangeAddress() { |
||
| 245 | 9 | ||
| 246 | /** |
||
| 247 | * @param string $feeStrategy |
||
| 248 | * @return $this |
||
| 249 | * @throws BlocktrailSDKException |
||
| 250 | */ |
||
| 251 | 9 | public function setFeeStrategy($feeStrategy) { |
|
| 260 | 9 | ||
| 261 | /** |
||
| 262 | 9 | * @return string |
|
| 263 | */ |
||
| 264 | public function getFeeStrategy() { |
||
| 267 | |||
| 268 | 3 | /** |
|
| 269 | 3 | * @param bool $randomizeChangeOutput |
|
| 270 | * @return $this |
||
| 271 | */ |
||
| 272 | public function randomizeChangeOutput($randomizeChangeOutput = true) { |
||
| 277 | |||
| 278 | 2 | /** |
|
| 279 | * @return bool |
||
| 280 | 2 | */ |
|
| 281 | public function shouldRandomizeChangeOuput() { |
||
| 284 | 2 | ||
| 285 | /** |
||
| 286 | 2 | * set desired fee (normally automatically calculated) |
|
| 287 | * |
||
| 288 | * @param int $value |
||
| 289 | * @return $this |
||
| 290 | */ |
||
| 291 | public function setFee($value) { |
||
| 301 | 2 | ||
| 302 | /** |
||
| 303 | 2 | * @return int|null |
|
| 304 | */ |
||
| 305 | public function getFee() { |
||
| 308 | |||
| 309 | 3 | /** |
|
| 310 | 3 | * @param int $fee |
|
| 311 | * @return $this |
||
| 312 | */ |
||
| 313 | public function validateFee($fee) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @return int|null |
||
| 321 | */ |
||
| 322 | public function getValidateFee() { |
||
| 325 | } |
||
| 326 |