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 | 5144 | public function __construct( |
|
| 51 | $nVersion = TransactionInterface::DEFAULT_VERSION, |
||
| 52 | array $vin = [], |
||
| 53 | array $vout = [], |
||
| 54 | array $vwit = [], |
||
| 55 | $nLockTime = 0 |
||
| 56 | ) { |
||
| 57 | 5144 | if ($nVersion > TransactionInterface::MAX_VERSION) { |
|
| 58 | 6 | throw new \InvalidArgumentException('Version must be less than ' . TransactionInterface::MAX_VERSION); |
|
| 59 | } |
||
| 60 | |||
| 61 | 5138 | 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 | 5132 | $this->version = $nVersion; |
|
| 66 | 5132 | $this->lockTime = $nLockTime; |
|
| 67 | |||
| 68 | $this->inputs = array_map(function (TransactionInputInterface $input) { |
||
| 69 | 4928 | return $input; |
|
| 70 | 5132 | }, $vin); |
|
| 71 | $this->outputs = array_map(function (TransactionOutputInterface $output) { |
||
| 72 | 4940 | return $output; |
|
| 73 | 5132 | }, $vout); |
|
| 74 | 5132 | $this->witness = array_map(function (ScriptWitnessInterface $scriptWitness) { |
|
| 75 | 4616 | return $scriptWitness; |
|
| 76 | 5132 | }, $vwit); |
|
| 77 | 5132 | } |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @return Transaction |
||
| 81 | */ |
||
| 82 | 452 | public function __clone() |
|
| 83 | { |
||
| 84 | //$this->inputs = clone $this->inputs; |
||
| 85 | //$this->outputs = clone $this->outputs; |
||
| 86 | 452 | } |
|
| 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 | 4862 | public function getVersion() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Get the array of inputs in the transaction |
||
| 122 | * |
||
| 123 | * @return TransactionInputInterface[] |
||
| 124 | */ |
||
| 125 | 4904 | public function getInputs() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @param int $index |
||
| 132 | * @return TransactionInputInterface |
||
| 133 | */ |
||
| 134 | 386 | public function getInput($index) |
|
| 135 | { |
||
| 136 | 386 | if (!isset($this->inputs[$index])) { |
|
| 137 | 6 | throw new \RuntimeException('No input at this index'); |
|
| 138 | } |
||
| 139 | 380 | return $this->inputs[$index]; |
|
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get Outputs |
||
| 144 | * |
||
| 145 | * @return TransactionOutputInterface[] |
||
| 146 | */ |
||
| 147 | 4886 | 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 | 464 | public function getWitnesses() |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @param int $index |
||
| 188 | * @return ScriptWitnessInterface |
||
| 189 | */ |
||
| 190 | 81 | public function getWitness($index) |
|
| 191 | { |
||
| 192 | 81 | if (!isset($this->witness[$index])) { |
|
| 193 | throw new \RuntimeException('No witness at this index'); |
||
| 194 | } |
||
| 195 | 81 | return $this->witness[$index]; |
|
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param int $vout |
||
| 200 | * @return OutPointInterface |
||
| 201 | */ |
||
| 202 | 4412 | public function makeOutpoint($vout) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * @param int $vout |
||
| 210 | * @return Utxo |
||
| 211 | */ |
||
| 212 | public function makeUtxo($vout) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Get Lock Time |
||
| 219 | * |
||
| 220 | * @return int |
||
| 221 | */ |
||
| 222 | 4868 | public function getLockTime() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @return int|string |
||
| 229 | */ |
||
| 230 | 6 | public function getValueOut() |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | 6 | public function isCoinbase() |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @param TransactionInterface $tx |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | 6 | public function equals(TransactionInterface $tx) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * @return BufferInterface |
||
| 293 | */ |
||
| 294 | 4742 | public function getBuffer() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * @return BufferInterface |
||
| 301 | */ |
||
| 302 | 102 | public function getWitnessBuffer() |
|
| 306 | } |
||
| 307 |
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.