Complex classes like Transaction 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 Transaction, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Transaction extends Serializable implements TransactionInterface |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var int |
||
| 17 | */ |
||
| 18 | private $version; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var TransactionInputInterface[] |
||
| 22 | */ |
||
| 23 | private $inputs; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var TransactionOutputInterface[] |
||
| 27 | */ |
||
| 28 | private $outputs; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var ScriptWitnessInterface[] |
||
| 32 | */ |
||
| 33 | private $witness; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | private $lockTime; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var BufferInterface |
||
| 42 | */ |
||
| 43 | private $wtxid; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var BufferInterface |
||
| 47 | */ |
||
| 48 | private $hash; |
||
| 49 | |||
| 50 | 2914 | /** |
|
| 51 | * Transaction constructor. |
||
| 52 | * |
||
| 53 | * @param int $nVersion |
||
| 54 | * @param TransactionInputInterface[] $vin |
||
| 55 | * @param TransactionOutputInterface[] $vout |
||
| 56 | * @param ScriptWitnessInterface[] $vwit |
||
| 57 | 2914 | * @param int $nLockTime |
|
| 58 | 6 | */ |
|
| 59 | public function __construct( |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @return BufferInterface |
||
| 90 | */ |
||
| 91 | 2380 | public function getTxHash() |
|
| 98 | |||
| 99 | 2380 | /** |
|
| 100 | * @return BufferInterface |
||
| 101 | 2380 | */ |
|
| 102 | public function getTxId() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return BufferInterface |
||
| 109 | */ |
||
| 110 | public function getWitnessTxId() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return int |
||
| 121 | */ |
||
| 122 | public function getVersion() |
||
| 126 | |||
| 127 | 2698 | /** |
|
| 128 | * Get the array of inputs in the transaction |
||
| 129 | * |
||
| 130 | * @return TransactionInputInterface[] |
||
| 131 | */ |
||
| 132 | public function getInputs() |
||
| 136 | 262 | ||
| 137 | 6 | /** |
|
| 138 | * @param int $index |
||
| 139 | 256 | * @return TransactionInputInterface |
|
| 140 | */ |
||
| 141 | public function getInput($index) |
||
| 148 | |||
| 149 | 2674 | /** |
|
| 150 | * Get Outputs |
||
| 151 | * |
||
| 152 | * @return TransactionOutputInterface[] |
||
| 153 | */ |
||
| 154 | public function getOutputs() |
||
| 158 | 2368 | ||
| 159 | 6 | /** |
|
| 160 | * @param int $vout |
||
| 161 | 2362 | * @return TransactionOutputInterface |
|
| 162 | */ |
||
| 163 | public function getOutput($vout) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | public function hasWitness() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return ScriptWitnessInterface[] |
||
| 187 | */ |
||
| 188 | public function getWitnesses() |
||
| 192 | 50 | ||
| 193 | /** |
||
| 194 | * @param int $index |
||
| 195 | 50 | * @return ScriptWitnessInterface |
|
| 196 | */ |
||
| 197 | public function getWitness($index) |
||
| 204 | 2296 | ||
| 205 | 2296 | /** |
|
| 206 | * @param int $vout |
||
| 207 | * @return OutPointInterface |
||
| 208 | */ |
||
| 209 | public function makeOutpoint($vout) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param int $vout |
||
| 217 | * @return Utxo |
||
| 218 | */ |
||
| 219 | public function makeUtxo($vout) |
||
| 223 | |||
| 224 | 2650 | /** |
|
| 225 | * Get Lock Time |
||
| 226 | * |
||
| 227 | * @return int |
||
| 228 | */ |
||
| 229 | public function getLockTime() |
||
| 233 | 6 | ||
| 234 | 6 | /** |
|
| 235 | 6 | * @return int|string |
|
| 236 | 3 | */ |
|
| 237 | public function getValueOut() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | public function isCoinbase() |
||
| 255 | 6 | ||
| 256 | 6 | /** |
|
| 257 | 6 | * @param TransactionInterface $tx |
|
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | 6 | public function equals(TransactionInterface $tx) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @return BufferInterface |
||
| 300 | */ |
||
| 301 | public function getBuffer() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @return BufferInterface |
||
| 308 | */ |
||
| 309 | public function getBaseSerialization() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @return BufferInterface |
||
| 316 | */ |
||
| 317 | public function getWitnessSerialization() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return BufferInterface |
||
| 328 | */ |
||
| 329 | public function getWitnessBuffer() |
||
| 333 | } |
||
| 334 |