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 |
||
| 14 | class Transaction extends Serializable implements TransactionInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var int |
||
| 18 | */ |
||
| 19 | private $version; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var TransactionInputInterface[] |
||
| 23 | */ |
||
| 24 | private $inputs; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var TransactionOutputInterface[] |
||
| 28 | */ |
||
| 29 | private $outputs; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var ScriptWitnessInterface[] |
||
| 33 | */ |
||
| 34 | private $witness; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | private $lockTime; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Transaction constructor. |
||
| 43 | * |
||
| 44 | * @param int $nVersion |
||
| 45 | * @param TransactionInputInterface[] $vin |
||
| 46 | * @param TransactionOutputInterface[] $vout |
||
| 47 | * @param ScriptWitnessInterface[] $vwit |
||
| 48 | * @param int $nLockTime |
||
| 49 | */ |
||
| 50 | 5000 | public function __construct( |
|
| 51 | $nVersion = TransactionInterface::DEFAULT_VERSION, |
||
| 52 | array $vin = [], |
||
| 53 | array $vout = [], |
||
| 54 | array $vwit = [], |
||
| 55 | $nLockTime = 0 |
||
| 56 | ) { |
||
| 57 | 5000 | if ($nVersion > TransactionInterface::MAX_VERSION) { |
|
| 58 | 6 | throw new \InvalidArgumentException('Version must be less than ' . TransactionInterface::MAX_VERSION); |
|
| 59 | } |
||
| 60 | |||
| 61 | 4994 | if ($nLockTime < 0 || $nLockTime > TransactionInterface::MAX_LOCKTIME) { |
|
| 62 | 6 | throw new \InvalidArgumentException('Locktime must be positive and less than ' . TransactionInterface::MAX_LOCKTIME); |
|
| 63 | } |
||
| 64 | |||
| 65 | 4988 | $this->version = $nVersion; |
|
| 66 | 4988 | $this->lockTime = $nLockTime; |
|
| 67 | |||
| 68 | $this->inputs = array_map(function (TransactionInputInterface $input) { |
||
| 69 | 4784 | return $input; |
|
| 70 | 4988 | }, $vin); |
|
| 71 | $this->outputs = array_map(function (TransactionOutputInterface $output) { |
||
| 72 | 4808 | return $output; |
|
| 73 | 4988 | }, $vout); |
|
| 74 | 4988 | $this->witness = array_map(function (ScriptWitnessInterface $scriptWitness) { |
|
| 75 | 4472 | return $scriptWitness; |
|
| 76 | 4988 | }, $vwit); |
|
| 77 | 4988 | } |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @return Transaction |
||
| 81 | */ |
||
| 82 | 308 | public function __clone() |
|
| 83 | { |
||
| 84 | //$this->inputs = clone $this->inputs; |
||
| 85 | //$this->outputs = clone $this->outputs; |
||
| 86 | 308 | } |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @return BufferInterface |
||
| 90 | */ |
||
| 91 | 4508 | public function getTxHash() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @return BufferInterface |
||
| 98 | */ |
||
| 99 | 4508 | public function getTxId() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @return BufferInterface |
||
| 106 | */ |
||
| 107 | public function getWitnessTxId() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return int |
||
| 114 | */ |
||
| 115 | 4718 | public function getVersion() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Get the array of inputs in the transaction |
||
| 122 | * |
||
| 123 | * @return TransactionInputInterface[] |
||
| 124 | */ |
||
| 125 | 4700 | public function getInputs() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @param int $index |
||
| 132 | * @return TransactionInputInterface |
||
| 133 | */ |
||
| 134 | 218 | public function getInput($index) |
|
| 135 | { |
||
| 136 | 218 | if (!isset($this->inputs[$index])) { |
|
| 137 | 6 | throw new \RuntimeException('No input at this index'); |
|
| 138 | } |
||
| 139 | 212 | return $this->inputs[$index]; |
|
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get Outputs |
||
| 144 | * |
||
| 145 | * @return TransactionOutputInterface[] |
||
| 146 | */ |
||
| 147 | 4742 | public function getOutputs() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @param int $vout |
||
| 154 | * @return TransactionOutputInterface |
||
| 155 | */ |
||
| 156 | 4502 | public function getOutput($vout) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | public function hasWitness() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return ScriptWitnessInterface[] |
||
| 180 | */ |
||
| 181 | 296 | public function getWitnesses() |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @return ScriptWitnessInterface |
||
| 188 | */ |
||
| 189 | public function getWitness($index) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param int $vout |
||
| 199 | * @return OutPointInterface |
||
| 200 | */ |
||
| 201 | 4412 | public function makeOutpoint($vout) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @param int $vout |
||
| 209 | * @return Utxo |
||
| 210 | */ |
||
| 211 | public function makeUtxo($vout) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Get Lock Time |
||
| 218 | * |
||
| 219 | * @return int |
||
| 220 | */ |
||
| 221 | 4724 | public function getLockTime() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @return int|string |
||
| 228 | */ |
||
| 229 | 6 | public function getValueOut() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | 6 | public function isCoinbase() |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @param TransactionInterface $tx |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | 6 | public function equals(TransactionInterface $tx) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @return Validator |
||
| 289 | */ |
||
| 290 | 54 | public function validator() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @return BufferInterface |
||
| 297 | */ |
||
| 298 | 4613 | public function getBuffer() |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @return BufferInterface |
||
| 305 | */ |
||
| 306 | 54 | public function getWitnessBuffer() |
|
| 310 | } |
||
| 311 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.