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|string |
||
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|string |
||
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 | 1284 | public function __construct( |
|
58 | $nVersion = TransactionInterface::DEFAULT_VERSION, |
||
59 | TransactionInputCollection $inputs = null, |
||
60 | TransactionOutputCollection $outputs = null, |
||
61 | TransactionWitnessCollection $witness = null, |
||
62 | $nLockTime = 0 |
||
63 | ) { |
||
64 | 1284 | $math = Bitcoin::getMath(); |
|
65 | 1284 | if ($math->cmp($nVersion, TransactionInterface::MAX_VERSION) > 0) { |
|
66 | 6 | throw new \InvalidArgumentException('Version must be less than ' . TransactionInterface::MAX_VERSION); |
|
67 | } |
||
68 | |||
69 | 1278 | if ($math->cmp($nLockTime, 0) < 0 || $math->cmp($nLockTime, TransactionInterface::MAX_LOCKTIME) > 0) { |
|
70 | 6 | throw new \InvalidArgumentException('Locktime must be positive and less than ' . TransactionInterface::MAX_LOCKTIME); |
|
71 | } |
||
72 | |||
73 | 1272 | $this->version = $nVersion; |
|
74 | 1272 | $this->inputs = $inputs ?: new TransactionInputCollection(); |
|
75 | 1272 | $this->outputs = $outputs ?: new TransactionOutputCollection(); |
|
76 | 1272 | $this->witness = $witness ?: new TransactionWitnessCollection(); |
|
77 | 1272 | $this->lockTime = $nLockTime; |
|
78 | |||
79 | 1272 | $this |
|
80 | 1272 | ->initFunctionAlias('version', 'getVersion') |
|
81 | 1272 | ->initFunctionAlias('inputs', 'getInputs') |
|
82 | 1272 | ->initFunctionAlias('outputs', 'getOutputs') |
|
83 | 1272 | ->initFunctionAlias('locktime', 'getLockTime'); |
|
84 | 1272 | } |
|
85 | |||
86 | /** |
||
87 | * @return Transaction |
||
88 | */ |
||
89 | 120 | public function __clone() |
|
94 | |||
95 | /** |
||
96 | * @return BufferInterface |
||
97 | */ |
||
98 | 132 | public function getTxHash() |
|
102 | |||
103 | /** |
||
104 | * @return BufferInterface |
||
105 | */ |
||
106 | 132 | public function getTxId() |
|
110 | |||
111 | /** |
||
112 | * @return \BitWasp\Buffertools\BufferInterface |
||
113 | */ |
||
114 | public function getWitnessTxId() |
||
118 | |||
119 | /** |
||
120 | * @return int|string |
||
121 | */ |
||
122 | 330 | public function getVersion() |
|
126 | |||
127 | /** |
||
128 | * Get the array of inputs in the transaction |
||
129 | * |
||
130 | * @return TransactionInputCollection |
||
131 | */ |
||
132 | 1026 | public function getInputs() |
|
136 | |||
137 | /** |
||
138 | * @param int $index |
||
139 | * @return TransactionInputInterface |
||
140 | */ |
||
141 | 108 | public function getInput($index) |
|
145 | |||
146 | /** |
||
147 | * Get Outputs |
||
148 | * |
||
149 | * @return TransactionOutputCollection |
||
150 | */ |
||
151 | 336 | public function getOutputs() |
|
155 | |||
156 | /** |
||
157 | * @param int $vout |
||
158 | * @return TransactionOutputInterface |
||
159 | */ |
||
160 | 102 | public function getOutput($vout) |
|
164 | |||
165 | /** |
||
166 | * @return TransactionWitnessCollection |
||
167 | */ |
||
168 | 102 | public function getWitnesses() |
|
172 | |||
173 | /** |
||
174 | * @return ScriptWitnessInterface |
||
175 | */ |
||
176 | public function getWitness($index) |
||
180 | |||
181 | /** |
||
182 | * @param int $vout |
||
183 | * @return OutPointInterface |
||
184 | */ |
||
185 | 24 | public function makeOutpoint($vout) |
|
190 | |||
191 | /** |
||
192 | * @param int $vout |
||
193 | * @return Utxo |
||
194 | */ |
||
195 | public function makeUtxo($vout) |
||
204 | |||
205 | /** |
||
206 | * Get Lock Time |
||
207 | * |
||
208 | * @return int|string |
||
209 | */ |
||
210 | 336 | public function getLockTime() |
|
214 | |||
215 | /** |
||
216 | * @return \BitWasp\Bitcoin\Transaction\SignatureHash\SigHash |
||
217 | */ |
||
218 | 6 | public function getSignatureHash() |
|
222 | |||
223 | /** |
||
224 | * @return int|string |
||
225 | */ |
||
226 | 6 | public function getValueOut() |
|
236 | |||
237 | /** |
||
238 | * @return bool |
||
239 | */ |
||
240 | 6 | public function isCoinbase() |
|
244 | |||
245 | /** |
||
246 | * @param TransactionInterface $tx |
||
247 | * @return bool |
||
248 | 54 | */ |
|
249 | public function equals(TransactionInterface $tx) |
||
283 | |||
284 | /** |
||
285 | * @return Validator |
||
286 | */ |
||
287 | public function validator() |
||
291 | |||
292 | /** |
||
293 | * @return BufferInterface |
||
294 | */ |
||
295 | public function getBuffer() |
||
299 | |||
300 | /** |
||
301 | * @return BufferInterface |
||
302 | */ |
||
303 | public function getWitnessBuffer() |
||
307 | } |
||
308 |
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: