Complex classes like Checker 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 Checker, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Checker |
||
26 | { |
||
27 | /** |
||
28 | * @var EcAdapterInterface |
||
29 | */ |
||
30 | protected $adapter; |
||
31 | |||
32 | /** |
||
33 | * @var TransactionInterface |
||
34 | */ |
||
35 | protected $transaction; |
||
36 | |||
37 | /** |
||
38 | * @var int |
||
39 | */ |
||
40 | protected $nInput; |
||
41 | |||
42 | /** |
||
43 | * @var int|string |
||
44 | */ |
||
45 | protected $amount; |
||
46 | |||
47 | /** |
||
48 | * @var Hasher |
||
49 | */ |
||
50 | protected $hasherV0; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $sigHashCache = []; |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $sigCache = []; |
||
61 | |||
62 | /** |
||
63 | * @var TransactionSignatureSerializer |
||
64 | */ |
||
65 | private $sigSerializer; |
||
66 | |||
67 | /** |
||
68 | * @var PublicKeySerializerInterface |
||
69 | */ |
||
70 | private $pubKeySerializer; |
||
71 | |||
72 | /** |
||
73 | * @var int |
||
74 | */ |
||
75 | protected $sigHashOptionalBits = SigHash::ANYONECANPAY; |
||
76 | |||
77 | /** |
||
78 | * Checker constructor. |
||
79 | * @param EcAdapterInterface $ecAdapter |
||
80 | * @param TransactionInterface $transaction |
||
81 | * @param int $nInput |
||
82 | * @param int $amount |
||
83 | * @param TransactionSignatureSerializer|null $sigSerializer |
||
84 | * @param PublicKeySerializerInterface|null $pubKeySerializer |
||
85 | */ |
||
86 | 2560 | public function __construct(EcAdapterInterface $ecAdapter, TransactionInterface $transaction, $nInput, $amount, TransactionSignatureSerializer $sigSerializer = null, PublicKeySerializerInterface $pubKeySerializer = null) |
|
95 | |||
96 | /** |
||
97 | * @param BufferInterface $signature |
||
98 | * @return bool |
||
99 | */ |
||
100 | 166 | public function isValidSignatureEncoding(BufferInterface $signature) |
|
111 | |||
112 | /** |
||
113 | * @param BufferInterface $signature |
||
114 | * @return bool |
||
115 | * @throws ScriptRuntimeException |
||
116 | * @throws \Exception |
||
117 | */ |
||
118 | 8 | public function isLowDerSignature(BufferInterface $signature) |
|
131 | |||
132 | /** |
||
133 | * @param int $hashType |
||
134 | * @return bool |
||
135 | */ |
||
136 | 154 | public function isDefinedHashtype($hashType) |
|
137 | { |
||
138 | 154 | $nHashType = $hashType & (~($this->sigHashOptionalBits)); |
|
139 | |||
140 | 154 | return !(($nHashType < SigHash::ALL) || ($nHashType > SigHash::SINGLE)); |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Determine whether the sighash byte appended to the signature encodes |
||
145 | * a valid sighash type. |
||
146 | * |
||
147 | * @param BufferInterface $signature |
||
148 | * @return bool |
||
149 | */ |
||
150 | 28 | public function isDefinedHashtypeSignature(BufferInterface $signature) |
|
159 | |||
160 | /** |
||
161 | * @param BufferInterface $signature |
||
162 | * @param int $flags |
||
163 | * @return $this |
||
164 | * @throws \BitWasp\Bitcoin\Exceptions\ScriptRuntimeException |
||
165 | */ |
||
166 | 384 | public function checkSignatureEncoding(BufferInterface $signature, $flags) |
|
182 | |||
183 | /** |
||
184 | * @param BufferInterface $publicKey |
||
185 | * @param int $flags |
||
186 | * @return $this |
||
187 | * @throws \Exception |
||
188 | */ |
||
189 | 324 | public function checkPublicKeyEncoding(BufferInterface $publicKey, $flags) |
|
197 | |||
198 | /** |
||
199 | * @param ScriptInterface $script |
||
200 | * @param int $sigHashType |
||
201 | * @param int $sigVersion |
||
202 | * @return BufferInterface |
||
203 | */ |
||
204 | 250 | public function getSigHash(ScriptInterface $script, $sigHashType, $sigVersion) |
|
226 | |||
227 | /** |
||
228 | * @param ScriptInterface $script |
||
229 | * @param BufferInterface $sigBuf |
||
230 | * @param BufferInterface $keyBuf |
||
231 | * @param int $sigVersion |
||
232 | * @param int $flags |
||
233 | * @return bool |
||
234 | * @throws ScriptRuntimeException |
||
235 | */ |
||
236 | 370 | public function checkSig(ScriptInterface $script, BufferInterface $sigBuf, BufferInterface $keyBuf, $sigVersion, $flags) |
|
259 | |||
260 | /** |
||
261 | * @param \BitWasp\Bitcoin\Script\Interpreter\Number $scriptLockTime |
||
262 | * @return bool |
||
263 | */ |
||
264 | 16 | public function checkLockTime(\BitWasp\Bitcoin\Script\Interpreter\Number $scriptLockTime) |
|
286 | |||
287 | /** |
||
288 | * @param \BitWasp\Bitcoin\Script\Interpreter\Number $sequence |
||
289 | * @return bool |
||
290 | */ |
||
291 | 18 | public function checkSequence(\BitWasp\Bitcoin\Script\Interpreter\Number $sequence) |
|
318 | } |
||
319 |