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 DataContainer 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 DataContainer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class DataContainer implements \ArrayAccess, \IteratorAggregate, \Countable |
||
21 | { |
||
22 | /** |
||
23 | * @var DataContainer parent container, for inheritance |
||
24 | * @since 2.0.0 |
||
25 | */ |
||
26 | protected $parent; |
||
27 | |||
28 | /** |
||
29 | * @var bool wether we want to use parent cascading |
||
30 | * @since 2.0.0 |
||
31 | */ |
||
32 | protected $parentEnabled = false; |
||
33 | |||
34 | /** |
||
35 | * @var array container data |
||
36 | * @since 2.0.0 |
||
37 | */ |
||
38 | protected $data = array(); |
||
39 | |||
40 | /** |
||
41 | * @var bool wether the container is read-only |
||
42 | * @since 2.0.0 |
||
43 | */ |
||
44 | protected $readOnly = false; |
||
45 | |||
46 | /** |
||
47 | * @var bool wether the container data has been modified |
||
48 | * @since 2.0.0 |
||
49 | */ |
||
50 | protected $isModified = false; |
||
51 | |||
52 | /** |
||
53 | * Constructor |
||
54 | * |
||
55 | * @param array $data container data |
||
56 | * @param boolean $readOnly wether the container is read-only |
||
57 | * @since 2.0.0 |
||
58 | */ |
||
59 | public function __construct(array $data = array(), $readOnly = false) |
||
64 | |||
65 | /** |
||
66 | * Get the parent of this container |
||
67 | * |
||
68 | * @return DataContainer |
||
69 | * @since 2.0.0 |
||
70 | */ |
||
71 | public function getParent() |
||
75 | |||
76 | /** |
||
77 | * Set the parent of this container, to support inheritance |
||
78 | * |
||
79 | * @param DataContainer $parent the parent container object |
||
80 | * @return $this |
||
81 | * @since 2.0.0 |
||
82 | */ |
||
83 | public function setParent(DataContainer $parent = null) |
||
98 | |||
99 | /** |
||
100 | * Enable the use of the parent object, if set |
||
101 | * |
||
102 | * @return $this |
||
103 | * @since 2.0.0 |
||
104 | */ |
||
105 | public function enableParent() |
||
114 | |||
115 | /** |
||
116 | * Disable the use of the parent object |
||
117 | * |
||
118 | * @return $this |
||
119 | * @since 2.0.0 |
||
120 | */ |
||
121 | public function disableParent() |
||
127 | |||
128 | /** |
||
129 | * Check whether or not this container has an active parent |
||
130 | * |
||
131 | * @return bool |
||
132 | * @since 2.0.0 |
||
133 | */ |
||
134 | public function hasParent() |
||
138 | |||
139 | /** |
||
140 | * Retrieve the modified state of the container |
||
141 | * |
||
142 | * @return bool |
||
143 | * @since 2.0.0 |
||
144 | */ |
||
145 | public function isModified() |
||
149 | |||
150 | /** |
||
151 | * Replace the container's data. |
||
152 | * |
||
153 | * @param array $data new data |
||
154 | * @return $this |
||
155 | * @throws RuntimeException |
||
156 | * @since 2.0.0 |
||
157 | */ |
||
158 | public function setContents(array $data) |
||
171 | |||
172 | /** |
||
173 | * Get the container's data |
||
174 | * |
||
175 | * @return array container's data |
||
176 | * @since 2.0.0 |
||
177 | */ |
||
178 | public function getContents() |
||
189 | |||
190 | /** |
||
191 | * Set wether the container is read-only. |
||
192 | * |
||
193 | * @param boolean $readOnly wether it's a read-only container |
||
194 | * @return $this |
||
195 | * @since 2.0.0 |
||
196 | */ |
||
197 | public function setReadOnly($readOnly = true) |
||
203 | |||
204 | /** |
||
205 | * Merge arrays into the container. |
||
206 | * |
||
207 | * @param array $arg array to merge with |
||
208 | * @return $this |
||
209 | * @throws RuntimeException |
||
210 | * @since 2.0.0 |
||
211 | */ |
||
212 | public function merge($arg) |
||
237 | |||
238 | /** |
||
239 | * Check wether the container is read-only. |
||
240 | * |
||
241 | * @return boolean $readOnly wether it's a read-only container |
||
242 | * @since 2.0.0 |
||
243 | */ |
||
244 | public function isReadOnly() |
||
248 | |||
249 | /** |
||
250 | * isset magic method |
||
251 | */ |
||
252 | public function __isset($key) |
||
256 | |||
257 | /** |
||
258 | * Check if a key was set upon this bag's data |
||
259 | * |
||
260 | * @param string $key |
||
261 | * @return bool |
||
262 | * @since 2.0.0 |
||
263 | */ |
||
264 | public function has($key) |
||
275 | |||
276 | /** |
||
277 | * get magic method |
||
278 | */ |
||
279 | public function __get($key) |
||
283 | |||
284 | /** |
||
285 | * Get a key's value from this bag's data |
||
286 | * |
||
287 | * @param string $key |
||
288 | * @param mixed $default |
||
289 | * @return mixed |
||
290 | * @since 2.0.0 |
||
291 | */ |
||
292 | public function get($key = null, $default = null) |
||
312 | |||
313 | /** |
||
314 | * set magic method |
||
315 | */ |
||
316 | public function __set($key, $value) |
||
320 | |||
321 | /** |
||
322 | * Set a config value |
||
323 | * |
||
324 | * @param string $key |
||
325 | * @param mixed $value |
||
326 | * @throws \RuntimeException |
||
327 | * @since 2.0.0 |
||
328 | */ |
||
329 | View Code Duplication | public function set($key, $value) |
|
349 | |||
350 | /** |
||
351 | * Delete data from the container |
||
352 | * |
||
353 | * @param string $key key to delete |
||
354 | * @return boolean delete success boolean |
||
355 | * @since 2.0.0 |
||
356 | */ |
||
357 | View Code Duplication | public function delete($key) |
|
373 | |||
374 | /** |
||
375 | * Allow usage of isset() on the param bag as an array |
||
376 | * |
||
377 | * @param string $key |
||
378 | * @return bool |
||
379 | * @since 2.0.0 |
||
380 | */ |
||
381 | public function offsetExists($key) |
||
385 | |||
386 | /** |
||
387 | * Allow fetching values as an array |
||
388 | * |
||
389 | * @param string $key |
||
390 | * @return mixed |
||
391 | * @throws OutOfBoundsException |
||
392 | * @since 2.0.0 |
||
393 | */ |
||
394 | public function offsetGet($key) |
||
401 | |||
402 | /** |
||
403 | * Disallow setting values like an array |
||
404 | * |
||
405 | * @param string $key |
||
406 | * @param mixed $value |
||
407 | * @since 2.0.0 |
||
408 | */ |
||
409 | public function offsetSet($key, $value) |
||
413 | |||
414 | /** |
||
415 | * Disallow unsetting values like an array |
||
416 | * |
||
417 | * @param string $key |
||
418 | * @throws RuntimeException |
||
419 | * @since 2.0.0 |
||
420 | */ |
||
421 | public function offsetUnset($key) |
||
425 | |||
426 | /** |
||
427 | * IteratorAggregate implementation |
||
428 | * |
||
429 | * @return IteratorAggregate iterator |
||
430 | * @since 2.0.0 |
||
431 | */ |
||
432 | public function getIterator() |
||
436 | |||
437 | /** |
||
438 | * Countable implementation |
||
439 | * |
||
440 | * @return int number of items stored in the container |
||
441 | * @since 2.0.0 |
||
442 | */ |
||
443 | public function count() |
||
447 | } |
||
448 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.