Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 $inputDomain; |
||
24 | |||
25 | /** @var string */ |
||
26 | protected $testedDomain; |
||
27 | |||
28 | /** @var array */ |
||
29 | protected $certificateFields = []; |
||
30 | |||
31 | /** @var array */ |
||
32 | protected $certificateChains = []; |
||
33 | |||
34 | /** @var array */ |
||
35 | protected $connectionMeta = []; |
||
36 | |||
37 | /** @var array */ |
||
38 | protected $crlLinks = []; |
||
39 | |||
40 | /** @var SslRevocationList */ |
||
41 | protected $crl = null; |
||
42 | |||
43 | /** @var Carbon */ |
||
44 | protected $revokedTime = null; |
||
45 | |||
46 | public static function createForHostName(string $url, int $timeout = 30): SslCertificate |
||
52 | |||
53 | private static function extractCrlLinks($rawCrlPoints): string |
||
60 | |||
61 | private static function parseCrlLinks($rawCrlInput): array |
||
75 | |||
76 | private static function parseCertChains(array $chains): array |
||
85 | |||
86 | |||
87 | public function withSslCrlCheck(): SslCertificate |
||
106 | |||
107 | public function __construct(array $downloadResults) |
||
122 | |||
123 | public function hasSslChain(): bool |
||
131 | |||
132 | public function getCertificateFields(): array |
||
136 | |||
137 | public function getCertificateChains(): array |
||
141 | |||
142 | public function getSerialNumber(): string |
||
146 | |||
147 | public function hasCrlLink(): bool |
||
151 | |||
152 | public function getCrlLinks() |
||
160 | |||
161 | public function getCrl() |
||
169 | |||
170 | public function isRevoked() |
||
174 | |||
175 | public function getCrlRevokedTime() |
||
181 | |||
182 | public function getResolvedIp(): string |
||
186 | |||
187 | public function getIssuer(): string |
||
191 | |||
192 | public function getDomain(): string |
||
201 | |||
202 | public function getTestedDomain(): string |
||
206 | |||
207 | public function getInputDomain(): string |
||
211 | |||
212 | public function getCertificateDomain(): string |
||
216 | |||
217 | public function getAdditionalDomains(): array |
||
225 | |||
226 | public function getSignatureAlgorithm(): string |
||
230 | |||
231 | public function getConnectionMeta(): array |
||
235 | |||
236 | public function validFromDate(): Carbon |
||
240 | |||
241 | public function expirationDate(): Carbon |
||
245 | |||
246 | public function isExpired(): bool |
||
250 | |||
251 | public function isTrusted(): bool |
||
255 | |||
256 | public function isValid(string $url = null): bool |
||
273 | |||
274 | public function isValidUntil(Carbon $carbon, string $url = null): bool |
||
282 | |||
283 | View Code Duplication | public function isValidDate(Carbon $carbon): bool |
|
291 | |||
292 | public function isSelfSigned(): bool |
||
315 | |||
316 | public function appliesToUrl(string $url): bool |
||
337 | |||
338 | protected function wildcardHostCoversHost(string $wildcardHost, string $host): bool |
||
348 | } |
||
349 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.