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 | 1666 | * @param Opcodes|null $opCodes |
|
| 30 | */ |
||
| 31 | 1666 | public function __construct(BufferInterface $script = null, Opcodes $opCodes = null) |
|
| 36 | |||
| 37 | /** |
||
| 38 | 1444 | * @return BufferInterface |
|
| 39 | */ |
||
| 40 | 1444 | public function getBuffer() |
|
| 44 | |||
| 45 | /** |
||
| 46 | 1114 | * @return Parser |
|
| 47 | */ |
||
| 48 | 1114 | public function getScriptParser() |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Get all opcodes |
||
| 55 | * |
||
| 56 | 672 | * @return Opcodes |
|
| 57 | */ |
||
| 58 | 672 | public function getOpCodes() |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Return a buffer containing the hash of this script. |
||
| 65 | * |
||
| 66 | 162 | * @return BufferInterface |
|
| 67 | */ |
||
| 68 | 162 | public function getScriptHash() |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @param bool|true $accurate |
||
| 75 | 48 | * @return int |
|
| 76 | */ |
||
| 77 | 48 | public function countSigOps($accurate = true) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @param WitnessProgram $program |
||
| 111 | 18 | * @return int |
|
| 112 | */ |
||
| 113 | 18 | private function witnessSigOps(WitnessProgram $program) |
|
| 121 | |||
| 122 | 18 | /** |
|
| 123 | 18 | * @param ScriptInterface $scriptSig |
|
| 124 | 18 | * @param ScriptWitnessInterface $scriptWitness |
|
| 125 | 6 | * @param int $flags |
|
| 126 | * @return int |
||
| 127 | */ |
||
| 128 | 12 | public function countWitnessSigOps(ScriptInterface $scriptSig, ScriptWitnessInterface $scriptWitness, $flags) |
|
| 149 | 18 | ||
| 150 | /** |
||
| 151 | * @param ScriptInterface $scriptSig |
||
| 152 | * @return int |
||
| 153 | */ |
||
| 154 | public function countP2shSigOps(ScriptInterface $scriptSig) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | public function isPushOnly() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param WitnessProgram|null $program |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | public function isWitness(WitnessProgram & $program = null) |
||
| 226 | } |
||
| 227 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.