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:
1 | <?php |
||
10 | class ConnectionSettings |
||
11 | { |
||
12 | const tlsOptionForceHttps = 'forceHttps'; |
||
13 | const tlsOptionVersion = 'tlsVersion'; |
||
14 | const tlsOptionCipher = 'tlsCipher'; |
||
15 | const tlsOptionPinnedKey = 'pinnedKey'; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $threemaId; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $secret; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $host; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | private $tlsOptions = []; |
||
36 | |||
37 | /** |
||
38 | * @param string $threemaId valid threema id (8chars) |
||
39 | * @param string $secret secret |
||
40 | * @param string|null $host server url |
||
41 | * @param array|null $tlsOptions advanced TLS options |
||
42 | */ |
||
43 | public function __construct($threemaId, $secret, $host = null, array $tlsOptions = null) { |
||
70 | |||
71 | /** |
||
72 | * @return string |
||
73 | */ |
||
74 | public function getThreemaId() { |
||
77 | |||
78 | /** |
||
79 | * @return string |
||
80 | */ |
||
81 | public function getSecret() { |
||
84 | |||
85 | /** |
||
86 | * @return string |
||
87 | */ |
||
88 | public function getHost() { |
||
91 | |||
92 | /** |
||
93 | * @return array |
||
94 | */ |
||
95 | public function getTlsOptions() { |
||
98 | |||
99 | /** |
||
100 | * @return string |
||
101 | */ |
||
102 | public function getTlsOption($option, $default = null) { |
||
105 | } |
||
106 |
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.