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 |
||
14 | class Configuration implements ConfigurationInterface |
||
15 | { |
||
16 | /** |
||
17 | * {@inheritDoc} |
||
18 | */ |
||
19 | public function getConfigTreeBuilder() |
||
34 | |||
35 | /** |
||
36 | * Creates a root config node with the provided key. |
||
37 | * |
||
38 | * @param string $key |
||
39 | * @return \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition |
||
40 | */ |
||
41 | private function createRootNode($key) |
||
46 | |||
47 | /** |
||
48 | * Formats the root REST endpoint. |
||
49 | * |
||
50 | * @param string $endpoint |
||
51 | * @return string |
||
52 | */ |
||
53 | private function formatRestEndpoint($endpoint) |
||
57 | |||
58 | /** |
||
59 | * Gets the api adapter configuration node. |
||
60 | * |
||
61 | * @return \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition |
||
62 | */ |
||
63 | View Code Duplication | private function getAdapterNode() |
|
83 | |||
84 | /** |
||
85 | * Gets the metadata configuration node. |
||
86 | * |
||
87 | * @return \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition |
||
88 | */ |
||
89 | private function getMetadataNode() |
||
148 | |||
149 | /** |
||
150 | * Gets the persisters configuration node. |
||
151 | * |
||
152 | * @return \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition |
||
153 | */ |
||
154 | View Code Duplication | private function getPersistersNode() |
|
182 | |||
183 | /** |
||
184 | * Gets the rest configuration node. |
||
185 | * |
||
186 | * @return \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition |
||
187 | */ |
||
188 | View Code Duplication | private function getRestNode() |
|
207 | |||
208 | /** |
||
209 | * Gets the schema indices configuration node. |
||
210 | * |
||
211 | * @return \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition |
||
212 | */ |
||
213 | private function getSchemaIndicesNode() |
||
241 | |||
242 | /** |
||
243 | * Gets the search clients configuration node. |
||
244 | * |
||
245 | * @return \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition |
||
246 | */ |
||
247 | View Code Duplication | private function getSearchClientsNode() |
|
275 | |||
276 | /** |
||
277 | * Validates the api adapter config. |
||
278 | * |
||
279 | * @param array $adapter |
||
280 | * @return self |
||
281 | * @throws InvalidConfigurationException |
||
282 | */ |
||
283 | private function validateAdapter(array $adapter) |
||
295 | |||
296 | /** |
||
297 | * Validates that a library class name exists. |
||
298 | * |
||
299 | * @param string $subClass |
||
300 | * @param string $path |
||
301 | * @throws InvalidConfigurationException |
||
302 | */ |
||
303 | private function validateLibClassExists($subClass, $path) |
||
310 | |||
311 | /** |
||
312 | * Validates the metadata cache config. |
||
313 | * |
||
314 | * @param array $config |
||
315 | * @return self |
||
316 | * @throws InvalidConfigurationException |
||
317 | */ |
||
318 | private function validateMetadataCache(array $config) |
||
334 | |||
335 | /** |
||
336 | * Validates the Redis metadata cache config. |
||
337 | * |
||
338 | * @param array $config |
||
339 | * @throws InvalidConfigurationException |
||
340 | */ |
||
341 | private function validateMetadataCacheRedis(array $config) |
||
347 | |||
348 | /** |
||
349 | * Validates the metadata drivers config. |
||
350 | * |
||
351 | * @param array $drivers |
||
352 | * @return self |
||
353 | * @throws InvalidConfigurationException |
||
354 | */ |
||
355 | private function validateMetadataDrivers(array $drivers) |
||
362 | |||
363 | /** |
||
364 | * Validates the MongoDb persister config. |
||
365 | * |
||
366 | * @param array $config |
||
367 | * @throws InvalidConfigurationException |
||
368 | */ |
||
369 | private function validatePersisterMongoDb(array $config) |
||
375 | |||
376 | /** |
||
377 | * Validates the persisters config. |
||
378 | * |
||
379 | * @param array $persisters |
||
380 | * @return self |
||
381 | * @throws InvalidConfigurationException |
||
382 | */ |
||
383 | private function validatePersisters(array $persisters) |
||
397 | |||
398 | /** |
||
399 | * Validates the schema indices config. |
||
400 | * |
||
401 | * @param array $indices |
||
402 | * @return self |
||
403 | * @throws InvalidConfigurationException |
||
404 | */ |
||
405 | private function validateSchemaIndices(array $indices) |
||
425 | |||
426 | /** |
||
427 | * Validates the search clients config. |
||
428 | * |
||
429 | * @param array $clients |
||
430 | * @return self |
||
431 | * @throws InvalidConfigurationException |
||
432 | */ |
||
433 | private function validateSearchClients(array $clients) |
||
446 | |||
447 | /** |
||
448 | * Validates a configuration that uses type and service as options. |
||
449 | * |
||
450 | * @param array $config |
||
451 | * @param string $path |
||
452 | * @throws InvalidArgumentException |
||
453 | */ |
||
454 | private function validateTypeAndService(array $config, $path) |
||
463 | } |
||
464 |
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.