Complex classes like CheckerBase 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 CheckerBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class CheckerBase |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var EcAdapterInterface |
||
| 26 | */ |
||
| 27 | protected $adapter; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var TransactionInterface |
||
| 31 | */ |
||
| 32 | protected $transaction; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | protected $nInput; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | protected $amount; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $sigCache = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var TransactionSignatureSerializer |
||
| 51 | */ |
||
| 52 | private $sigSerializer; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var PublicKeySerializerInterface |
||
| 56 | */ |
||
| 57 | private $pubKeySerializer; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var int |
||
| 61 | */ |
||
| 62 | protected $sigHashOptionalBits = SigHash::ANYONECANPAY; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Checker constructor. |
||
| 66 | * @param EcAdapterInterface $ecAdapter |
||
| 67 | * @param TransactionInterface $transaction |
||
| 68 | * @param int $nInput |
||
| 69 | * @param int $amount |
||
| 70 | * @param TransactionSignatureSerializer|null $sigSerializer |
||
| 71 | * @param PublicKeySerializerInterface|null $pubKeySerializer |
||
| 72 | */ |
||
| 73 | 4755 | public function __construct(EcAdapterInterface $ecAdapter, TransactionInterface $transaction, $nInput, $amount, TransactionSignatureSerializer $sigSerializer = null, PublicKeySerializerInterface $pubKeySerializer = null) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * @param ScriptInterface $script |
||
| 85 | * @param int $hashType |
||
| 86 | * @param int $sigVersion |
||
| 87 | * @return BufferInterface |
||
| 88 | */ |
||
| 89 | abstract public function getSigHash(ScriptInterface $script, $hashType, $sigVersion); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param BufferInterface $signature |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | 164 | public function isValidSignatureEncoding(BufferInterface $signature) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param BufferInterface $signature |
||
| 109 | * @return bool |
||
| 110 | * @throws ScriptRuntimeException |
||
| 111 | * @throws \Exception |
||
| 112 | */ |
||
| 113 | 7 | public function isLowDerSignature(BufferInterface $signature) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @param int $hashType |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | 94 | public function isDefinedHashtype($hashType) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Determine whether the sighash byte appended to the signature encodes |
||
| 140 | * a valid sighash type. |
||
| 141 | * |
||
| 142 | * @param BufferInterface $signature |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | 44 | public function isDefinedHashtypeSignature(BufferInterface $signature) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @param BufferInterface $signature |
||
| 157 | * @param int $flags |
||
| 158 | * @return $this |
||
| 159 | * @throws \BitWasp\Bitcoin\Exceptions\ScriptRuntimeException |
||
| 160 | */ |
||
| 161 | 547 | public function checkSignatureEncoding(BufferInterface $signature, $flags) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @param BufferInterface $publicKey |
||
| 180 | * @param int $flags |
||
| 181 | * @return $this |
||
| 182 | * @throws \Exception |
||
| 183 | */ |
||
| 184 | 442 | public function checkPublicKeyEncoding(BufferInterface $publicKey, $flags) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @param ScriptInterface $script |
||
| 195 | * @param BufferInterface $sigBuf |
||
| 196 | * @param BufferInterface $keyBuf |
||
| 197 | * @param int $sigVersion |
||
| 198 | * @param int $flags |
||
| 199 | * @return bool |
||
| 200 | * @throws ScriptRuntimeException |
||
| 201 | */ |
||
| 202 | 540 | public function checkSig(ScriptInterface $script, BufferInterface $sigBuf, BufferInterface $keyBuf, $sigVersion, $flags) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @param \BitWasp\Bitcoin\Script\Interpreter\Number $scriptLockTime |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | public function checkLockTime(\BitWasp\Bitcoin\Script\Interpreter\Number $scriptLockTime) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param \BitWasp\Bitcoin\Script\Interpreter\Number $sequence |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | 8 | public function checkSequence(\BitWasp\Bitcoin\Script\Interpreter\Number $sequence) |
|
| 284 | } |
||
| 285 |