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 |
||
25 | class Configuration |
||
26 | { |
||
27 | const SEPARATOR = '/[:\.]/'; |
||
28 | |||
29 | /** |
||
30 | * @type array |
||
31 | */ |
||
32 | protected $values = []; |
||
33 | |||
34 | /** |
||
35 | * Create a new configuration and set the default values. |
||
36 | * |
||
37 | * @param mixed $values The values |
||
38 | */ |
||
39 | 69 | public function __construct(array $values = []) |
|
43 | |||
44 | /** |
||
45 | * Get a value by its "dot-noted" path. |
||
46 | * |
||
47 | * @param string $path The dot notation path (or empty for all values) |
||
48 | * @param mixed $default A default value |
||
49 | * |
||
50 | * @return mixed The value if found, else default |
||
51 | */ |
||
52 | 39 | public function get($path = '', $default = null) |
|
69 | |||
70 | /** |
||
71 | * Set a value by its "dot-noted" path. |
||
72 | * |
||
73 | * @param string $path The dot notation path (or empty for all values) |
||
74 | * @param mixed $value The value(s) |
||
75 | * |
||
76 | * @return bool Success |
||
77 | */ |
||
78 | 45 | public function set($path = '', $value) |
|
108 | |||
109 | /** |
||
110 | * Add new configurations to existing conf. |
||
111 | * |
||
112 | * @param string $path |
||
113 | * @param array $values |
||
114 | * |
||
115 | * @return bool |
||
116 | */ |
||
117 | 12 | public function add($path, array $values) |
|
123 | |||
124 | /** |
||
125 | * Check if a configuration exists. |
||
126 | * |
||
127 | * @param string $path |
||
128 | * |
||
129 | * @return bool |
||
130 | */ |
||
131 | 3 | public function have($path) |
|
144 | |||
145 | /** |
||
146 | * Set all the configs. |
||
147 | * |
||
148 | * @param array $values |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | 9 | public function setValues($values) |
|
158 | |||
159 | /** |
||
160 | * Get all the configs. |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | 30 | public function getValues() |
|
168 | |||
169 | /** |
||
170 | * [explode description]. |
||
171 | * |
||
172 | * @param string $path [description] |
||
173 | * |
||
174 | * @return array |
||
175 | */ |
||
176 | 66 | protected function explode($path) |
|
181 | |||
182 | /** |
||
183 | * array_merge_recursive does indeed merge arrays, but it converts values with duplicate |
||
184 | * keys to arrays rather than overwriting the value in the first array with the duplicate |
||
185 | * value in the second array, as array_merge does. I.e., with array_merge_recursive, |
||
186 | * this happens (documented behavior):. |
||
187 | * |
||
188 | * array_merge_recursive(array('key' => 'org value'), array('key' => 'new value')); |
||
189 | * => array('key' => array('org value', 'new value')); |
||
190 | * |
||
191 | * arrayMergeRecursiveDistinct does not change the datatypes of the values in the arrays. |
||
192 | * Matching keys' values in the second array overwrite those in the first array, as is the |
||
193 | * case with array_merge, i.e.: |
||
194 | * |
||
195 | * arrayMergeRecursiveDistinct(array('key' => 'org value'), array('key' => 'new value')); |
||
196 | * => array('key' => array('new value')); |
||
197 | * |
||
198 | * Parameters are passed by reference, though only for performance reasons. They're not |
||
199 | * altered by this function. |
||
200 | * |
||
201 | * If key is integer, it will be merged like array_merge do: |
||
202 | * arrayMergeRecursiveDistinct(array(0 => 'org value'), array(0 => 'new value')); |
||
203 | * => array(0 => 'org value', 1 => 'new value'); |
||
204 | * |
||
205 | * @param array $array1 |
||
206 | * @param array $array2 |
||
207 | * |
||
208 | * @return array |
||
209 | * |
||
210 | * @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk> |
||
211 | * @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com> |
||
212 | * @author Anton Medvedev <anton (at) elfet (dot) ru> |
||
213 | */ |
||
214 | 12 | protected function arrayMergeRecursiveDistinct(array &$array1, array &$array2) |
|
236 | |||
237 | /** |
||
238 | * Loads config from a JSON file. |
||
239 | * |
||
240 | * @param string $filepath Path to the file where to read the config |
||
241 | * |
||
242 | * @return bool |
||
243 | */ |
||
244 | 15 | public function load($filepath) |
|
264 | |||
265 | /** |
||
266 | * Saves config to a JSON file. |
||
267 | * |
||
268 | * @param string $filepath Path to the file where to write the config |
||
269 | * |
||
270 | * @return bool |
||
271 | */ |
||
272 | 3 | public function save($filepath) |
|
276 | } |
||
277 |
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.