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 | * @var string |
||
19 | */ |
||
20 | private $fingerprintSha256; |
||
21 | |||
22 | public static function download(): Downloader |
||
26 | |||
27 | public static function createForHostName(string $url, int $timeout = 30): self |
||
33 | |||
34 | public function __construct(array $rawCertificateFields, string $fingerprint = '', string $fingerprintSha256 = '') |
||
41 | |||
42 | public function getRawCertificateFields(): array |
||
46 | |||
47 | public function getIssuer(): string |
||
51 | |||
52 | public function getDomain(): string |
||
68 | |||
69 | public function getSignatureAlgorithm(): string |
||
73 | |||
74 | public function getFingerprint(): string |
||
78 | |||
79 | /** |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getFingerprintSha256(): string |
||
86 | |||
87 | public function getAdditionalDomains(): array |
||
95 | |||
96 | public function validFromDate(): Carbon |
||
100 | |||
101 | public function expirationDate(): Carbon |
||
105 | |||
106 | public function isExpired(): bool |
||
110 | |||
111 | public function isValid(string $url = null) |
||
123 | |||
124 | public function isSelfSigned(): bool |
||
128 | |||
129 | public function usesSha1Hash(): bool |
||
143 | |||
144 | public function isValidUntil(Carbon $carbon, string $url = null): bool |
||
152 | |||
153 | public function daysUntilExpirationDate(): int |
||
161 | |||
162 | public function getDomains(): array |
||
170 | |||
171 | public function appliesToUrl(string $url): bool |
||
194 | |||
195 | protected function wildcardHostCoversHost(string $wildcardHost, string $host): bool |
||
215 | |||
216 | public function getRawCertificateFieldsJson(): string |
||
220 | |||
221 | public function getHash(): string |
||
225 | |||
226 | public function __toString(): string |
||
230 | |||
231 | public function containsDomain(string $domain): bool |
||
247 | |||
248 | public function isPreCertificate() |
||
253 | } |
||
254 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: