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 wflLists 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 wflLists, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class wflLists |
||
|
|
|||
| 14 | { |
||
| 15 | var $value; |
||
| 16 | var $selected; |
||
| 17 | var $path='uploads'; |
||
| 18 | var $size; |
||
| 19 | var $emptyselect; |
||
| 20 | var $type; |
||
| 21 | var $prefix; |
||
| 22 | var $suffix; |
||
| 23 | |||
| 24 | function __construct($path="uploads", $value = null, $selected='', $size = 1, $emptyselect = 0, $type = 0, $prefix='', $suffix='') |
||
| 25 | { |
||
| 26 | $this -> value = $value; |
||
| 27 | $this -> selection = $selected; |
||
| 28 | $this -> path = $path; |
||
| 29 | $this -> size = intval($size); |
||
| 30 | $this -> emptyselect = ($emptyselect) ? 0 : 1; |
||
| 31 | $this -> type = $type; |
||
| 32 | } |
||
| 33 | |||
| 34 | View Code Duplication | function &getarray($this_array) { |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Private to be called by other parts of the class |
||
| 54 | */ |
||
| 55 | function &getDirListAsArray($dirname) { |
||
| 72 | |||
| 73 | function &getListTypeAsArray($dirname, $type='', $prefix="", $noselection = 1) { |
||
| 114 | |||
| 115 | function &getForum( $type = 1, $selected ) { |
||
| 150 | |||
| 151 | function value() |
||
| 155 | |||
| 156 | function selected() |
||
| 160 | |||
| 161 | function paths() |
||
| 165 | |||
| 166 | function size() |
||
| 170 | |||
| 171 | function emptyselect() |
||
| 175 | |||
| 176 | function type() |
||
| 180 | |||
| 181 | function prefix() |
||
| 185 | |||
| 186 | function suffix() |
||
| 190 | } |
||
| 191 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.