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 |
||
| 14 | class OutputClassifier |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @deprecated |
||
| 18 | */ |
||
| 19 | const PAYTOPUBKEY = 'pubkey'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @deprecated |
||
| 23 | */ |
||
| 24 | const PAYTOPUBKEYHASH = 'pubkeyhash'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @deprecated |
||
| 28 | */ |
||
| 29 | const PAYTOSCRIPTHASH = 'scripthash'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @deprecated |
||
| 33 | */ |
||
| 34 | const WITNESS_V0_KEYHASH = 'witness_v0_keyhash'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @deprecated |
||
| 38 | */ |
||
| 39 | const WITNESS_V0_SCRIPTHASH = 'witness_v0_scripthash'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @deprecated |
||
| 43 | */ |
||
| 44 | const MULTISIG = 'multisig'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @deprecated |
||
| 48 | */ |
||
| 49 | const NULLDATA = 'nulldata'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @deprecated |
||
| 53 | */ |
||
| 54 | const UNKNOWN = 'nonstandard'; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @deprecated |
||
| 58 | */ |
||
| 59 | const NONSTANDARD = 'nonstandard'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @deprecated |
||
| 63 | */ |
||
| 64 | const P2PK = 'pubkey'; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @deprecated |
||
| 68 | */ |
||
| 69 | const P2PKH = 'pubkeyhash'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @deprecated |
||
| 73 | */ |
||
| 74 | const P2SH = 'scripthash'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @deprecated |
||
| 78 | */ |
||
| 79 | const P2WSH = 'witness_v0_scripthash'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @deprecated |
||
| 83 | */ |
||
| 84 | const P2WKH = 'witness_v0_keyhash'; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @deprecated |
||
| 88 | */ |
||
| 89 | const WITNESS_COINBASE_COMMITMENT = 'witness_coinbase_commitment'; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param Operation[] $decoded |
||
| 93 | * @return false|BufferInterface |
||
| 94 | */ |
||
| 95 | 206 | private function decodeP2PK(array $decoded) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @param ScriptInterface $script |
||
| 114 | * @return bool |
||
| 115 | */ |
||
| 116 | 20 | public function isPayToPublicKey(ScriptInterface $script) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @param Operation[] $decoded |
||
| 129 | * @return BufferInterface|false |
||
| 130 | */ |
||
| 131 | 188 | private function decodeP2PKH(array $decoded) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @param ScriptInterface $script |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | 16 | public function isPayToPublicKeyHash(ScriptInterface $script) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @param array $decoded |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | 1220 | private function decodeP2SH(array $decoded) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @param ScriptInterface $script |
||
| 206 | * @return bool |
||
| 207 | */ |
||
| 208 | 1156 | public function isPayToScriptHash(ScriptInterface $script) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @param Operation[] $decoded |
||
| 221 | * @return bool|BufferInterface[] |
||
| 222 | */ |
||
| 223 | 162 | private function decodeMultisig(array $decoded) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param ScriptInterface $script |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | 20 | public function isMultisig(ScriptInterface $script) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @param ScriptInterface $script |
||
| 273 | * @param Operation[] $decoded |
||
| 274 | * @return false|BufferInterface |
||
| 275 | */ |
||
| 276 | 16 | private function decodeWitnessNoLimit(ScriptInterface $script, array $decoded) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * @param ScriptInterface $script |
||
| 301 | * @param int $limit |
||
| 302 | * @param array $decoded |
||
| 303 | * @return BufferInterface|false |
||
| 304 | */ |
||
| 305 | private function decodeWithLimit(ScriptInterface $script, $limit, array $decoded) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param ScriptInterface $script |
||
| 320 | * @param Operation[] $decoded |
||
| 321 | * @return BufferInterface|false |
||
| 322 | */ |
||
| 323 | private function decodeP2WKH(ScriptInterface $script, array $decoded) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param ScriptInterface $script |
||
| 330 | * @param Operation[] $decoded |
||
| 331 | * @return BufferInterface|false |
||
| 332 | */ |
||
| 333 | private function decodeP2WSH(ScriptInterface $script, array $decoded) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param Operation[] $decoded |
||
| 340 | * @return BufferInterface|false |
||
| 341 | */ |
||
| 342 | 60 | private function decodeP2WKH2(array $decoded) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * @param Operation[] $decoded |
||
| 356 | * @return BufferInterface|false |
||
| 357 | */ |
||
| 358 | 100 | private function decodeP2WSH2(array $decoded) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * @param ScriptInterface $script |
||
| 372 | * @return bool |
||
| 373 | */ |
||
| 374 | 16 | public function isWitness(ScriptInterface $script) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * @param Operation[] $decoded |
||
| 387 | * @return false|BufferInterface |
||
| 388 | */ |
||
| 389 | 48 | private function decodeNullData(array $decoded) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * @param ScriptInterface $script |
||
| 404 | * @return bool |
||
| 405 | */ |
||
| 406 | 2 | public function isNullData(ScriptInterface $script) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * @param array $decoded |
||
| 418 | * @return bool|BufferInterface |
||
| 419 | */ |
||
| 420 | 62 | private function decodeWitnessCoinbaseCommitment(array $decoded) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * @param ScriptInterface $script |
||
| 442 | * @return bool |
||
| 443 | */ |
||
| 444 | 14 | public function isWitnessCoinbaseCommitment(ScriptInterface $script) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * @param array $decoded |
||
| 456 | * @param null $solution |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | 200 | private function classifyDecoded(array $decoded, &$solution = null) |
|
| 491 | |||
| 492 | /** |
||
| 493 | * @param ScriptInterface $script |
||
| 494 | * @param mixed $solution |
||
| 495 | * @return string |
||
| 496 | */ |
||
| 497 | 200 | public function classify(ScriptInterface $script, &$solution = null) |
|
| 505 | |||
| 506 | /** |
||
| 507 | * @param ScriptInterface $script |
||
| 508 | * @return OutputData |
||
| 509 | */ |
||
| 510 | 180 | public function decode(ScriptInterface $script) |
|
| 516 | |||
| 517 | /** |
||
| 518 | * @param ScriptInterface $script |
||
| 519 | * @return OutputData[] |
||
| 520 | */ |
||
| 521 | public function decodeSequence(ScriptInterface $script, $allowNonstandard = false) |
||
| 558 | } |
||
| 559 |