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 | 118 | public function __construct($info) |
|
| 60 | { |
||
| 61 | 118 | if (!is_object($info)) { |
|
| 62 | throw new \RuntimeException("First value to checksig must be an object"); |
||
| 63 | } |
||
| 64 | |||
| 65 | 118 | $infoClass = get_class($info); |
|
| 66 | 118 | switch ($infoClass) { |
|
| 67 | case PayToPubkey::class: |
||
| 68 | /** @var PayToPubkey $info */ |
||
| 69 | 47 | $this->scriptType = $info->getType(); |
|
| 70 | 47 | $this->requiredSigs = $info->getRequiredSigCount(); |
|
| 71 | 47 | $this->keyCount = 1; |
|
| 72 | 47 | break; |
|
| 73 | case PayToPubkeyHash::class: |
||
| 74 | /** @var PayToPubkeyHash $info */ |
||
| 75 | 44 | $this->scriptType = ScriptType::P2PKH; |
|
| 76 | 44 | $this->requiredSigs = $info->getRequiredSigCount(); |
|
| 77 | 44 | $this->keyCount = 1; |
|
| 78 | 44 | break; |
|
| 79 | case Multisig::class: |
||
| 80 | /** @var Multisig $info */ |
||
| 81 | 38 | $this->scriptType = ScriptType::MULTISIG; |
|
| 82 | 38 | $this->requiredSigs = $info->getRequiredSigCount(); |
|
| 83 | 38 | $this->keyCount = $info->getKeyCount(); |
|
| 84 | 38 | break; |
|
| 85 | default: |
||
| 86 | throw new \RuntimeException("Unsupported class passed to Checksig"); |
||
| 87 | } |
||
| 88 | |||
| 89 | 118 | $this->info = $info; |
|
| 90 | 118 | } |
|
| 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 | 2 | public function setRequired(bool $setting) |
|
| 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 | 95 | public function isRequired(): bool |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Returns the underlying script info class |
||
| 119 | * |
||
| 120 | * @return Multisig|PayToPubkey|PayToPubkeyHash |
||
| 121 | */ |
||
| 122 | 48 | 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 | 108 | public function getType(): string |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @return array|BufferInterface|BufferInterface[] |
||
| 142 | */ |
||
| 143 | 75 | public function getSolution() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @return int |
||
| 156 | */ |
||
| 157 | 86 | public function getRequiredSigs(): int |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | 101 | public function isFullySigned(): bool |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @param int $idx |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | 97 | public function hasSignature(int $idx): bool |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @param int $idx |
||
| 189 | * @param TransactionSignatureInterface $signature |
||
| 190 | * @return $this |
||
| 191 | */ |
||
| 192 | 95 | public function setSignature(int $idx, TransactionSignatureInterface $signature) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @param int $idx |
||
| 204 | * @return TransactionSignatureInterface|null |
||
| 205 | */ |
||
| 206 | 95 | public function getSignature(int $idx) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | 95 | public function getSignatures(): array |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @param int $idx |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | 56 | public function hasKey(int $idx): bool |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @param int $idx |
||
| 234 | * @return PublicKeyInterface|null |
||
| 235 | */ |
||
| 236 | 56 | public function getKey(int $idx) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @param $idx |
||
| 247 | * @param PublicKeyInterface|null $key |
||
| 248 | * @return $this |
||
| 249 | */ |
||
| 250 | 103 | public function setKey(int $idx, PublicKeyInterface $key = null) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * @return PublicKeyInterface[] |
||
| 262 | */ |
||
| 263 | 85 | public function getKeys(): array |
|
| 267 | |||
| 268 | /** |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | 85 | public function isVerify(): bool |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @param TransactionSignatureSerializer $txSigSerializer |
||
| 278 | * @param PublicKeySerializerInterface $pubKeySerializer |
||
| 279 | * @return BufferInterface[] |
||
| 280 | */ |
||
| 281 | 95 | 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: