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 | use Macroable; |
||
| 11 | |||
| 12 | /** @var array */ |
||
| 13 | protected $rawCertificateFields = []; |
||
| 14 | |||
| 15 | /** @var string */ |
||
| 16 | protected $fingerprint = ''; |
||
| 17 | |||
| 18 | public static function download(): Downloader |
||
| 22 | |||
| 23 | public static function createForHostName(string $url, int $timeout = 30): self |
||
| 29 | |||
| 30 | public function __construct(array $rawCertificateFields, string $fingerprint = '') |
||
| 31 | { |
||
| 32 | $this->rawCertificateFields = $rawCertificateFields; |
||
| 33 | |||
| 34 | $this->fingerprint = $fingerprint; |
||
| 35 | } |
||
| 36 | |||
| 37 | public function getRawCertificateFields(): array |
||
| 38 | { |
||
| 39 | return $this->rawCertificateFields; |
||
| 40 | } |
||
| 41 | |||
| 42 | public function getIssuer(): string |
||
| 43 | { |
||
| 44 | return $this->rawCertificateFields['issuer']['CN'] ?? ''; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function getDomain(): string |
||
| 48 | { |
||
| 49 | if (! array_key_exists('CN', $this->rawCertificateFields['subject'])) { |
||
| 50 | return ''; |
||
| 51 | } |
||
| 52 | |||
| 53 | if (is_string($this->rawCertificateFields['subject']['CN'])) { |
||
| 54 | return $this->rawCertificateFields['subject']['CN']; |
||
| 55 | } |
||
| 56 | |||
| 57 | if (is_array($this->rawCertificateFields['subject']['CN'])) { |
||
| 58 | return $this->rawCertificateFields['subject']['CN'][0]; |
||
| 59 | } |
||
| 60 | |||
| 61 | return ''; |
||
| 62 | } |
||
| 63 | |||
| 64 | public function getSignatureAlgorithm(): string |
||
| 68 | |||
| 69 | public function getFingerprint(): string |
||
| 70 | { |
||
| 71 | return $this->fingerprint; |
||
| 72 | } |
||
| 73 | |||
| 74 | public function getAdditionalDomains(): array |
||
| 82 | |||
| 83 | public function validFromDate(): Carbon |
||
| 87 | |||
| 88 | public function expirationDate(): Carbon |
||
| 92 | |||
| 93 | public function isExpired(): bool |
||
| 97 | |||
| 98 | public function isValid(string $url = null) |
||
| 110 | |||
| 111 | public function isSelfSigned(): bool |
||
| 115 | |||
| 116 | public function usesSha1Hash(): bool |
||
| 130 | |||
| 131 | public function isValidUntil(Carbon $carbon, string $url = null): bool |
||
| 139 | |||
| 140 | public function daysUntilExpirationDate(): int |
||
| 148 | |||
| 149 | public function getDomains(): array |
||
| 157 | |||
| 158 | public function appliesToUrl(string $url): bool |
||
| 176 | |||
| 177 | protected function wildcardHostCoversHost(string $wildcardHost, string $host): bool |
||
| 197 | |||
| 198 | public function getRawCertificateFieldsJson(): string |
||
| 202 | |||
| 203 | public function getHash(): string |
||
| 207 | |||
| 208 | public function __toString(): string |
||
| 212 | |||
| 213 | public function containsDomain(string $domain): bool |
||
| 229 | } |
||
| 230 |