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 |
||
| 14 | class Script extends Serializable implements ScriptInterface |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var Opcodes |
||
| 19 | */ |
||
| 20 | protected $opCodes; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $script; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var BufferInterface|null |
||
| 29 | */ |
||
| 30 | protected $scriptHash; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var BufferInterface|null |
||
| 34 | */ |
||
| 35 | protected $witnessScriptHash; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param BufferInterface $script |
||
| 39 | * @param Opcodes|null $opCodes |
||
| 40 | */ |
||
| 41 | 1604 | public function __construct(BufferInterface $script = null, Opcodes $opCodes = null) |
|
| 46 | |||
| 47 | /** |
||
| 48 | * @return BufferInterface |
||
| 49 | */ |
||
| 50 | 1614 | public function getBuffer() |
|
| 54 | |||
| 55 | /** |
||
| 56 | * @return Parser |
||
| 57 | */ |
||
| 58 | 1446 | public function getScriptParser() |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Get all opcodes |
||
| 65 | * |
||
| 66 | * @return Opcodes |
||
| 67 | */ |
||
| 68 | 6 | public function getOpCodes() |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Return a buffer containing the HASH160 of this script. |
||
| 75 | * |
||
| 76 | * @return BufferInterface |
||
| 77 | */ |
||
| 78 | 36 | public function getScriptHash() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Return a buffer containing the SHA256 of this script. |
||
| 89 | * |
||
| 90 | * @return BufferInterface |
||
| 91 | */ |
||
| 92 | 79 | public function getWitnessScriptHash() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @param bool|true $accurate |
||
| 103 | * @return int |
||
| 104 | */ |
||
| 105 | 18 | public function countSigOps($accurate = true) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @param WitnessProgram $program |
||
| 137 | * @param ScriptWitnessInterface $scriptWitness |
||
| 138 | * @return int |
||
| 139 | */ |
||
| 140 | 2 | private function witnessSigOps(WitnessProgram $program, ScriptWitnessInterface $scriptWitness) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * @param ScriptInterface $scriptSig |
||
| 159 | * @param ScriptWitnessInterface $scriptWitness |
||
| 160 | * @param int $flags |
||
| 161 | * @return int |
||
| 162 | */ |
||
| 163 | 2 | public function countWitnessSigOps(ScriptInterface $scriptSig, ScriptWitnessInterface $scriptWitness, $flags) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @param ScriptInterface $scriptSig |
||
| 189 | * @return int |
||
| 190 | */ |
||
| 191 | 8 | public function countP2shSigOps(ScriptInterface $scriptSig) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | 106 | public function isPushOnly() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @param WitnessProgram|null $program |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | 123 | public function isWitness(& $program = null) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param ScriptInterface $script |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | 153 | public function equals(ScriptInterface $script) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | 2 | public function __debugInfo() |
|
| 288 | } |
||
| 289 |