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 |
||
21 | class AppConfigService { |
||
22 | |||
23 | /** |
||
24 | * |
||
25 | * @var IConfig |
||
26 | */ |
||
27 | private $config; |
||
28 | |||
29 | /** |
||
30 | * |
||
31 | * @var IL10N |
||
32 | */ |
||
33 | private $l10n; |
||
34 | |||
35 | /** |
||
36 | * |
||
37 | * @var RedisUtil |
||
38 | */ |
||
39 | private $redisUtil; |
||
40 | |||
41 | /** |
||
42 | * |
||
43 | * @var ILogger |
||
44 | */ |
||
45 | private $logger; |
||
46 | |||
47 | /** |
||
48 | * Constructor |
||
49 | * |
||
50 | * @param IConfig $config |
||
51 | * @param IL10N $l10n |
||
52 | * @param RedisUtil $redisUtil |
||
53 | * @param ILogger $logger |
||
54 | */ |
||
55 | 32 | public function __construct(IConfig $config, IL10N $l10n, RedisUtil $redisUtil, ILogger $logger) { |
|
61 | |||
62 | /** |
||
63 | * Get a value by key |
||
64 | * |
||
65 | * @param string $key |
||
66 | * @return string |
||
67 | */ |
||
68 | 2 | public function getAppValue($key) { |
|
71 | |||
72 | /** |
||
73 | * Evaluate if all needed redis related settings are set. |
||
74 | * |
||
75 | * @return boolean |
||
76 | */ |
||
77 | 4 | public function evaluateRedisSettings() { |
|
88 | |||
89 | /** |
||
90 | * Set a value for the given key. |
||
91 | * |
||
92 | * @param string $key |
||
93 | * @param string $value |
||
94 | * @return string |
||
95 | * @throws NotFoundException |
||
96 | */ |
||
97 | 21 | public function setAppValue($key, $value) { |
|
117 | |||
118 | /** |
||
119 | * Checks for the right languages format. |
||
120 | * |
||
121 | * @param string $value |
||
122 | * @throws NotFoundException |
||
123 | */ |
||
124 | 5 | private function checkLanguages($value) { |
|
130 | |||
131 | /** |
||
132 | * Checks for the right Redis host format. |
||
133 | * |
||
134 | * @param string $value |
||
135 | * @throws NotFoundException |
||
136 | */ |
||
137 | 6 | private function checkRedisHost($value) { |
|
144 | |||
145 | /** |
||
146 | * Checks for the right Redis port format. |
||
147 | * |
||
148 | * @param string $value |
||
149 | * @throws NotFoundException |
||
150 | */ |
||
151 | 5 | private function checkRedisPort($value) { |
|
156 | |||
157 | /** |
||
158 | * Checks for the right Redis DB format. |
||
159 | * |
||
160 | * @param string $value |
||
161 | * @throws NotFoundException |
||
162 | */ |
||
163 | 3 | private function checkRedisDb($value) { |
|
168 | } |
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.