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 IndexConfigurationRepository |
||
11 | { |
||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | private $config; |
||
16 | |||
17 | /** |
||
18 | * @param array $config |
||
19 | */ |
||
20 | 16 | public function __construct(array $config = array()) |
|
24 | |||
25 | /** |
||
26 | * Get all configuration for an index |
||
27 | * |
||
28 | * @param string $index |
||
29 | * |
||
30 | * @return array|null |
||
31 | */ |
||
32 | 11 | public function get($index) |
|
40 | |||
41 | /** |
||
42 | * Get settings for an index |
||
43 | * |
||
44 | * @param string $index |
||
45 | * |
||
46 | * @return array|null |
||
47 | */ |
||
48 | 3 | View Code Duplication | public function getSettings($index) |
62 | |||
63 | /** |
||
64 | * Get mappings for an index |
||
65 | * |
||
66 | * @param string $index |
||
67 | * |
||
68 | * @return array|null |
||
69 | */ |
||
70 | 4 | View Code Duplication | public function getMappings($index) |
80 | |||
81 | /** |
||
82 | * Get mapping for a type |
||
83 | * |
||
84 | * @param string $index |
||
85 | * @param string $type |
||
86 | * |
||
87 | * @return array|null |
||
88 | */ |
||
89 | 3 | public function getMapping($index, $type) |
|
103 | } |
||
104 |
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.