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 ZendConfig 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 ZendConfig, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class ZendConfig implements \Countable, \Iterator |
||
35 | { |
||
36 | /** |
||
37 | * Whether in-memory modifications to configuration data are allowed |
||
38 | * |
||
39 | * @var boolean |
||
40 | */ |
||
41 | protected $allowModifications; |
||
42 | |||
43 | /** |
||
44 | * Iteration index |
||
45 | * |
||
46 | * @var integer |
||
47 | */ |
||
48 | protected $index; |
||
49 | |||
50 | /** |
||
51 | * Number of elements in configuration data |
||
52 | * |
||
53 | * @var integer |
||
54 | */ |
||
55 | protected $count; |
||
56 | |||
57 | /** |
||
58 | * Contains array of configuration data |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $data; |
||
63 | |||
64 | /** |
||
65 | * Used when unsetting values during iteration to ensure we do not skip |
||
66 | * the next element |
||
67 | * |
||
68 | * @var boolean |
||
69 | */ |
||
70 | protected $skipNextIteration; |
||
71 | |||
72 | /** |
||
73 | * Contains which config file sections were loaded. This is null |
||
74 | * if all sections were loaded, a string name if one section is loaded |
||
75 | * and an array of string names if multiple sections were loaded. |
||
76 | * |
||
77 | * @var mixed |
||
78 | */ |
||
79 | protected $loadedSection; |
||
80 | |||
81 | /** |
||
82 | * This is used to track section inheritance. The keys are names of sections that |
||
83 | * extend other sections, and the values are the extended sections. |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | protected $extends = array(); |
||
88 | |||
89 | /** |
||
90 | * Load file error string. |
||
91 | * |
||
92 | * Is null if there was no error while file loading |
||
93 | * |
||
94 | * @var string |
||
95 | */ |
||
96 | protected $loadFileErrorStr = null; |
||
97 | |||
98 | /** |
||
99 | * Zend_Config provides a property based interface to |
||
100 | * an array. The data are read-only unless $allowModifications |
||
101 | * is set to true on construction. |
||
102 | * |
||
103 | * Zend_Config also implements Countable and Iterator to |
||
104 | * facilitate easy access to the data. |
||
105 | * |
||
106 | * @param array $array |
||
107 | * @param boolean $allowModifications |
||
108 | * @return void |
||
|
|||
109 | */ |
||
110 | public function __construct(array $array, $allowModifications = false) |
||
125 | |||
126 | /** |
||
127 | * Retrieve a value and return $default if there is no element set. |
||
128 | * |
||
129 | * @param string $name |
||
130 | * @param mixed $default |
||
131 | * @return mixed |
||
132 | */ |
||
133 | public function get($name, $default = null) |
||
141 | |||
142 | /** |
||
143 | * Magic function so that $obj->value will work. |
||
144 | * |
||
145 | * @param string $name |
||
146 | * @return mixed |
||
147 | */ |
||
148 | public function __get($name) |
||
152 | |||
153 | /** |
||
154 | * Only allow setting of a property if $allowModifications |
||
155 | * was set to true on construction. Otherwise, throw an exception. |
||
156 | * |
||
157 | * @param string $name |
||
158 | * @param mixed $value |
||
159 | * @throws Exception |
||
160 | * @return void |
||
161 | */ |
||
162 | public function __set($name, $value) |
||
175 | |||
176 | /** |
||
177 | * Deep clone of this instance to ensure that nested Zend_Configs |
||
178 | * are also cloned. |
||
179 | * |
||
180 | * @return void |
||
181 | */ |
||
182 | View Code Duplication | public function __clone() |
|
194 | |||
195 | /** |
||
196 | * Return an associative array of the stored data. |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | View Code Duplication | public function toArray() |
|
213 | |||
214 | /** |
||
215 | * Support isset() overloading on PHP 5.1 |
||
216 | * |
||
217 | * @param string $name |
||
218 | * @return boolean |
||
219 | */ |
||
220 | public function __isset($name) |
||
224 | |||
225 | /** |
||
226 | * Support unset() overloading on PHP 5.1 |
||
227 | * |
||
228 | * @param string $name |
||
229 | * @throws Exception |
||
230 | * @return void |
||
231 | */ |
||
232 | public function __unset($name) |
||
242 | |||
243 | /** |
||
244 | * Defined by Countable interface |
||
245 | * |
||
246 | * @return int |
||
247 | */ |
||
248 | public function count() |
||
252 | |||
253 | /** |
||
254 | * Defined by Iterator interface |
||
255 | * |
||
256 | * @return mixed |
||
257 | */ |
||
258 | public function current() |
||
263 | |||
264 | /** |
||
265 | * Defined by Iterator interface |
||
266 | * |
||
267 | * @return mixed |
||
268 | */ |
||
269 | public function key() |
||
273 | |||
274 | /** |
||
275 | * Defined by Iterator interface |
||
276 | * |
||
277 | */ |
||
278 | public function next() |
||
287 | |||
288 | /** |
||
289 | * Defined by Iterator interface |
||
290 | * |
||
291 | */ |
||
292 | public function rewind() |
||
298 | |||
299 | /** |
||
300 | * Defined by Iterator interface |
||
301 | * |
||
302 | * @return boolean |
||
303 | */ |
||
304 | public function valid() |
||
308 | |||
309 | /** |
||
310 | * Returns the section name(s) loaded. |
||
311 | * |
||
312 | * @return mixed |
||
313 | */ |
||
314 | public function getSectionName() |
||
321 | |||
322 | /** |
||
323 | * Returns true if all sections were loaded |
||
324 | * |
||
325 | * @return boolean |
||
326 | */ |
||
327 | public function areAllSectionsLoaded() |
||
331 | |||
332 | |||
333 | /** |
||
334 | * Merge another Zend_Config with this one. The items |
||
335 | * in $merge will override the same named items in |
||
336 | * the current config. |
||
337 | * |
||
338 | * @param ZendConfig $merge |
||
339 | * @return ZendConfig |
||
340 | */ |
||
341 | public function merge(ZendConfig $merge) |
||
361 | |||
362 | /** |
||
363 | * Prevent any more modifications being made to this instance. Useful |
||
364 | * after merge() has been used to merge multiple Zend_Config objects |
||
365 | * into one object which should then not be modified again. |
||
366 | * |
||
367 | */ |
||
368 | public function setReadOnly() |
||
377 | |||
378 | /** |
||
379 | * Returns if this Zend_Config object is read only or not. |
||
380 | * |
||
381 | * @return boolean |
||
382 | */ |
||
383 | public function readOnly() |
||
387 | |||
388 | /** |
||
389 | * Get the current extends |
||
390 | * |
||
391 | * @return array |
||
392 | */ |
||
393 | public function getExtends() |
||
397 | |||
398 | /** |
||
399 | * Set an extend for Zend_Config_Writer |
||
400 | * |
||
401 | * @param string $extendingSection |
||
402 | * @param string $extendedSection |
||
403 | * @return void |
||
404 | */ |
||
405 | public function setExtend($extendingSection, $extendedSection = null) |
||
413 | |||
414 | /** |
||
415 | * Throws an exception if $extendingSection may not extend $extendedSection, |
||
416 | * and tracks the section extension if it is valid. |
||
417 | * |
||
418 | * @param string $extendingSection |
||
419 | * @param string $extendedSection |
||
420 | * @throws Exception |
||
421 | * @return void |
||
422 | */ |
||
423 | protected function assertValidExtend($extendingSection, $extendedSection) |
||
436 | |||
437 | /** |
||
438 | * Handle any errors from simplexml_load_file or parse_ini_file |
||
439 | * |
||
440 | * @param integer $errno |
||
441 | * @param string $errstr |
||
442 | * @param string $errfile |
||
443 | * @param integer $errline |
||
444 | */ |
||
445 | public function loadFileErrorHandler($errno, $errstr, $errfile, $errline) |
||
453 | |||
454 | /** |
||
455 | * Merge two arrays recursively, overwriting keys of the same name |
||
456 | * in $firstArray with the value in $secondArray. |
||
457 | * |
||
458 | * @param mixed $firstArray First array |
||
459 | * @param mixed $secondArray Second array to merge into first array |
||
460 | * @return array |
||
461 | */ |
||
462 | protected function arrayMergeRecursive($firstArray, $secondArray) |
||
482 | } |
||
483 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.