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 FileList 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 FileList, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class FileList |
||
|
|
|||
| 21 | { |
||
| 22 | public $filelist = array(); |
||
| 23 | |||
| 24 | public $value; |
||
| 25 | public $selected; |
||
| 26 | public $path = 'uploads'; |
||
| 27 | public $size; |
||
| 28 | public $emptySelect; |
||
| 29 | public $type; |
||
| 30 | public $prefix; |
||
| 31 | public $suffix; |
||
| 32 | public $noSelection; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * fileList::construct() |
||
| 36 | * |
||
| 37 | * @param string $path |
||
| 38 | * @param null $value |
||
| 39 | * @param string $selected |
||
| 40 | * @param integer $size |
||
| 41 | * |
||
| 42 | * @internal param int $emptySelect |
||
| 43 | * @internal param int $type |
||
| 44 | * @internal param string $prefix |
||
| 45 | * @internal param string $suffix |
||
| 46 | * @return \fileList |
||
| 47 | */ |
||
| 48 | public function __construct($path = 'uploads', $value = null, $selected = '', $size = 1) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * SpotList::setNoSelection() |
||
| 67 | * |
||
| 68 | * @param integer $value |
||
| 69 | * |
||
| 70 | * @return void |
||
| 71 | */ |
||
| 72 | |||
| 73 | public function setEmptySelect($value = 0) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param int $value |
||
| 80 | */ |
||
| 81 | public function setNoSelection($value = 0) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $value |
||
| 88 | */ |
||
| 89 | public function setPrefix($value = '') |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $value |
||
| 96 | */ |
||
| 97 | public function setSuffix($value = '') |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param string $value |
||
| 104 | */ |
||
| 105 | public function setListType($value = 'images') |
||
| 109 | |||
| 110 | /** |
||
| 111 | * SpotList::showSelection() |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | View Code Duplication | public function &showSelection() |
|
| 133 | |||
| 134 | /** |
||
| 135 | * SpotList::getListTypeAsArray() |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | public function &getListTypeAsArray() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return null |
||
| 197 | */ |
||
| 198 | public function value() |
||
| 202 | |||
| 203 | public function isSelected() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public function paths() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return int |
||
| 218 | */ |
||
| 219 | public function size() |
||
| 223 | |||
| 224 | public function isEmptySelect() |
||
| 228 | |||
| 229 | public function getType() |
||
| 233 | |||
| 234 | public function getPrefix() |
||
| 238 | |||
| 239 | public function getSuffix() |
||
| 243 | } |
||
| 244 |
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.