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:
Complex classes like Config often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Config, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Config implements Countable, Iterator, ArrayAccess |
||
14 | { |
||
15 | /** |
||
16 | * Whether modifications to configuration data are allowed. |
||
17 | * |
||
18 | * @var bool |
||
19 | */ |
||
20 | protected $allowModifications; |
||
21 | |||
22 | /** |
||
23 | * All of the configuration items. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $data = []; |
||
28 | |||
29 | /** |
||
30 | * Used when unsetting values during iteration to ensure we do not skip |
||
31 | * the next element. |
||
32 | * |
||
33 | * @var bool |
||
34 | */ |
||
35 | protected $skipNextIteration; |
||
36 | |||
37 | /** |
||
38 | * Create a new configuration repository. |
||
39 | * |
||
40 | * @param array $array |
||
41 | * @param bool $allowModifications |
||
42 | */ |
||
43 | 1 | public function __construct(array $array = [], $allowModifications = false) |
|
51 | |||
52 | /** |
||
53 | * @param $name |
||
54 | * @param $value |
||
55 | * @return $this |
||
56 | */ |
||
57 | 1 | protected function setDataItem($name, $value) |
|
70 | |||
71 | /** |
||
72 | * @param $name |
||
73 | * @return string |
||
74 | */ |
||
75 | public function __get($name) |
||
79 | |||
80 | /** |
||
81 | * Retrieve a value and return $default if there is no element set. |
||
82 | * |
||
83 | * @param string $key |
||
84 | * @param string $default |
||
85 | * @return mixed |
||
86 | */ |
||
87 | 1 | public function get($key, $default = null) |
|
97 | |||
98 | /** |
||
99 | * @param string $key |
||
100 | * @return mixed|null |
||
101 | */ |
||
102 | 1 | public function getByKey($key) |
|
110 | |||
111 | /** |
||
112 | * Determine if the given configuration value exists. |
||
113 | * |
||
114 | * @param string $key |
||
115 | * @return bool |
||
116 | */ |
||
117 | 1 | public function hasByKey($key) |
|
121 | |||
122 | /** |
||
123 | * @param string $path |
||
124 | * @return string |
||
125 | */ |
||
126 | 1 | View Code Duplication | protected function getByPath($path) |
140 | |||
141 | /** |
||
142 | * @param $key |
||
143 | * @return bool |
||
144 | */ |
||
145 | public function __isset($key) |
||
149 | |||
150 | /** |
||
151 | * Determine if the given configuration value exists. |
||
152 | * |
||
153 | * @param string $key |
||
154 | * @return bool |
||
155 | */ |
||
156 | 1 | public function has($key) |
|
164 | |||
165 | /** |
||
166 | * @param string $path |
||
167 | * @return bool |
||
168 | */ |
||
169 | 1 | View Code Duplication | public function hasByPath($path) |
183 | |||
184 | /** |
||
185 | * Determine if the given configuration option exists. |
||
186 | * |
||
187 | * @param string $key |
||
188 | * @return bool |
||
189 | */ |
||
190 | public function offsetExists($key) |
||
194 | |||
195 | /** |
||
196 | * Get a configuration option. |
||
197 | * |
||
198 | * @param string $key |
||
199 | * @return mixed |
||
200 | */ |
||
201 | public function offsetGet($key) |
||
205 | |||
206 | /** |
||
207 | * Set a configuration option. |
||
208 | * |
||
209 | * @param string $key |
||
210 | * @param mixed $value |
||
211 | * @return void |
||
212 | */ |
||
213 | public function offsetSet($key, $value) |
||
217 | |||
218 | /** |
||
219 | * @param $name |
||
220 | * @param $value |
||
221 | * @return $this |
||
222 | * @throws Exception\RuntimeException |
||
223 | */ |
||
224 | public function set($name, $value) |
||
232 | |||
233 | /** |
||
234 | * Returns whether this Config object is read only or not. |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | public function isReadOnly() |
||
242 | |||
243 | /** |
||
244 | * Unset a configuration option. |
||
245 | * |
||
246 | * @param string $key |
||
247 | * @return void |
||
248 | */ |
||
249 | public function offsetUnset($key) |
||
253 | |||
254 | /** |
||
255 | * @param string $path |
||
256 | * @return $this |
||
257 | */ |
||
258 | 1 | public function mergeFile($path) |
|
264 | |||
265 | /** |
||
266 | * Merge another Config with this one. |
||
267 | * |
||
268 | * For duplicate keys, the following will be performed: |
||
269 | * - Nested Configs will be recursively merged. |
||
270 | * - Items in $merge with INTEGER keys will be appended. |
||
271 | * - Items in $merge with STRING keys will overwrite current values. |
||
272 | * |
||
273 | * @param self $merge |
||
274 | * @return $this |
||
275 | */ |
||
276 | 1 | public function merge(self $merge) |
|
303 | |||
304 | /** |
||
305 | * Return an associative array of the stored data. |
||
306 | * |
||
307 | * @return array |
||
308 | */ |
||
309 | 1 | public function toArray() |
|
324 | |||
325 | /** |
||
326 | * count(): defined by Countable interface. |
||
327 | * |
||
328 | * @see Countable::count() |
||
329 | * @return int |
||
330 | */ |
||
331 | public function count() |
||
335 | |||
336 | /** |
||
337 | * current(): defined by Iterator interface. |
||
338 | * |
||
339 | * @see Iterator::current() |
||
340 | * @return mixed |
||
341 | */ |
||
342 | 1 | public function current() |
|
348 | |||
349 | /** |
||
350 | * next(): defined by Iterator interface. |
||
351 | * |
||
352 | * @see Iterator::next() |
||
353 | * @return void |
||
354 | */ |
||
355 | 1 | public function next() |
|
364 | |||
365 | /** |
||
366 | * rewind(): defined by Iterator interface. |
||
367 | * |
||
368 | * @see Iterator::rewind() |
||
369 | * @return void |
||
370 | */ |
||
371 | 1 | public function rewind() |
|
376 | |||
377 | /** |
||
378 | * valid(): defined by Iterator interface. |
||
379 | * |
||
380 | * @see Iterator::valid() |
||
381 | * @return bool |
||
382 | */ |
||
383 | 1 | public function valid() |
|
387 | |||
388 | /** |
||
389 | * key(): defined by Iterator interface. |
||
390 | * |
||
391 | * @see Iterator::key() |
||
392 | * @return mixed |
||
393 | */ |
||
394 | 1 | public function key() |
|
398 | } |
||
399 |
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.