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 |
||
| 18 | class Transaction extends Serializable implements TransactionInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var int |
||
| 22 | */ |
||
| 23 | private $version; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var TransactionInputCollection |
||
| 27 | */ |
||
| 28 | private $inputs; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var TransactionOutputCollection |
||
| 32 | */ |
||
| 33 | private $outputs; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var TransactionWitnessCollection |
||
| 37 | */ |
||
| 38 | private $witness; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var int |
||
| 42 | */ |
||
| 43 | private $lockTime; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Transaction constructor. |
||
| 47 | * |
||
| 48 | * @param int $nVersion |
||
| 49 | * @param TransactionInputCollection|null $inputs |
||
| 50 | * @param TransactionOutputCollection|null $outputs |
||
| 51 | * @param TransactionWitnessCollection|null $witness |
||
| 52 | * @param int $nLockTime |
||
| 53 | */ |
||
| 54 | 2485 | public function __construct( |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @return Transaction |
||
| 79 | */ |
||
| 80 | 157 | public function __clone() |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @return BufferInterface |
||
| 88 | */ |
||
| 89 | 2254 | public function getTxHash() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * @return BufferInterface |
||
| 96 | */ |
||
| 97 | 2254 | public function getTxId() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @return BufferInterface |
||
| 104 | */ |
||
| 105 | public function getWitnessTxId() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return int |
||
| 112 | */ |
||
| 113 | 2335 | public function getVersion() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Get the array of inputs in the transaction |
||
| 120 | * |
||
| 121 | * @return TransactionInputCollection |
||
| 122 | */ |
||
| 123 | 2335 | public function getInputs() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @param int $index |
||
| 130 | * @return TransactionInputInterface |
||
| 131 | */ |
||
| 132 | 85 | public function getInput($index) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Get Outputs |
||
| 139 | * |
||
| 140 | * @return TransactionOutputCollection |
||
| 141 | */ |
||
| 142 | 2353 | public function getOutputs() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @param int $vout |
||
| 149 | * @return TransactionOutputInterface |
||
| 150 | */ |
||
| 151 | 2245 | public function getOutput($vout) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @return TransactionWitnessCollection |
||
| 158 | */ |
||
| 159 | 148 | public function getWitnesses() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @return ScriptWitnessInterface |
||
| 166 | */ |
||
| 167 | public function getWitness($index) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param int $vout |
||
| 174 | * @return OutPointInterface |
||
| 175 | */ |
||
| 176 | 2206 | public function makeOutpoint($vout) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @param int $vout |
||
| 184 | * @return Utxo |
||
| 185 | */ |
||
| 186 | public function makeUtxo($vout) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get Lock Time |
||
| 193 | * |
||
| 194 | * @return int |
||
| 195 | */ |
||
| 196 | 2338 | public function getLockTime() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @return Hasher |
||
| 203 | */ |
||
| 204 | public function getSignatureHash() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @return int|string |
||
| 211 | */ |
||
| 212 | 3 | public function getValueOut() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @return bool |
||
| 225 | */ |
||
| 226 | 3 | public function isCoinbase() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @param TransactionInterface $tx |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | 3 | public function equals(TransactionInterface $tx) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @return Validator |
||
| 272 | */ |
||
| 273 | 27 | public function validator() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @return BufferInterface |
||
| 280 | */ |
||
| 281 | 2299 | public function getBuffer() |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @return BufferInterface |
||
| 288 | */ |
||
| 289 | 27 | public function getWitnessBuffer() |
|
| 293 | } |
||
| 294 |
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: