Complex classes like Curve 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 Curve, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | final class Curve |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Elliptic curve over the field of integers modulo a prime. |
||
| 27 | * |
||
| 28 | * @var \GMP |
||
| 29 | */ |
||
| 30 | private $a; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var \GMP |
||
| 34 | */ |
||
| 35 | private $b; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var \GMP |
||
| 39 | */ |
||
| 40 | private $prime; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Binary length of keys associated with these curve parameters. |
||
| 44 | * |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | private $size; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var Point |
||
| 51 | */ |
||
| 52 | private $generator; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param int $size |
||
| 56 | * @param \GMP $prime |
||
| 57 | * @param \GMP $a |
||
| 58 | * @param \GMP $b |
||
| 59 | * @param Point $generator |
||
| 60 | */ |
||
| 61 | public function __construct(int $size, \GMP $prime, \GMP $a, \GMP $b, Point $generator) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return \GMP |
||
| 72 | */ |
||
| 73 | public function getA(): \GMP |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return \GMP |
||
| 80 | */ |
||
| 81 | public function getB(): \GMP |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @return \GMP |
||
| 88 | */ |
||
| 89 | public function getPrime(): \GMP |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return int |
||
| 96 | */ |
||
| 97 | public function getSize(): int |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param \GMP $x |
||
| 104 | * @param \GMP $y |
||
| 105 | * @param \GMP|null $order |
||
| 106 | * |
||
| 107 | * @return Point |
||
| 108 | */ |
||
| 109 | public function getPoint(\GMP $x, \GMP $y, ?\GMP $order = null): Point |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param \GMP $x |
||
| 127 | * @param \GMP $y |
||
| 128 | * |
||
| 129 | * @return PublicKey |
||
| 130 | */ |
||
| 131 | public function getPublicKeyFrom(\GMP $x, \GMP $y): PublicKey |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param \GMP $x |
||
| 144 | * @param \GMP $y |
||
| 145 | * |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | public function contains(\GMP $x, \GMP $y): bool |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param Point $one |
||
| 170 | * @param Point $two |
||
| 171 | * |
||
| 172 | * @return Point |
||
| 173 | */ |
||
| 174 | public function add(Point $one, Point $two): Point |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param Point $one |
||
| 215 | * @param \GMP $n |
||
| 216 | * |
||
| 217 | * @return Point |
||
| 218 | */ |
||
| 219 | public function mul(Point $one, \GMP $n): Point |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param Curve $other |
||
| 259 | * |
||
| 260 | * @return int |
||
| 261 | */ |
||
| 262 | public function cmp(Curve $other): int |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param Curve $other |
||
| 273 | * |
||
| 274 | * @return bool |
||
| 275 | */ |
||
| 276 | public function equals(Curve $other): bool |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function __toString(): string |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param Point $point |
||
| 291 | */ |
||
| 292 | private function validate(Point $point) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param Point $point |
||
| 301 | * |
||
| 302 | * @return Point |
||
| 303 | */ |
||
| 304 | public function getDouble(Point $point): Point |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @return PrivateKey |
||
| 336 | */ |
||
| 337 | public function createPrivateKey(): PrivateKey |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param PrivateKey $privateKey |
||
| 344 | * |
||
| 345 | * @return PublicKey |
||
| 346 | */ |
||
| 347 | public function createPublicKey(PrivateKey $privateKey): PublicKey |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @return \GMP |
||
| 356 | */ |
||
| 357 | private function generate(): \GMP |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Returns the number of bits used to store this number. Non-significant upper bits are not counted. |
||
| 373 | * |
||
| 374 | * @param \GMP $x |
||
| 375 | * |
||
| 376 | * @return int |
||
| 377 | * |
||
| 378 | * @see https://www.openssl.org/docs/crypto/BN_num_bytes.html |
||
| 379 | */ |
||
| 380 | private function bnNumBits(\GMP $x): int |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @return Point |
||
| 397 | */ |
||
| 398 | public function getGenerator(): Point |
||
| 402 | } |
||
| 403 |