Complex classes like OutputClassifier 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 OutputClassifier, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class OutputClassifier |
||
| 12 | { |
||
| 13 | const PAYTOPUBKEY = 'pubkey'; |
||
| 14 | const PAYTOPUBKEYHASH = 'pubkeyhash'; |
||
| 15 | const PAYTOSCRIPTHASH = 'scripthash'; |
||
| 16 | const WITNESS_V0_KEYHASH = 'witness_v0_keyhash'; |
||
| 17 | const WITNESS_V0_SCRIPTHASH = 'witness_v0_scripthash'; |
||
| 18 | const MULTISIG = 'multisig'; |
||
| 19 | const NULLDATA = 'nulldata'; |
||
| 20 | const UNKNOWN = 'unknown'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param Operation[] $decoded |
||
| 24 | * @return false|BufferInterface |
||
| 25 | */ |
||
| 26 | 222 | private function decodeP2PK(array $decoded) |
|
| 42 | |||
| 43 | /** |
||
| 44 | * @param ScriptInterface $script |
||
| 45 | * @return bool |
||
| 46 | */ |
||
| 47 | 50 | public function isPayToPublicKey(ScriptInterface $script) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * @param Operation[] $decoded |
||
| 60 | * @return BufferInterface|false |
||
| 61 | */ |
||
| 62 | 186 | private function decodeP2PKH(array $decoded) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @param ScriptInterface $script |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | 36 | public function isPayToPublicKeyHash(ScriptInterface $script) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param array $decoded |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | 2069 | private function decodeP2SH(array $decoded) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @param ScriptInterface $script |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | 2003 | public function isPayToScriptHash(ScriptInterface $script) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @param Operation[] $decoded |
||
| 152 | * @return bool|BufferInterface[] |
||
| 153 | */ |
||
| 154 | 162 | private function decodeMultisig(array $decoded) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @param ScriptInterface $script |
||
| 189 | * @return bool |
||
| 190 | */ |
||
| 191 | 1078 | public function isMultisig(ScriptInterface $script) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @param ScriptInterface $script |
||
| 204 | * @param Operation[] $decoded |
||
| 205 | * @return false|BufferInterface |
||
| 206 | */ |
||
| 207 | 96 | private function decodeWitnessNoLimit(ScriptInterface $script, array $decoded) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @param ScriptInterface $script |
||
| 232 | * @param int $limit |
||
| 233 | * @param array $decoded |
||
| 234 | * @return BufferInterface|false |
||
| 235 | */ |
||
| 236 | 66 | private function decodeWithLimit(ScriptInterface $script, $limit, array $decoded) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @param ScriptInterface $script |
||
| 251 | * @param Operation[] $decoded |
||
| 252 | * @return BufferInterface|false |
||
| 253 | */ |
||
| 254 | 42 | private function decodeP2WKH(ScriptInterface $script, array $decoded) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * @param ScriptInterface $script |
||
| 261 | * @param Operation[] $decoded |
||
| 262 | * @return BufferInterface|false |
||
| 263 | */ |
||
| 264 | 66 | private function decodeP2WSH(ScriptInterface $script, array $decoded) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * @param ScriptInterface $script |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | 30 | public function isWitness(ScriptInterface $script) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @param Operation[] $decoded |
||
| 286 | * @return false|BufferInterface |
||
| 287 | */ |
||
| 288 | 24 | private function decodeNullData(array $decoded) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @param ScriptInterface $script |
||
| 303 | * @return bool |
||
| 304 | */ |
||
| 305 | 6 | public function isNullData(ScriptInterface $script) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * @param ScriptInterface $script |
||
| 317 | * @param mixed $solution |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | 204 | public function classify(ScriptInterface $script, &$solution = null) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * @param ScriptInterface $script |
||
| 354 | * @return OutputData |
||
| 355 | */ |
||
| 356 | 108 | public function decode(ScriptInterface $script) |
|
| 362 | } |
||
| 363 |