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 | use Macroable; |
||
11 | |||
12 | /** @var array */ |
||
13 | protected $rawCertificateFields = []; |
||
14 | |||
15 | /** @var string */ |
||
16 | protected $fingerprint = ''; |
||
17 | |||
18 | /** @var string */ |
||
19 | private $fingerprintSha256 = ''; |
||
20 | |||
21 | /** @var string */ |
||
22 | private $remoteAddress = ''; |
||
23 | |||
24 | public static function download(): Downloader |
||
28 | |||
29 | public static function createForHostName(string $url, int $timeout = 30): self |
||
33 | |||
34 | public static function createFromFile(string $pathToCertificate): self |
||
38 | |||
39 | View Code Duplication | public static function createFromString(string $certificatePem): self |
|
52 | |||
53 | public function __construct( |
||
67 | |||
68 | public function getRawCertificateFields(): array |
||
72 | |||
73 | public function getIssuer(): string |
||
77 | |||
78 | public function getDomain(): string |
||
94 | |||
95 | public function getSignatureAlgorithm(): string |
||
99 | |||
100 | public function getOrganization(): string |
||
104 | |||
105 | public function getFingerprint(): string |
||
109 | |||
110 | /** |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getFingerprintSha256(): string |
||
117 | |||
118 | public function getAdditionalDomains(): array |
||
126 | |||
127 | public function validFromDate(): Carbon |
||
131 | |||
132 | public function expirationDate(): Carbon |
||
136 | |||
137 | public function lifespanInDays(): int |
||
141 | |||
142 | public function isExpired(): bool |
||
146 | |||
147 | public function isValid(string $url = null) |
||
159 | |||
160 | public function isSelfSigned(): bool |
||
164 | |||
165 | public function usesSha1Hash(): bool |
||
179 | |||
180 | public function isValidUntil(Carbon $carbon, string $url = null): bool |
||
188 | |||
189 | public function daysUntilExpirationDate(): int |
||
197 | |||
198 | public function getDomains(): array |
||
206 | |||
207 | public function appliesToUrl(string $url): bool |
||
230 | |||
231 | protected function wildcardHostCoversHost(string $wildcardHost, string $host): bool |
||
251 | |||
252 | public function getRawCertificateFieldsJson(): string |
||
256 | |||
257 | public function getHash(): string |
||
261 | |||
262 | public function getRemoteAddress(): string |
||
266 | |||
267 | public function __toString(): string |
||
271 | |||
272 | public function containsDomain(string $domain): bool |
||
288 | |||
289 | public function isPreCertificate(): bool |
||
301 | } |
||
302 |
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.