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 |
||
19 | class Transaction extends Serializable implements TransactionInterface |
||
20 | { |
||
21 | use FunctionAliasArrayAccess; |
||
22 | |||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | private $version; |
||
27 | |||
28 | /** |
||
29 | * @var TransactionInputCollection |
||
30 | */ |
||
31 | private $inputs; |
||
32 | |||
33 | /** |
||
34 | * @var TransactionOutputCollection |
||
35 | */ |
||
36 | private $outputs; |
||
37 | |||
38 | /** |
||
39 | * @var TransactionWitnessCollection |
||
40 | */ |
||
41 | private $witness; |
||
42 | |||
43 | /** |
||
44 | * @var int |
||
45 | */ |
||
46 | private $lockTime; |
||
47 | |||
48 | /** |
||
49 | * Transaction constructor. |
||
50 | * |
||
51 | * @param int $nVersion |
||
52 | * @param TransactionInputCollection|null $inputs |
||
53 | * @param TransactionOutputCollection|null $outputs |
||
54 | * @param TransactionWitnessCollection|null $witness |
||
55 | * @param int $nLockTime |
||
56 | */ |
||
57 | 636 | public function __construct( |
|
84 | |||
85 | /** |
||
86 | * @return Transaction |
||
87 | */ |
||
88 | 63 | public function __clone() |
|
93 | |||
94 | /** |
||
95 | * @return BufferInterface |
||
96 | */ |
||
97 | 60 | public function getTxHash() |
|
101 | |||
102 | /** |
||
103 | * @return BufferInterface |
||
104 | */ |
||
105 | 60 | public function getTxId() |
|
109 | |||
110 | /** |
||
111 | * @return BufferInterface |
||
112 | */ |
||
113 | public function getWitnessTxId() |
||
117 | |||
118 | /** |
||
119 | * @return int |
||
120 | */ |
||
121 | 141 | public function getVersion() |
|
125 | |||
126 | /** |
||
127 | * Get the array of inputs in the transaction |
||
128 | * |
||
129 | * @return TransactionInputCollection |
||
130 | */ |
||
131 | 486 | public function getInputs() |
|
135 | |||
136 | /** |
||
137 | * @param int $index |
||
138 | * @return TransactionInputInterface |
||
139 | */ |
||
140 | 57 | public function getInput($index) |
|
144 | |||
145 | /** |
||
146 | * Get Outputs |
||
147 | * |
||
148 | * @return TransactionOutputCollection |
||
149 | */ |
||
150 | 159 | public function getOutputs() |
|
154 | |||
155 | /** |
||
156 | * @param int $vout |
||
157 | * @return TransactionOutputInterface |
||
158 | */ |
||
159 | 51 | public function getOutput($vout) |
|
163 | |||
164 | /** |
||
165 | * @return TransactionWitnessCollection |
||
166 | */ |
||
167 | 54 | public function getWitnesses() |
|
171 | |||
172 | /** |
||
173 | * @return ScriptWitnessInterface |
||
174 | */ |
||
175 | public function getWitness($index) |
||
179 | |||
180 | /** |
||
181 | * @param int $vout |
||
182 | * @return OutPointInterface |
||
183 | */ |
||
184 | 12 | public function makeOutpoint($vout) |
|
189 | |||
190 | /** |
||
191 | * @param int $vout |
||
192 | * @return Utxo |
||
193 | */ |
||
194 | public function makeUtxo($vout) |
||
198 | |||
199 | /** |
||
200 | * Get Lock Time |
||
201 | * |
||
202 | * @return int |
||
203 | */ |
||
204 | 144 | public function getLockTime() |
|
208 | |||
209 | /** |
||
210 | * @return Hasher |
||
211 | */ |
||
212 | public function getSignatureHash() |
||
216 | |||
217 | /** |
||
218 | * @return int|string |
||
219 | */ |
||
220 | 3 | public function getValueOut() |
|
230 | |||
231 | /** |
||
232 | * @return bool |
||
233 | */ |
||
234 | 3 | public function isCoinbase() |
|
238 | |||
239 | /** |
||
240 | * @param TransactionInterface $tx |
||
241 | * @return bool |
||
242 | */ |
||
243 | 3 | public function equals(TransactionInterface $tx) |
|
277 | |||
278 | /** |
||
279 | * @return Validator |
||
280 | */ |
||
281 | 27 | public function validator() |
|
285 | |||
286 | /** |
||
287 | * @return BufferInterface |
||
288 | */ |
||
289 | 105 | public function getBuffer() |
|
293 | |||
294 | /** |
||
295 | * @return BufferInterface |
||
296 | */ |
||
297 | 27 | public function getWitnessBuffer() |
|
301 | } |
||
302 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: