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 | * @return false|BufferInterface |
||
96 | */ |
||
97 | 162 | private function decodeP2PK(array $decoded) |
|
113 | |||
114 | /** |
||
115 | * @param ScriptInterface $script |
||
116 | * @return bool |
||
117 | */ |
||
118 | 10 | public function isPayToPublicKey(ScriptInterface $script): bool |
|
128 | |||
129 | /** |
||
130 | * @param Operation[] $decoded |
||
131 | * @return BufferInterface|false |
||
132 | */ |
||
133 | 151 | private function decodeP2PKH(array $decoded) |
|
162 | |||
163 | /** |
||
164 | * @param ScriptInterface $script |
||
165 | * @return bool |
||
166 | */ |
||
167 | 8 | public function isPayToPublicKeyHash(ScriptInterface $script): bool |
|
177 | |||
178 | /** |
||
179 | * @param array $decoded |
||
180 | * @return bool|BufferInterface |
||
181 | */ |
||
182 | 2198 | private function decodeP2SH(array $decoded) |
|
205 | |||
206 | /** |
||
207 | * @param ScriptInterface $script |
||
208 | * @return bool |
||
209 | */ |
||
210 | 2098 | public function isPayToScriptHash(ScriptInterface $script): bool |
|
220 | |||
221 | /** |
||
222 | * @param Operation[] $decoded |
||
223 | * @return bool|BufferInterface[] |
||
224 | */ |
||
225 | 120 | private function decodeMultisig(array $decoded) |
|
257 | |||
258 | /** |
||
259 | * @param ScriptInterface $script |
||
260 | * @return bool |
||
261 | */ |
||
262 | 10 | public function isMultisig(ScriptInterface $script): bool |
|
272 | |||
273 | /** |
||
274 | * @param ScriptInterface $script |
||
275 | * @param Operation[] $decoded |
||
276 | * @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 | 50 | private function decodeP2WKH2(array $decoded) |
|
316 | |||
317 | /** |
||
318 | * @param Operation[] $decoded |
||
319 | * @return BufferInterface|false |
||
320 | */ |
||
321 | 76 | private function decodeP2WSH2(array $decoded) |
|
332 | |||
333 | /** |
||
334 | * @param ScriptInterface $script |
||
335 | * @return bool |
||
336 | */ |
||
337 | 8 | public function isWitness(ScriptInterface $script): bool |
|
347 | |||
348 | /** |
||
349 | * @param Operation[] $decoded |
||
350 | * @return false|BufferInterface |
||
351 | */ |
||
352 | 40 | private function decodeNullData(array $decoded) |
|
364 | |||
365 | /** |
||
366 | * @param ScriptInterface $script |
||
367 | * @return bool |
||
368 | */ |
||
369 | 1 | public function isNullData(ScriptInterface $script): bool |
|
378 | |||
379 | /** |
||
380 | * @param array $decoded |
||
381 | * @return bool|BufferInterface |
||
382 | */ |
||
383 | 46 | private function decodeWitnessCoinbaseCommitment(array $decoded) |
|
402 | |||
403 | /** |
||
404 | * @param ScriptInterface $script |
||
405 | * @return bool |
||
406 | */ |
||
407 | 7 | public function isWitnessCoinbaseCommitment(ScriptInterface $script): bool |
|
416 | |||
417 | /** |
||
418 | * @param array $decoded |
||
419 | * @param null $solution |
||
420 | * @return string |
||
421 | */ |
||
422 | 159 | private function classifyDecoded(array $decoded, &$solution = null): string |
|
454 | |||
455 | /** |
||
456 | * @param ScriptInterface $script |
||
457 | * @param mixed $solution |
||
458 | * @return string |
||
459 | */ |
||
460 | 159 | public function classify(ScriptInterface $script, &$solution = null): string |
|
468 | |||
469 | /** |
||
470 | * @param ScriptInterface $script |
||
471 | * @return OutputData |
||
472 | */ |
||
473 | 149 | public function decode(ScriptInterface $script): OutputData |
|
479 | |||
480 | /** |
||
481 | * @param ScriptInterface $script |
||
482 | * @param bool $allowNonstandard |
||
483 | * @return OutputData[] |
||
484 | */ |
||
485 | public function decodeSequence(ScriptInterface $script, bool $allowNonstandard = false): array |
||
522 | } |
||
523 |