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 | public $value; |
||
16 | public $selected; |
||
17 | public $path = 'uploads'; |
||
18 | public $size; |
||
19 | public $emptyselect; |
||
20 | public $type; |
||
21 | public $prefix; |
||
22 | public $suffix; |
||
23 | |||
24 | /** |
||
25 | * @param string $path |
||
26 | * @param null $value |
||
27 | * @param string $selected |
||
28 | * @param int $size |
||
29 | * @param int $emptyselect |
||
30 | * @param int $type |
||
31 | * @param string $prefix |
||
32 | * @param string $suffix |
||
33 | */ |
||
34 | public function __construct( |
||
51 | |||
52 | /** |
||
53 | * @param array $this_array |
||
54 | * |
||
55 | * @return string |
||
56 | */ |
||
57 | public function getarray($this_array) |
||
75 | |||
76 | /** |
||
77 | * Private to be called by other parts of the class |
||
78 | * @param $dirname |
||
79 | * @return array |
||
80 | */ |
||
81 | public function getDirListAsArray($dirname) |
||
99 | |||
100 | /** |
||
101 | * @param $dirname |
||
102 | * @param string $type |
||
103 | * @param string $prefix |
||
104 | * @param int $noselection |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | public static function getListTypeAsArray($dirname, $type = '', $prefix = '', $noselection = 1) |
||
154 | |||
155 | /** |
||
156 | * @param int $type |
||
157 | * @param $selected |
||
158 | * |
||
159 | * @return mixed |
||
160 | */ |
||
161 | public static function getForum($type = 1, $selected) |
||
197 | |||
198 | /** |
||
199 | * @return null |
||
200 | */ |
||
201 | public function getValue() |
||
205 | |||
206 | public function getSelected() |
||
210 | |||
211 | /** |
||
212 | * @return string |
||
213 | */ |
||
214 | public function getPath() |
||
218 | |||
219 | /** |
||
220 | * @return int |
||
221 | */ |
||
222 | public function getSize() |
||
226 | |||
227 | /** |
||
228 | * @return int |
||
229 | */ |
||
230 | public function getEmptySelect() |
||
234 | |||
235 | /** |
||
236 | * @return int |
||
237 | */ |
||
238 | public function getType() |
||
242 | |||
243 | public function getPrefix() |
||
247 | |||
248 | public function getSuffix() |
||
252 | } |
||
253 |
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.