Complex classes like Script 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 Script, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Script extends Serializable implements ScriptInterface |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * @var Opcodes |
||
20 | */ |
||
21 | protected $opCodes; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $script; |
||
27 | |||
28 | /** |
||
29 | * @var BufferInterface|null |
||
30 | */ |
||
31 | protected $scriptHash; |
||
32 | |||
33 | /** |
||
34 | * @var BufferInterface|null |
||
35 | */ |
||
36 | protected $witnessScriptHash; |
||
37 | |||
38 | /** |
||
39 | * @param BufferInterface $script |
||
40 | * @param Opcodes|null $opCodes |
||
41 | 3014 | */ |
|
42 | public function __construct(BufferInterface $script = null, Opcodes $opCodes = null) |
||
47 | |||
48 | /** |
||
49 | * @return BufferInterface |
||
50 | 3052 | */ |
|
51 | public function getBuffer() |
||
55 | |||
56 | /** |
||
57 | * @return Parser |
||
58 | 2820 | */ |
|
59 | public function getScriptParser() |
||
63 | |||
64 | /** |
||
65 | * Get all opcodes |
||
66 | * |
||
67 | * @return Opcodes |
||
68 | 10 | */ |
|
69 | public function getOpCodes() |
||
73 | |||
74 | /** |
||
75 | * Return a buffer containing the HASH160 of this script. |
||
76 | * |
||
77 | * @return BufferInterface |
||
78 | 94 | */ |
|
79 | public function getScriptHash() |
||
87 | |||
88 | /** |
||
89 | * Return a buffer containing the SHA256 of this script. |
||
90 | * |
||
91 | * @return BufferInterface |
||
92 | 180 | */ |
|
93 | public function getWitnessScriptHash() |
||
101 | |||
102 | /** |
||
103 | * @param bool|true $accurate |
||
104 | * @return int |
||
105 | 18 | */ |
|
106 | public function countSigOps($accurate = true) |
||
135 | |||
136 | /** |
||
137 | * @param WitnessProgram $program |
||
138 | * @param ScriptWitnessInterface $scriptWitness |
||
139 | * @return int |
||
140 | 2 | */ |
|
141 | private function witnessSigOps(WitnessProgram $program, ScriptWitnessInterface $scriptWitness) |
||
157 | |||
158 | /** |
||
159 | * @param ScriptInterface $scriptSig |
||
160 | * @param ScriptWitnessInterface $scriptWitness |
||
161 | * @param int $flags |
||
162 | * @return int |
||
163 | 2 | */ |
|
164 | public function countWitnessSigOps(ScriptInterface $scriptSig, ScriptWitnessInterface $scriptWitness, $flags) |
||
187 | |||
188 | /** |
||
189 | * @param ScriptInterface $scriptSig |
||
190 | * @return int |
||
191 | 8 | */ |
|
192 | public function countP2shSigOps(ScriptInterface $scriptSig) |
||
219 | |||
220 | /** |
||
221 | * @param array|null $ops |
||
222 | 214 | * @return bool |
|
223 | */ |
||
224 | 214 | public function isPushOnly(array&$ops = null) |
|
248 | |||
249 | /** |
||
250 | 286 | * @param WitnessProgram|null $program |
|
251 | 286 | * @return bool |
|
252 | 176 | */ |
|
253 | public function isWitness(& $program = null) |
||
279 | 12 | ||
280 | /** |
||
281 | * @param BufferInterface $scriptHash |
||
282 | * @return bool |
||
283 | */ |
||
284 | public function isP2SH(& $scriptHash) |
||
297 | 2 | ||
298 | /** |
||
299 | * @param ScriptInterface $script |
||
300 | * @return bool |
||
301 | */ |
||
302 | 2 | public function equals(ScriptInterface $script) |
|
306 | |||
307 | /** |
||
308 | * @return string |
||
309 | */ |
||
310 | public function __debugInfo() |
||
322 | } |
||
323 |