Complex classes like SslCertificate 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 SslCertificate, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class SslCertificate |
||
| 9 | { |
||
| 10 | /** @var bool */ |
||
| 11 | protected $trusted; |
||
| 12 | |||
| 13 | /** @var bool */ |
||
| 14 | protected $revoked; |
||
| 15 | |||
| 16 | /** @var string */ |
||
| 17 | protected $ip; |
||
| 18 | |||
| 19 | /** @var BigInteger */ |
||
| 20 | protected $serial; |
||
| 21 | |||
| 22 | /** @var string */ |
||
| 23 | protected $testedDomain; |
||
| 24 | |||
| 25 | /** @var array */ |
||
| 26 | protected $certificateFields = []; |
||
| 27 | |||
| 28 | /** @var array */ |
||
| 29 | protected $certificateChains = []; |
||
| 30 | |||
| 31 | /** @var array */ |
||
| 32 | protected $connectionMeta = []; |
||
| 33 | |||
| 34 | /** @var SslRevocationList */ |
||
| 35 | protected $crl; |
||
| 36 | |||
| 37 | /** @var array */ |
||
| 38 | protected $crlLinks = []; |
||
| 39 | |||
| 40 | /** @var Carbon */ |
||
| 41 | protected $revokedTime; |
||
| 42 | |||
| 43 | 2 | public static function createForHostName(string $url, int $timeout = 30): SslCertificate |
|
| 49 | |||
| 50 | 3 | private static function extractCrlLinks($rawCrlPoints): string |
|
| 57 | |||
| 58 | 3 | private static function parseCrlLinks($rawCrlInput): array |
|
| 72 | |||
| 73 | 3 | private function getRevokedDate() |
|
| 81 | |||
| 82 | 3 | private function isClrRevoked() |
|
| 97 | |||
| 98 | 23 | private static function parseCertChains(array $chains): array |
|
| 107 | |||
| 108 | 23 | public function __construct(array $downloadResults) |
|
| 125 | |||
| 126 | 2 | public function hasSslChain(): bool |
|
| 134 | |||
| 135 | 1 | public function getTestedDomain(): string |
|
| 139 | |||
| 140 | 1 | public function getCertificateFields(): array |
|
| 144 | |||
| 145 | 1 | public function getCertificateChains(): array |
|
| 149 | |||
| 150 | 1 | public function getSerialNumber(): string |
|
| 154 | |||
| 155 | 3 | public function hasCrlLink(): bool |
|
| 159 | |||
| 160 | 3 | public function getCrlLinks() |
|
| 168 | |||
| 169 | public function getCrl() |
||
| 177 | |||
| 178 | 6 | public function isRevoked() |
|
| 182 | |||
| 183 | 1 | public function getCrlRevokedTime() |
|
| 189 | |||
| 190 | 1 | public function getResolvedIp(): string |
|
| 194 | |||
| 195 | 1 | public function getIssuer(): string |
|
| 199 | |||
| 200 | 8 | public function getDomain(): string |
|
| 204 | |||
| 205 | 1 | public function getSignatureAlgorithm(): string |
|
| 209 | |||
| 210 | 7 | public function getAdditionalDomains(): array |
|
| 218 | |||
| 219 | public function getConnectionMeta(): array |
||
| 223 | |||
| 224 | 6 | public function validFromDate(): Carbon |
|
| 228 | |||
| 229 | 7 | public function expirationDate(): Carbon |
|
| 233 | |||
| 234 | 1 | public function isExpired(): bool |
|
| 238 | |||
| 239 | 1 | public function isTrusted(): bool |
|
| 243 | |||
| 244 | 5 | public function isValid(string $url = null): bool |
|
| 261 | |||
| 262 | 1 | public function isValidUntil(Carbon $carbon, string $url = null): bool |
|
| 270 | |||
| 271 | 6 | public function appliesToUrl(string $url): bool |
|
| 272 | { |
||
| 273 | 6 | if (starts_with($url, '*.') === true) { |
|
| 274 | 1 | $url = substr($url, 2); |
|
| 275 | } |
||
| 276 | 6 | $host = (new Url($url))->getHostName(); |
|
| 277 | |||
| 278 | 6 | $certificateHosts = array_merge([$this->getDomain()], $this->getAdditionalDomains()); |
|
| 279 | |||
| 280 | 6 | foreach ($certificateHosts as $certificateHost) { |
|
| 281 | 6 | if ($host === $certificateHost) { |
|
| 282 | 5 | return true; |
|
| 283 | } |
||
| 284 | |||
| 285 | 2 | if ($this->wildcardHostCoversHost($certificateHost, $host)) { |
|
| 286 | 2 | return true; |
|
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | 1 | return false; |
|
| 291 | } |
||
| 292 | |||
| 293 | 2 | protected function wildcardHostCoversHost(string $wildcardHost, string $host): bool |
|
| 303 | } |
||
| 304 |