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 |
||
46 | class ConfigService { |
||
47 | |||
48 | |||
49 | const FIELDS_LIMIT = 'fields_limit'; |
||
50 | const ELASTIC_HOST = 'elastic_host'; |
||
51 | const ELASTIC_INDEX = 'elastic_index'; |
||
52 | const ELASTIC_VER_BELOW66 = 'es_ver_below66'; |
||
53 | const ANALYZER_TOKENIZER = 'analyzer_tokenizer'; |
||
54 | |||
55 | |||
56 | public $defaults = [ |
||
57 | self::ELASTIC_HOST => '', |
||
58 | self::ELASTIC_INDEX => '', |
||
59 | self::FIELDS_LIMIT => '10000', |
||
60 | self::ELASTIC_VER_BELOW66 => '0', |
||
61 | self::ANALYZER_TOKENIZER => 'standard' |
||
62 | ]; |
||
63 | |||
64 | |||
65 | /** @var IConfig */ |
||
66 | private $config; |
||
67 | |||
68 | /** @var string */ |
||
69 | private $userId; |
||
70 | |||
71 | /** @var MiscService */ |
||
72 | private $miscService; |
||
73 | |||
74 | |||
75 | /** |
||
76 | * ConfigService constructor. |
||
77 | * |
||
78 | * @param IConfig $config |
||
79 | * @param string $userId |
||
80 | * @param MiscService $miscService |
||
81 | */ |
||
82 | public function __construct(IConfig $config, $userId, MiscService $miscService) { |
||
87 | |||
88 | |||
89 | /** |
||
90 | * @return array |
||
91 | */ |
||
92 | public function getConfig(): array { |
||
102 | |||
103 | |||
104 | /** |
||
105 | * @param array $save |
||
106 | */ |
||
107 | public function setConfig(array $save) { |
||
116 | |||
117 | |||
118 | /** |
||
119 | * @return array |
||
120 | * @throws ConfigurationException |
||
121 | */ |
||
122 | public function getElasticHost(): array { |
||
135 | |||
136 | |||
137 | /** |
||
138 | * @return string |
||
139 | * @throws ConfigurationException |
||
140 | */ |
||
141 | public function getElasticIndex(): string { |
||
152 | |||
153 | |||
154 | /** |
||
155 | * Get a value by key |
||
156 | * |
||
157 | * @param string $key |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | View Code Duplication | public function getAppValue(string $key): string { |
|
169 | |||
170 | /** |
||
171 | * Set a value by key |
||
172 | * |
||
173 | * @param string $key |
||
174 | * @param string $value |
||
175 | */ |
||
176 | public function setAppValue(string $key, string $value) { |
||
179 | |||
180 | /** |
||
181 | * remove a key |
||
182 | * |
||
183 | * @param string $key |
||
184 | * |
||
185 | * @return string |
||
186 | */ |
||
187 | public function deleteAppValue(string $key): string { |
||
190 | |||
191 | /** |
||
192 | * Get a user value by key |
||
193 | * |
||
194 | * @param string $key |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | View Code Duplication | public function getUserValue(string $key): string { |
|
208 | |||
209 | /** |
||
210 | * Set a user value by key |
||
211 | * |
||
212 | * @param string $key |
||
213 | * @param string $value |
||
214 | * |
||
215 | * @throws PreConditionNotMetException |
||
216 | */ |
||
217 | public function setUserValue(string $key, string $value) { |
||
220 | |||
221 | /** |
||
222 | * Get a user value by key and user |
||
223 | * |
||
224 | * @param string $userId |
||
225 | * @param string $key |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | public function getValueForUser(string $userId, string $key): string { |
||
232 | |||
233 | /** |
||
234 | * Set a user value by key |
||
235 | * |
||
236 | * @param string $userId |
||
237 | * @param string $key |
||
238 | * @param string $value |
||
239 | * |
||
240 | * @throws PreConditionNotMetException |
||
241 | */ |
||
242 | public function setValueForUser($userId, $key, $value) { |
||
245 | |||
246 | |||
247 | /** |
||
248 | * @return int |
||
249 | */ |
||
250 | public function getNcVersion(): int { |
||
255 | |||
256 | } |
||
257 | |||
258 |
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.