Complex classes like FullyQualifiedScript 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 FullyQualifiedScript, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class FullyQualifiedScript |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var OutputData |
||
| 22 | */ |
||
| 23 | private $spkData; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var OutputData|null |
||
| 27 | */ |
||
| 28 | private $rsData; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var OutputData|null |
||
| 32 | */ |
||
| 33 | private $wsData; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var OutputData |
||
| 37 | */ |
||
| 38 | private $signData; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var int |
||
| 42 | */ |
||
| 43 | private $sigVersion; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * This is responsible for checking that the script-hash |
||
| 47 | * commitments between scripts were satisfied, and determines |
||
| 48 | * the sigVersion. |
||
| 49 | * |
||
| 50 | * It rejects superfluous redeem & witness scripts, and refuses |
||
| 51 | * to construct unless all necessary scripts are provided. |
||
| 52 | * |
||
| 53 | * @param OutputData $spkData |
||
| 54 | * @param OutputData|null $rsData |
||
| 55 | * @param OutputData|null $wsData |
||
| 56 | */ |
||
| 57 | public function __construct( |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Checks $chunks (a decompiled scriptSig) for it's last element, |
||
| 104 | * or defers to SignData. If both are provided, it checks the |
||
| 105 | * value obtained from $chunks against SignData. |
||
| 106 | * |
||
| 107 | * @param BufferInterface[] $chunks |
||
| 108 | * @param SignData $signData |
||
| 109 | * @return P2shScript |
||
| 110 | */ |
||
| 111 | public static function findRedeemScript(array $chunks, SignData $signData) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Checks the witness for it's last element, or whatever |
||
| 136 | * the SignData happens to have. If SignData has a WS, |
||
| 137 | * it will ensure that if chunks has a script, it matches WS. |
||
| 138 | * @param ScriptWitnessInterface $witness |
||
| 139 | * @param SignData $signData |
||
| 140 | * @return Script|ScriptInterface|WitnessScript |
||
| 141 | */ |
||
| 142 | public static function findWitnessScript(ScriptWitnessInterface $witness, SignData $signData) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * This function attempts to produce a FQS from |
||
| 167 | * raw scripts and witnesses. High level checking |
||
| 168 | * of script types is done to determine what we need |
||
| 169 | * from all this, before initializing the constructor |
||
| 170 | * for final validation. |
||
| 171 | * |
||
| 172 | * @param ScriptInterface $scriptPubKey |
||
| 173 | * @param ScriptInterface|null $scriptSig |
||
| 174 | * @param ScriptWitnessInterface|null $witness |
||
| 175 | * @param SignData|null $signData |
||
| 176 | * @param OutputClassifier|null $classifier |
||
| 177 | * @return FullyQualifiedScript |
||
| 178 | */ |
||
| 179 | public static function fromTxData( |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Was the FQS's scriptPubKey P2SH? |
||
| 213 | * @return bool |
||
| 214 | */ |
||
| 215 | public function isP2SH() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Was the FQS's scriptPubKey, or redeemScript, P2WSH? |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public function isP2WSH() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Returns the scriptPubKey. |
||
| 231 | * @return OutputData |
||
| 232 | */ |
||
| 233 | public function scriptPubKey() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Returns the sign script we qualified from |
||
| 240 | * the spk/rs/ws. Essentially this is the script |
||
| 241 | * that actually locks the coins (the CScript |
||
| 242 | * passed into EvalScript in interpreter.cpp) |
||
| 243 | * |
||
| 244 | * @return OutputData |
||
| 245 | */ |
||
| 246 | public function signScript() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Returns the signature hashing algorithm version. |
||
| 253 | * Defaults to V0, unless script was segwit. |
||
| 254 | * @return int |
||
| 255 | */ |
||
| 256 | public function sigVersion() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Returns the redeemScript, if we had one. |
||
| 263 | * Throws an exception otherwise. |
||
| 264 | * @return OutputData |
||
| 265 | * @throws \RuntimeException |
||
| 266 | */ |
||
| 267 | public function redeemScript() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Returns the witnessScript, if we had one. |
||
| 278 | * Throws an exception otherwise. |
||
| 279 | * @return OutputData |
||
| 280 | * @throws \RuntimeException |
||
| 281 | */ |
||
| 282 | public function witnessScript() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Encodes the stack (the stack passed as an |
||
| 293 | * argument to EvalScript in interpreter.cpp) |
||
| 294 | * into a scriptSig and witness structure. These |
||
| 295 | * are suitable for directly encoding in a transaction. |
||
| 296 | * @param Stack $stack |
||
| 297 | * @return SigValues |
||
| 298 | */ |
||
| 299 | public function encodeStack(Stack $stack) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param ScriptInterface $scriptSig |
||
| 332 | * @param ScriptWitnessInterface $witness |
||
| 333 | * @return Stack |
||
| 334 | */ |
||
| 335 | public function extractStack(ScriptInterface $scriptSig, ScriptWitnessInterface $witness) |
||
| 363 | } |
||
| 364 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: