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 | * @param BufferInterface $script |
||
| 29 | * @param Opcodes|null $opCodes |
||
| 30 | */ |
||
| 31 | 2385 | public function __construct(BufferInterface $script = null, Opcodes $opCodes = null) |
|
| 32 | { |
||
| 33 | 2385 | $this->script = $script instanceof BufferInterface ? $script->getBinary() : ''; |
|
| 34 | 2385 | $this->opCodes = $opCodes ?: new Opcodes(); |
|
| 35 | 2385 | } |
|
| 36 | |||
| 37 | /** |
||
| 38 | * @return BufferInterface |
||
| 39 | */ |
||
| 40 | 2199 | public function getBuffer() |
|
| 44 | |||
| 45 | /** |
||
| 46 | * @return Parser |
||
| 47 | */ |
||
| 48 | 1863 | public function getScriptParser() |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Get all opcodes |
||
| 55 | * |
||
| 56 | * @return Opcodes |
||
| 57 | */ |
||
| 58 | 1326 | public function getOpCodes() |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Return a buffer containing the hash of this script. |
||
| 65 | * |
||
| 66 | * @return BufferInterface |
||
| 67 | */ |
||
| 68 | 198 | public function getScriptHash() |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @param bool|true $accurate |
||
| 75 | * @return int |
||
| 76 | */ |
||
| 77 | 48 | public function countSigOps($accurate = true) |
|
| 78 | { |
||
| 79 | 48 | $count = 0; |
|
| 80 | 48 | $parser = $this->getScriptParser(); |
|
| 81 | |||
| 82 | 48 | $lastOp = 0xff; |
|
| 83 | 48 | foreach ($parser as $exec) { |
|
| 84 | 36 | if ($exec->isPush()) { |
|
| 85 | 36 | continue; |
|
| 86 | } |
||
| 87 | |||
| 88 | 36 | $op = $exec->getOp(); |
|
| 89 | 36 | if ($op > Opcodes::OP_PUSHDATA4) { |
|
| 90 | // None of these are pushdatas, so just an opcode |
||
| 91 | 36 | if ($op === Opcodes::OP_CHECKSIG || $op === Opcodes::OP_CHECKSIGVERIFY) { |
|
| 92 | 24 | $count++; |
|
| 93 | 36 | } elseif ($op === Opcodes::OP_CHECKMULTISIG || $op === Opcodes::OP_CHECKMULTISIGVERIFY) { |
|
| 94 | 30 | if ($accurate && ($lastOp >= Opcodes::OP_1 && $lastOp <= Opcodes::OP_16)) { |
|
| 95 | 24 | $c = ($lastOp - (Opcodes::OP_1 - 1)); |
|
| 96 | 24 | $count += $c; |
|
| 97 | 24 | } else { |
|
| 98 | 12 | $count += 20; |
|
| 99 | } |
||
| 100 | 30 | } |
|
| 101 | |||
| 102 | 36 | $lastOp = $op; |
|
| 103 | 36 | } |
|
| 104 | 48 | } |
|
| 105 | |||
| 106 | 48 | return $count; |
|
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param WitnessProgram $program |
||
| 111 | * @return int |
||
| 112 | */ |
||
| 113 | private function witnessSigOps(WitnessProgram $program, ScriptWitnessInterface $scriptWitness) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param ScriptInterface $scriptSig |
||
| 132 | * @param ScriptWitnessInterface $scriptWitness |
||
| 133 | * @param int $flags |
||
| 134 | * @return int |
||
| 135 | */ |
||
| 136 | public function countWitnessSigOps(ScriptInterface $scriptSig, ScriptWitnessInterface $scriptWitness, $flags) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param ScriptInterface $scriptSig |
||
| 162 | * @return int |
||
| 163 | */ |
||
| 164 | 18 | public function countP2shSigOps(ScriptInterface $scriptSig) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | 60 | public function isPushOnly() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * @param WitnessProgram|null $program |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | 108 | public function isWitness(WitnessProgram & $program = null) |
|
| 235 | } |
||
| 236 |