Complex classes like Checksig 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 Checksig, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Checksig |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | private $scriptType; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var bool |
||
| 25 | */ |
||
| 26 | private $required = true; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var PayToPubkeyHash|PayToPubkey|Multisig |
||
| 30 | */ |
||
| 31 | private $info; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var int |
||
| 35 | */ |
||
| 36 | protected $requiredSigs; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | protected $keyCount; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var TransactionSignatureInterface[] |
||
| 45 | */ |
||
| 46 | protected $signatures = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var PublicKeyInterface[]|null[] |
||
| 50 | */ |
||
| 51 | protected $publicKeys = []; |
||
| 52 | /** |
||
| 53 | * Checksig constructor. |
||
| 54 | * @param Multisig|PayToPubkeyHash|PayToPubkey $info |
||
| 55 | */ |
||
| 56 | 136 | public function __construct($info) |
|
| 57 | { |
||
| 58 | 136 | if (!is_object($info)) { |
|
| 59 | throw new \RuntimeException("First value to checksig must be an object"); |
||
| 60 | } |
||
| 61 | |||
| 62 | 136 | $infoClass = get_class($info); |
|
| 63 | switch ($infoClass) { |
||
| 64 | 136 | case PayToPubkey::class: |
|
| 65 | /** @var PayToPubkey $info */ |
||
| 66 | 50 | $this->scriptType = $info->getType(); |
|
| 67 | 50 | $this->requiredSigs = $info->getRequiredSigCount(); |
|
| 68 | 50 | $this->keyCount = 1; |
|
| 69 | 50 | break; |
|
| 70 | 102 | case PayToPubkeyHash::class: |
|
| 71 | /** @var PayToPubkeyHash $info */ |
||
| 72 | 42 | $this->scriptType = ScriptType::P2PKH; |
|
| 73 | 42 | $this->requiredSigs = $info->getRequiredSigCount(); |
|
| 74 | 42 | $this->keyCount = 1; |
|
| 75 | 42 | break; |
|
| 76 | 64 | case Multisig::class: |
|
| 77 | /** @var Multisig $info */ |
||
| 78 | 64 | $this->scriptType = ScriptType::MULTISIG; |
|
| 79 | 64 | $this->requiredSigs = $info->getRequiredSigCount(); |
|
| 80 | 64 | $this->keyCount = $info->getKeyCount(); |
|
| 81 | 64 | break; |
|
| 82 | default: |
||
| 83 | throw new \RuntimeException("Unsupported class passed to Checksig"); |
||
| 84 | } |
||
| 85 | |||
| 86 | 136 | $this->info = $info; |
|
| 87 | 136 | } |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Mark this Checksig operation as not required. Will use OP_0 |
||
| 91 | * in place of all values (satisfying MINIMALDATA / MINIMALIF) |
||
| 92 | * |
||
| 93 | * @param bool $setting |
||
| 94 | * @return $this |
||
| 95 | */ |
||
| 96 | 4 | public function setRequired($setting) |
|
| 97 | { |
||
| 98 | 4 | if (!is_bool($setting)) { |
|
| 99 | throw new \RuntimeException("Invalid input to setRequired"); |
||
| 100 | } |
||
| 101 | 4 | $this->required = $setting; |
|
| 102 | 4 | return $this; |
|
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Returns whether this opcodes successful completion is |
||
| 107 | * necessary for the overall successful operation of the |
||
| 108 | * script |
||
| 109 | * |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | 116 | public function isRequired() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Returns the underlying script info class |
||
| 119 | * |
||
| 120 | * @return Multisig|PayToPubkey|PayToPubkeyHash |
||
| 121 | */ |
||
| 122 | 84 | public function getInfo() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Return the script type |
||
| 129 | * NB: Checksig overloads the various templates, returning 'multisig' |
||
| 130 | * even if the opcode was multisigverify. Check the getInfo() result, |
||
| 131 | * or isVerify() result, if this is important. |
||
| 132 | * |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | 136 | public function getType() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @return array|BufferInterface|BufferInterface[] |
||
| 142 | */ |
||
| 143 | 82 | public function getSolution() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @return int |
||
| 156 | */ |
||
| 157 | 120 | public function getRequiredSigs() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | 122 | public function isFullySigned() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @param int $idx |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | 118 | public function hasSignature($idx) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @param int $idx |
||
| 189 | * @param TransactionSignatureInterface $signature |
||
| 190 | * @return $this |
||
| 191 | */ |
||
| 192 | 116 | public function setSignature($idx, TransactionSignatureInterface $signature) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @param int $idx |
||
| 204 | * @return TransactionSignatureInterface|null |
||
| 205 | */ |
||
| 206 | 116 | public function getSignature($idx) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | 116 | public function getSignatures() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @param int $idx |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | 68 | public function hasKey($idx) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @param int $idx |
||
| 234 | * @return PublicKeyInterface|null |
||
| 235 | */ |
||
| 236 | 68 | public function getKey($idx) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @param $idx |
||
| 247 | * @param PublicKeyInterface|null $key |
||
| 248 | * @return $this |
||
| 249 | */ |
||
| 250 | 128 | public function setKey($idx, $key) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * @return PublicKeyInterface[] |
||
| 262 | */ |
||
| 263 | 118 | public function getKeys() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | 118 | public function isVerify() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @param TransactionSignatureSerializer $txSigSerializer |
||
| 278 | * @param PublicKeySerializerInterface $pubKeySerializer |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | 116 | public function serialize(TransactionSignatureSerializer $txSigSerializer, PublicKeySerializerInterface $pubKeySerializer) |
|
| 321 | } |
||
| 322 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: