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 | * Transaction constructor. |
||
42 | * |
||
43 | * @param int $nVersion |
||
44 | * @param TransactionInputInterface[] $vin |
||
45 | * @param TransactionOutputInterface[] $vout |
||
46 | * @param ScriptWitnessInterface[] $vwit |
||
47 | * @param int $nLockTime |
||
48 | */ |
||
49 | public function __construct( |
||
77 | 2902 | ||
78 | /** |
||
79 | * @return BufferInterface |
||
80 | */ |
||
81 | public function getTxHash() |
||
85 | |||
86 | 304 | /** |
|
87 | * @return BufferInterface |
||
88 | */ |
||
89 | public function getTxId() |
||
93 | 2380 | ||
94 | /** |
||
95 | * @return BufferInterface |
||
96 | */ |
||
97 | public function getWitnessTxId() |
||
101 | 2380 | ||
102 | /** |
||
103 | * @return int |
||
104 | */ |
||
105 | public function getVersion() |
||
109 | |||
110 | /** |
||
111 | * Get the array of inputs in the transaction |
||
112 | * |
||
113 | * @return TransactionInputInterface[] |
||
114 | */ |
||
115 | 2644 | public function getInputs() |
|
119 | |||
120 | /** |
||
121 | * @param int $index |
||
122 | * @return TransactionInputInterface |
||
123 | */ |
||
124 | public function getInput($index) |
||
131 | |||
132 | /** |
||
133 | * Get Outputs |
||
134 | 262 | * |
|
135 | * @return TransactionOutputInterface[] |
||
136 | 262 | */ |
|
137 | 6 | public function getOutputs() |
|
141 | |||
142 | /** |
||
143 | * @param int $vout |
||
144 | * @return TransactionOutputInterface |
||
145 | */ |
||
146 | public function getOutput($vout) |
||
153 | |||
154 | /** |
||
155 | * @return bool |
||
156 | 2368 | */ |
|
157 | public function hasWitness() |
||
167 | |||
168 | /** |
||
169 | * @return ScriptWitnessInterface[] |
||
170 | */ |
||
171 | public function getWitnesses() |
||
175 | |||
176 | /** |
||
177 | * @param int $index |
||
178 | * @return ScriptWitnessInterface |
||
179 | */ |
||
180 | public function getWitness($index) |
||
187 | |||
188 | /** |
||
189 | * @param int $vout |
||
190 | 50 | * @return OutPointInterface |
|
191 | */ |
||
192 | 50 | public function makeOutpoint($vout) |
|
197 | |||
198 | /** |
||
199 | * @param int $vout |
||
200 | * @return Utxo |
||
201 | */ |
||
202 | 2296 | public function makeUtxo($vout) |
|
206 | |||
207 | /** |
||
208 | * Get Lock Time |
||
209 | * |
||
210 | * @return int |
||
211 | */ |
||
212 | public function getLockTime() |
||
216 | |||
217 | /** |
||
218 | * @return int|string |
||
219 | */ |
||
220 | public function getValueOut() |
||
230 | 6 | ||
231 | /** |
||
232 | 6 | * @return bool |
|
233 | 6 | */ |
|
234 | 6 | public function isCoinbase() |
|
238 | 6 | ||
239 | /** |
||
240 | * @param TransactionInterface $tx |
||
241 | * @return bool |
||
242 | */ |
||
243 | public function equals(TransactionInterface $tx) |
||
280 | 3 | ||
281 | /** |
||
282 | 6 | * @return BufferInterface |
|
283 | */ |
||
284 | public function getBuffer() |
||
288 | 6 | ||
289 | /** |
||
290 | * @return BufferInterface |
||
291 | */ |
||
292 | public function getBaseSerialization() |
||
296 | 2590 | ||
297 | /** |
||
298 | * @return BufferInterface |
||
299 | */ |
||
300 | public function getWitnessSerialization() |
||
308 | |||
309 | /** |
||
310 | * @return BufferInterface |
||
311 | */ |
||
312 | public function getWitnessBuffer() |
||
316 | } |
||
317 |