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 |
||
| 18 | class Checksig |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private $scriptType; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | private $required = true; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var PayToPubkeyHash|PayToPubkey|Multisig |
||
| 32 | */ |
||
| 33 | private $info; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | protected $requiredSigs; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var int |
||
| 42 | */ |
||
| 43 | protected $keyCount; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var TransactionSignatureInterface[] |
||
| 47 | */ |
||
| 48 | protected $signatures = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var PublicKeyInterface[]|null[] |
||
| 52 | */ |
||
| 53 | protected $publicKeys = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Checksig constructor. |
||
| 57 | * @param Multisig|PayToPubkeyHash|PayToPubkey $info |
||
| 58 | */ |
||
| 59 | 3 | public function __construct($info) |
|
| 60 | { |
||
| 61 | 3 | if (!is_object($info)) { |
|
| 62 | throw new \RuntimeException("First value to checksig must be an object"); |
||
| 63 | } |
||
| 64 | |||
| 65 | 3 | $infoClass = get_class($info); |
|
| 66 | switch ($infoClass) { |
||
| 67 | 3 | case PayToPubkey::class: |
|
| 68 | /** @var PayToPubkey $info */ |
||
| 69 | 1 | $this->scriptType = $info->getType(); |
|
| 70 | 1 | $this->requiredSigs = $info->getRequiredSigCount(); |
|
| 71 | 1 | $this->keyCount = 1; |
|
| 72 | 1 | break; |
|
| 73 | 2 | case PayToPubkeyHash::class: |
|
| 74 | /** @var PayToPubkeyHash $info */ |
||
| 75 | 1 | $this->scriptType = ScriptType::P2PKH; |
|
| 76 | 1 | $this->requiredSigs = $info->getRequiredSigCount(); |
|
| 77 | 1 | $this->keyCount = 1; |
|
| 78 | 1 | break; |
|
| 79 | 1 | case Multisig::class: |
|
| 80 | /** @var Multisig $info */ |
||
| 81 | 1 | $this->scriptType = ScriptType::MULTISIG; |
|
| 82 | 1 | $this->requiredSigs = $info->getRequiredSigCount(); |
|
| 83 | 1 | $this->keyCount = $info->getKeyCount(); |
|
| 84 | 1 | break; |
|
| 85 | default: |
||
| 86 | throw new \RuntimeException("Unsupported class passed to Checksig"); |
||
| 87 | } |
||
| 88 | |||
| 89 | 3 | $this->info = $info; |
|
| 90 | 3 | } |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Mark this Checksig operation as not required. Will use OP_0 |
||
| 94 | * in place of all values (satisfying MINIMALDATA / MINIMALIF) |
||
| 95 | * |
||
| 96 | * @param bool $setting |
||
| 97 | * @return $this |
||
| 98 | */ |
||
| 99 | public function setRequired(bool $setting) |
||
| 100 | { |
||
| 101 | $this->required = $setting; |
||
| 102 | 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 | public function isRequired(): bool |
||
| 113 | { |
||
| 114 | return $this->required; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Returns the underlying script info class |
||
| 119 | * |
||
| 120 | * @return Multisig|PayToPubkey|PayToPubkeyHash |
||
| 121 | */ |
||
| 122 | 1 | 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 | 3 | public function getType(): string |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @return array|BufferInterface|BufferInterface[] |
||
| 142 | */ |
||
| 143 | 1 | public function getSolution() |
|
| 144 | { |
||
| 145 | 1 | if ($this->info instanceof Multisig) { |
|
| 146 | return $this->info->getKeyBuffers(); |
||
| 147 | 1 | } else if ($this->info instanceof PayToPubkey) { |
|
| 148 | 1 | return $this->info->getKeyBuffer(); |
|
| 149 | } else { |
||
| 150 | return $this->info->getPubKeyHash(); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return int |
||
| 156 | */ |
||
| 157 | 1 | public function getRequiredSigs(): int |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | public function isFullySigned(): bool |
||
| 166 | { |
||
| 167 | if ($this->required) { |
||
| 168 | return $this->requiredSigs === count($this->signatures); |
||
| 169 | } else { |
||
| 170 | return true; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param int $idx |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | public function hasSignature(int $idx): bool |
||
| 179 | { |
||
| 180 | if ($idx > $this->requiredSigs) { |
||
| 181 | throw new \RuntimeException("Out of range signature queried"); |
||
| 182 | } |
||
| 183 | |||
| 184 | return array_key_exists($idx, $this->signatures); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param int $idx |
||
| 189 | * @param TransactionSignatureInterface $signature |
||
| 190 | * @return $this |
||
| 191 | */ |
||
| 192 | public function setSignature(int $idx, TransactionSignatureInterface $signature) |
||
| 193 | { |
||
| 194 | if ($idx < 0 || $idx > $this->keyCount) { |
||
| 195 | throw new \RuntimeException("Out of range signature for operation"); |
||
| 196 | } |
||
| 197 | |||
| 198 | $this->signatures[$idx] = $signature; |
||
| 199 | return $this; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param int $idx |
||
| 204 | * @return TransactionSignatureInterface|null |
||
| 205 | */ |
||
| 206 | public function getSignature(int $idx) |
||
| 207 | { |
||
| 208 | if (!$this->hasSignature($idx)) { |
||
| 209 | return null; |
||
| 210 | } |
||
| 211 | |||
| 212 | return $this->signatures[$idx]; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | public function getSignatures(): array |
||
| 219 | { |
||
| 220 | return $this->signatures; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param int $idx |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | public function hasKey(int $idx): bool |
||
| 228 | { |
||
| 229 | return array_key_exists($idx, $this->publicKeys); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param int $idx |
||
| 234 | * @return PublicKeyInterface|null |
||
| 235 | */ |
||
| 236 | public function getKey(int $idx) |
||
| 237 | { |
||
| 238 | if (!$this->hasKey($idx)) { |
||
| 239 | return null; |
||
| 240 | } |
||
| 241 | |||
| 242 | return $this->publicKeys[$idx]; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param $idx |
||
| 247 | * @param PublicKeyInterface|null $key |
||
| 248 | * @return $this |
||
| 249 | */ |
||
| 250 | 1 | public function setKey(int $idx, PublicKeyInterface $key = null) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * @return PublicKeyInterface[] |
||
| 262 | */ |
||
| 263 | public function getKeys(): array |
||
| 264 | { |
||
| 265 | return $this->publicKeys; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | public function isVerify(): bool |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param TransactionSignatureSerializer $txSigSerializer |
||
| 278 | * @param PublicKeySerializerInterface $pubKeySerializer |
||
| 279 | * @return BufferInterface[] |
||
| 280 | */ |
||
| 281 | public function serialize( |
||
| 323 | } |
||
| 324 |
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: