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 UNKNOWN = 'unknown'; |
||
20 | const NONSTANDARD = 'nonstandard'; |
||
21 | |||
22 | /** |
||
23 | * @param ScriptInterface $script |
||
24 | * @param BufferInterface $publicKey |
||
25 | * @return bool |
||
26 | */ |
||
27 | public function isPayToPublicKey(ScriptInterface $script, & $publicKey = null) |
||
45 | 144 | ||
46 | /** |
||
47 | 144 | * @param ScriptInterface $script |
|
48 | 120 | * @param null $pubKeyHash |
|
49 | * @return bool |
||
50 | */ |
||
51 | 42 | public function isPayToPublicKeyHash(ScriptInterface $script, & $pubKeyHash = null) |
|
82 | |||
83 | /** |
||
84 | 66 | * @param ScriptInterface $script |
|
85 | * @param null $scriptHash |
||
86 | 66 | * @return bool |
|
87 | 66 | */ |
|
88 | 66 | public function isPayToScriptHash(ScriptInterface $script, & $scriptHash = null) |
|
113 | 72 | ||
114 | 72 | /** |
|
115 | * @param ScriptInterface $script |
||
116 | * @param array $keys |
||
117 | * @return bool |
||
118 | */ |
||
119 | 72 | public function isMultisig(ScriptInterface $script, & $keys = []) |
|
153 | 48 | ||
154 | 48 | /** |
|
155 | * @param ScriptInterface $script |
||
156 | 48 | * @param null $programHash |
|
157 | 48 | * @return bool |
|
158 | 48 | */ |
|
159 | 48 | public function isWitness(ScriptInterface $script, & $programHash = null) |
|
184 | |||
185 | 30 | /** |
|
186 | 30 | * @param ScriptInterface $script |
|
187 | * @param null $solution |
||
188 | * @return string |
||
189 | */ |
||
190 | 30 | public function classify(ScriptInterface $script, &$solution = null) |
|
217 | } |
||
218 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.