Complex classes like Checksig 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 Checksig, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Checksig |
||
17 | { |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $scriptType; |
||
22 | |||
23 | /** |
||
24 | * @var bool |
||
25 | */ |
||
26 | private $required = true; |
||
27 | |||
28 | /** |
||
29 | * @var PayToPubkeyHash|PayToPubkey|Multisig |
||
30 | */ |
||
31 | private $info; |
||
32 | |||
33 | /** |
||
34 | * @var int |
||
35 | */ |
||
36 | protected $requiredSigs; |
||
37 | |||
38 | /** |
||
39 | * @var int |
||
40 | */ |
||
41 | protected $keyCount; |
||
42 | |||
43 | /** |
||
44 | * @var TransactionSignatureInterface[] |
||
45 | */ |
||
46 | protected $signatures = []; |
||
47 | |||
48 | /** |
||
49 | * @var PublicKeyInterface[]|null[] |
||
50 | */ |
||
51 | protected $publicKeys = []; |
||
52 | |||
53 | /** |
||
54 | * Checksig constructor. |
||
55 | * @param $info |
||
56 | */ |
||
57 | public function __construct($info, TransactionSignatureSerializer $txSigSerializer, PublicKeySerializerInterface $pubKeySerializer) |
||
91 | |||
92 | public function receivesValue(Conditional $conditional) |
||
102 | |||
103 | public function setRequired($setting) |
||
111 | |||
112 | public function isRequired() |
||
116 | |||
117 | /** |
||
118 | * @return Multisig|PayToPubkey|PayToPubkeyHash |
||
119 | */ |
||
120 | public function getInfo() |
||
124 | |||
125 | /** |
||
126 | * @return string |
||
127 | */ |
||
128 | public function getType() |
||
132 | |||
133 | /** |
||
134 | * @return array|BufferInterface|BufferInterface[] |
||
135 | */ |
||
136 | public function getSolution() |
||
146 | |||
147 | /** |
||
148 | * @return int |
||
149 | */ |
||
150 | public function getRequiredSigs() |
||
154 | |||
155 | /** |
||
156 | * @return bool |
||
157 | */ |
||
158 | public function isFullySigned() |
||
166 | |||
167 | /** |
||
168 | * @param int $idx |
||
169 | * @return bool |
||
170 | */ |
||
171 | public function hasSignature($idx) |
||
179 | |||
180 | /** |
||
181 | * @param int $idx |
||
182 | * @param TransactionSignatureInterface $signature |
||
183 | * @return $this |
||
184 | */ |
||
185 | public function setSignature($idx, TransactionSignatureInterface $signature) |
||
194 | |||
195 | /** |
||
196 | * @param int $idx |
||
197 | * @return TransactionSignatureInterface|null |
||
198 | */ |
||
199 | public function getSignature($idx) |
||
207 | |||
208 | /** |
||
209 | * @return array |
||
210 | */ |
||
211 | public function getSignatures() |
||
215 | |||
216 | /** |
||
217 | * @param int $idx |
||
218 | * @return bool |
||
219 | */ |
||
220 | public function hasKey($idx) |
||
224 | |||
225 | /** |
||
226 | * @param int $idx |
||
227 | * @return PublicKeyInterface|null |
||
228 | */ |
||
229 | public function getKey($idx) |
||
237 | |||
238 | /** |
||
239 | * @param $idx |
||
240 | * @param PublicKeyInterface|null $key |
||
241 | * @return $this |
||
242 | */ |
||
243 | public function setKey($idx, $key) |
||
252 | |||
253 | /** |
||
254 | * @return PublicKeyInterface[] |
||
255 | */ |
||
256 | public function getKeys() |
||
260 | |||
261 | public function isVerify() |
||
265 | |||
266 | /** |
||
267 | * @return array |
||
268 | */ |
||
269 | public function serialize() |
||
307 | } |
||
308 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: