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 |
||
8 | class Config |
||
9 | { |
||
10 | /** |
||
11 | * @var array |
||
12 | */ |
||
13 | protected $config; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $multiValued; |
||
19 | |||
20 | /** |
||
21 | * @param array $config |
||
22 | * @param array $multiValued |
||
23 | */ |
||
24 | 18 | public function __construct(array $config, array $multiValued = []) |
|
29 | |||
30 | /** |
||
31 | * Returns the complete configuration. |
||
32 | * |
||
33 | * @return array |
||
34 | */ |
||
35 | 18 | public function getConfig() |
|
39 | |||
40 | /** |
||
41 | * Returns the multiValued state of fields. |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | public function getMultiValued() |
||
49 | |||
50 | /** |
||
51 | * Checks if a field is multi-valued |
||
52 | * |
||
53 | * @param string $name |
||
54 | * |
||
55 | * @return boolean |
||
56 | */ |
||
57 | 14 | public function isMultiValued($name) |
|
61 | |||
62 | /** |
||
63 | * Checks if a configuration exists for a given field |
||
64 | * |
||
65 | * @param string $name |
||
66 | * |
||
67 | * @return boolean |
||
68 | */ |
||
69 | 18 | public function hasFieldConfig($name) |
|
73 | |||
74 | /** |
||
75 | * Returns the configuration for a field, if it exists |
||
76 | * |
||
77 | * @param string $name |
||
78 | * |
||
79 | * @return array|null |
||
80 | */ |
||
81 | 10 | public function getFieldConfig($name) |
|
87 | |||
88 | /** |
||
89 | * Checks if an key exists for a given field config |
||
90 | * |
||
91 | * @param string $config |
||
92 | * @param integer $key |
||
93 | * |
||
94 | * @return boolean |
||
95 | */ |
||
96 | 10 | View Code Duplication | public function hasFieldConfigKey($config, $key) |
104 | |||
105 | /** |
||
106 | * Checks if a name-value exists for a given field config |
||
107 | * |
||
108 | * @param string $field |
||
109 | * @param string $value |
||
110 | * |
||
111 | * @return boolean |
||
112 | */ |
||
113 | View Code Duplication | public function hasFieldConfigValue($field, $value) |
|
121 | |||
122 | /** |
||
123 | * Returns the name for a field config value |
||
124 | * |
||
125 | * @param string $config |
||
126 | * @param integer $key |
||
127 | * |
||
128 | * @return string|null |
||
129 | */ |
||
130 | View Code Duplication | public function getFieldConfigValueByKey($config, $key) |
|
138 | |||
139 | /** |
||
140 | * Returns the key for a field config name-value |
||
141 | * |
||
142 | * @param string $config |
||
143 | * @param string $value |
||
144 | * |
||
145 | * @return integer|null |
||
146 | */ |
||
147 | View Code Duplication | public function getFieldConfigKey($config, $value) |
|
155 | } |
||
156 |
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.