Complex classes like EcAdapter 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 EcAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class EcAdapter implements EcAdapterInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var Math |
||
| 28 | */ |
||
| 29 | private $math; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var GeneratorPoint |
||
| 33 | */ |
||
| 34 | private $generator; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param Math $math |
||
| 38 | 28 | * @param GeneratorPoint $generator |
|
| 39 | */ |
||
| 40 | 28 | public function __construct(Math $math, GeneratorPoint $generator) |
|
| 45 | |||
| 46 | /** |
||
| 47 | 378 | * @return Math |
|
| 48 | */ |
||
| 49 | 378 | public function getMath() |
|
| 53 | |||
| 54 | /** |
||
| 55 | 270 | * @return GeneratorPoint |
|
| 56 | */ |
||
| 57 | 270 | public function getGenerator() |
|
| 61 | |||
| 62 | /** |
||
| 63 | * @param \GMP $scalar |
||
| 64 | * @param bool|false $compressed |
||
| 65 | 104 | * @return PrivateKey |
|
| 66 | */ |
||
| 67 | 104 | public function getPrivateKey(\GMP $scalar, $compressed = false) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @param PointInterface $point |
||
| 74 | * @param bool|false $compressed |
||
| 75 | 88 | * @return PublicKey |
|
| 76 | */ |
||
| 77 | 88 | public function getPublicKey(PointInterface $point, $compressed = false) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @param \GMP $r |
||
| 84 | * @param \GMP $s |
||
| 85 | * @return Signature |
||
| 86 | */ |
||
| 87 | public function getSignature(\GMP $r, \GMP $s) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param BufferInterface $messageHash |
||
| 94 | * @param PublicKey $publicKey |
||
| 95 | * @param Signature $signature |
||
| 96 | 157 | * @return bool |
|
| 97 | */ |
||
| 98 | 157 | private function doVerify(BufferInterface $messageHash, PublicKey $publicKey, Signature $signature) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @param BufferInterface $messageHash |
||
| 107 | * @param PublicKeyInterface $publicKey |
||
| 108 | * @param SignatureInterface $signature |
||
| 109 | 157 | * @return bool |
|
| 110 | */ |
||
| 111 | public function verify(BufferInterface $messageHash, PublicKeyInterface $publicKey, SignatureInterface $signature) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param BufferInterface $messageHash |
||
| 120 | * @param PrivateKey $privateKey |
||
| 121 | * @param RbgInterface|null $rbg |
||
| 122 | 46 | * @return Signature |
|
| 123 | */ |
||
| 124 | 46 | private function doSign(BufferInterface $messageHash, PrivateKey $privateKey, RbgInterface $rbg = null) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * @param BufferInterface $messageHash |
||
| 144 | * @param PrivateKeyInterface $privateKey |
||
| 145 | * @param RbgInterface $rbg |
||
| 146 | * @return SignatureInterface |
||
| 147 | 46 | * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure |
|
| 148 | */ |
||
| 149 | public function sign(BufferInterface $messageHash, PrivateKeyInterface $privateKey, RbgInterface $rbg = null) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param BufferInterface $messageHash |
||
| 157 | * @param CompactSignatureInterface $signature |
||
| 158 | * @return PublicKey |
||
| 159 | 20 | * @throws \Exception |
|
| 160 | */ |
||
| 161 | 20 | public function recover(BufferInterface $messageHash, CompactSignatureInterface $signature) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Attempt to calculate the public key recovery param by trial and error |
||
| 221 | * |
||
| 222 | * @param \GMP $r |
||
| 223 | * @param \GMP $s |
||
| 224 | * @param BufferInterface $messageHash |
||
| 225 | * @param PublicKey $publicKey |
||
| 226 | * @return int |
||
| 227 | 18 | * @throws \Exception |
|
| 228 | */ |
||
| 229 | 18 | public function calcPubKeyRecoveryParam(\GMP $r, \GMP $s, BufferInterface $messageHash, PublicKey $publicKey) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @param BufferInterface $messageHash |
||
| 248 | * @param PrivateKey $privateKey |
||
| 249 | * @param RbgInterface|null $rbg |
||
| 250 | * @return CompactSignature |
||
| 251 | 12 | * @throws \Exception |
|
| 252 | */ |
||
| 253 | 12 | private function doSignCompact(BufferInterface $messageHash, PrivateKey $privateKey, RbgInterface $rbg = null) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * @param PrivateKeyInterface $privateKey |
||
| 270 | * @param BufferInterface $messageHash |
||
| 271 | * @param RbgInterface $rbg |
||
| 272 | 12 | * @return CompactSignature |
|
| 273 | */ |
||
| 274 | public function signCompact(BufferInterface $messageHash, PrivateKeyInterface $privateKey, RbgInterface $rbg = null) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param BufferInterface $privateKey |
||
| 282 | 108 | * @return bool |
|
| 283 | */ |
||
| 284 | 108 | public function validatePrivateKey(BufferInterface $privateKey) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * @param \GMP $element |
||
| 293 | * @param bool $half |
||
| 294 | 52 | * @return bool |
|
| 295 | */ |
||
| 296 | 52 | public function validateSignatureElement(\GMP $element, $half = false) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * @param BufferInterface $publicKey |
||
| 309 | * @return PublicKeyInterface |
||
| 310 | 272 | * @throws \Exception |
|
| 311 | */ |
||
| 312 | 272 | public function publicKeyFromBuffer(BufferInterface $publicKey) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * @param \GMP $int |
||
| 346 | * @param int $byteSize |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | private function fixedSizeInt(\GMP $int, $byteSize) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param PrivateKey $privateKey |
||
| 366 | * @param PublicKey $publicKey |
||
| 367 | * @return BufferInterface |
||
| 368 | */ |
||
| 369 | private function doEcdh(PrivateKey $privateKey, PublicKey $publicKey) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param PrivateKeyInterface $privateKey |
||
| 379 | * @param PublicKeyInterface $publicKey |
||
| 380 | * @return BufferInterface |
||
| 381 | */ |
||
| 382 | public function ecdh(PrivateKeyInterface $privateKey, PublicKeyInterface $publicKey) |
||
| 390 | } |
||
| 391 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.