Complex classes like ECKey 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 ECKey, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | final class ECKey |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var null|Sequence |
||
| 30 | */ |
||
| 31 | private $sequence = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var bool |
||
| 35 | */ |
||
| 36 | private $private = false; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | private $values = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * ECKey constructor. |
||
| 45 | * |
||
| 46 | * @param JWK $data |
||
| 47 | */ |
||
| 48 | private function __construct(JWK $data) |
||
| 49 | { |
||
| 50 | $this->loadJWK($data->all()); |
||
| 51 | $this->private = isset($this->values['d']); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param JWK $jwk |
||
| 56 | * |
||
| 57 | * @return ECKey |
||
| 58 | */ |
||
| 59 | public static function createFromJWK(JWK $jwk): ECKey |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param string $pem |
||
| 66 | * |
||
| 67 | * @return ECKey |
||
| 68 | */ |
||
| 69 | public static function createFromPEM(string $pem): ECKey |
||
| 70 | { |
||
| 71 | $data = self::loadPEM($pem); |
||
| 72 | |||
| 73 | return new self(JWK::create($data)); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param string $data |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | * |
||
| 81 | * @throws \Exception |
||
| 82 | */ |
||
| 83 | private static function loadPEM(string $data): array |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param array $children |
||
| 107 | * |
||
| 108 | * @return array |
||
| 109 | */ |
||
| 110 | private static function loadPKCS8(array $children): array |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param array $children |
||
| 123 | * |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | private static function loadPublicPEM(array $children): array |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $oid |
||
| 162 | * |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | private static function getCurve(string $oid): string |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | private static function getSupportedCurves(): array |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param object $children |
||
| 190 | */ |
||
| 191 | private static function verifyVersion(Object $children) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param object $children |
||
| 200 | * @param string|null $x |
||
| 201 | * @param string|null $y |
||
| 202 | */ |
||
| 203 | private static function getXAndY(Object $children, ?string &$x, ?string &$y) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param object $children |
||
| 225 | * |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | private static function getD(Object $children): string |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param array $children |
||
| 239 | * |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | private static function loadPrivatePEM(array $children): array |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param array $children |
||
| 270 | * |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | private static function isPKCS8(array $children): bool |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param ECKey $private |
||
| 291 | * |
||
| 292 | * @return ECKey |
||
| 293 | */ |
||
| 294 | public static function toPublic(ECKey $private): ECKey |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @return array |
||
| 306 | */ |
||
| 307 | public function toArray() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param array $jwk |
||
| 314 | */ |
||
| 315 | private function loadJWK(array $jwk) |
||
| 334 | |||
| 335 | private function initPublicKey() |
||
| 348 | |||
| 349 | private function initPrivateKey() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | public function toPEM(): string |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param $curve |
||
| 386 | * |
||
| 387 | * @return string |
||
| 388 | */ |
||
| 389 | private function getOID(string $curve): string |
||
| 399 | } |
||
| 400 |