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 |
||
| 16 | class OutputClassifier |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @deprecated |
||
| 20 | */ |
||
| 21 | const PAYTOPUBKEY = 'pubkey'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @deprecated |
||
| 25 | */ |
||
| 26 | const PAYTOPUBKEYHASH = 'pubkeyhash'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @deprecated |
||
| 30 | */ |
||
| 31 | const PAYTOSCRIPTHASH = 'scripthash'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @deprecated |
||
| 35 | */ |
||
| 36 | const WITNESS_V0_KEYHASH = 'witness_v0_keyhash'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @deprecated |
||
| 40 | */ |
||
| 41 | const WITNESS_V0_SCRIPTHASH = 'witness_v0_scripthash'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @deprecated |
||
| 45 | */ |
||
| 46 | const MULTISIG = 'multisig'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @deprecated |
||
| 50 | */ |
||
| 51 | const NULLDATA = 'nulldata'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @deprecated |
||
| 55 | */ |
||
| 56 | const UNKNOWN = 'nonstandard'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @deprecated |
||
| 60 | */ |
||
| 61 | const NONSTANDARD = 'nonstandard'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @deprecated |
||
| 65 | */ |
||
| 66 | const P2PK = 'pubkey'; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @deprecated |
||
| 70 | */ |
||
| 71 | const P2PKH = 'pubkeyhash'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @deprecated |
||
| 75 | */ |
||
| 76 | const P2SH = 'scripthash'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @deprecated |
||
| 80 | */ |
||
| 81 | const P2WSH = 'witness_v0_scripthash'; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @deprecated |
||
| 85 | */ |
||
| 86 | const P2WKH = 'witness_v0_keyhash'; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @deprecated |
||
| 90 | */ |
||
| 91 | const WITNESS_COINBASE_COMMITMENT = 'witness_coinbase_commitment'; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param Operation[] $decoded |
||
| 95 | 134 | * @return false|BufferInterface |
|
| 96 | */ |
||
| 97 | 134 | private function decodeP2PK(array $decoded) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @param ScriptInterface $script |
||
| 116 | 10 | * @return bool |
|
| 117 | */ |
||
| 118 | public function isPayToPublicKey(ScriptInterface $script): bool |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param Operation[] $decoded |
||
| 131 | 125 | * @return BufferInterface|false |
|
| 132 | */ |
||
| 133 | 125 | private function decodeP2PKH(array $decoded) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @param ScriptInterface $script |
||
| 165 | 8 | * @return bool |
|
| 166 | */ |
||
| 167 | public function isPayToPublicKeyHash(ScriptInterface $script): bool |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param array $decoded |
||
| 180 | 1154 | * @return bool|BufferInterface |
|
| 181 | */ |
||
| 182 | 1154 | private function decodeP2SH(array $decoded) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * @param ScriptInterface $script |
||
| 208 | 1104 | * @return bool |
|
| 209 | */ |
||
| 210 | public function isPayToScriptHash(ScriptInterface $script): bool |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param Operation[] $decoded |
||
| 223 | 104 | * @return bool|BufferInterface[] |
|
| 224 | */ |
||
| 225 | 104 | private function decodeMultisig(array $decoded) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * @param ScriptInterface $script |
||
| 260 | 10 | * @return bool |
|
| 261 | */ |
||
| 262 | public function isMultisig(ScriptInterface $script): bool |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param ScriptInterface $script |
||
| 275 | * @param Operation[] $decoded |
||
| 276 | 7 | * @return false|BufferInterface |
|
| 277 | */ |
||
| 278 | 7 | private function decodeWitnessNoLimit(ScriptInterface $script, array $decoded) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @param Operation[] $decoded |
||
| 303 | * @return BufferInterface|false |
||
| 304 | */ |
||
| 305 | private function decodeP2WKH2(array $decoded) |
||
| 306 | { |
||
| 307 | if (count($decoded) === 2 |
||
| 308 | && $decoded[0]->getOp() === Opcodes::OP_0 |
||
| 309 | && $decoded[1]->isPush() |
||
| 310 | && $decoded[1]->getDataSize() === 20) { |
||
| 311 | return $decoded[1]->getData(); |
||
| 312 | } |
||
| 313 | |||
| 314 | return false; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param Operation[] $decoded |
||
| 319 | * @return BufferInterface|false |
||
| 320 | */ |
||
| 321 | private function decodeP2WSH2(array $decoded) |
||
| 322 | { |
||
| 323 | if (count($decoded) === 2 |
||
| 324 | && $decoded[0]->getOp() === Opcodes::OP_0 |
||
| 325 | && $decoded[1]->isPush() |
||
| 326 | && $decoded[1]->getDataSize() === 32) { |
||
| 327 | return $decoded[1]->getData(); |
||
| 328 | } |
||
| 329 | |||
| 330 | return false; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param ScriptInterface $script |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | public function isWitness(ScriptInterface $script): bool |
||
| 347 | 46 | ||
| 348 | 5 | /** |
|
| 349 | * @param Operation[] $decoded |
||
| 350 | * @return false|BufferInterface |
||
| 351 | 41 | */ |
|
| 352 | private function decodeNullData(array $decoded) |
||
| 364 | 36 | ||
| 365 | /** |
||
| 366 | * @param ScriptInterface $script |
||
| 367 | 46 | * @return bool |
|
| 368 | */ |
||
| 369 | public function isNullData(ScriptInterface $script): bool |
||
| 378 | 2 | ||
| 379 | /** |
||
| 380 | * @param array $decoded |
||
| 381 | * @return bool|BufferInterface |
||
| 382 | 2 | */ |
|
| 383 | private function decodeWitnessCoinbaseCommitment(array $decoded) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param ScriptInterface $script |
||
| 405 | * @return bool |
||
| 406 | 1 | */ |
|
| 407 | public function isWitnessCoinbaseCommitment(ScriptInterface $script): bool |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param array $decoded |
||
| 419 | * @param null $solution |
||
| 420 | 46 | * @return string |
|
| 421 | */ |
||
| 422 | 46 | private function classifyDecoded(array $decoded, &$solution = null): string |
|
| 454 | |||
| 455 | /** |
||
| 456 | * @param ScriptInterface $script |
||
| 457 | * @param mixed $solution |
||
| 458 | * @return string |
||
| 459 | 131 | */ |
|
| 460 | public function classify(ScriptInterface $script, &$solution = null): string |
||
| 468 | 33 | ||
| 469 | 99 | /** |
|
| 470 | 28 | * @param ScriptInterface $script |
|
| 471 | 28 | * @return OutputData |
|
| 472 | 90 | */ |
|
| 473 | 49 | public function decode(ScriptInterface $script): OutputData |
|
| 479 | 5 | ||
| 480 | 5 | /** |
|
| 481 | 41 | * @param ScriptInterface $script |
|
| 482 | 1 | * @param bool $allowNonstandard |
|
| 483 | 1 | * @return OutputData[] |
|
| 484 | 40 | */ |
|
| 485 | 1 | public function decodeSequence(ScriptInterface $script, bool $allowNonstandard = false): array |
|
| 522 | } |
||
| 523 |