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 HtmlDropdown 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 HtmlDropdown, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class HtmlDropdown extends HtmlSemDoubleElement { |
||
15 | use LabeledIconTrait { |
||
16 | addIcon as addIconP; |
||
17 | } |
||
18 | protected $mClass="menu"; |
||
19 | protected $mTagName="div"; |
||
20 | protected $items=array (); |
||
21 | protected $_params=array("action"=>"nothing","on"=>"hover"); |
||
22 | protected $input; |
||
23 | |||
24 | public function __construct($identifier, $value="", $items=array()) { |
||
36 | |||
37 | public function addItem($item,$value=NULL,$image=NULL){ |
||
42 | |||
43 | public function addIcon($icon,$before=true,$labeled=false){ |
||
47 | /** |
||
48 | * Insert an item at a position |
||
49 | * @param mixed $item |
||
50 | * @param number $position |
||
51 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
52 | */ |
||
53 | public function insertItem($item,$position=0){ |
||
61 | |||
62 | protected function beforeAddItem($item,$value=NULL,$image=NULL){ |
||
76 | |||
77 | /* (non-PHPdoc) |
||
78 | * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
||
79 | */ |
||
80 | public function fromDatabaseObject($object, $function) { |
||
83 | |||
84 | public function addInput($name){ |
||
90 | |||
91 | View Code Duplication | public function addItems($items){ |
|
102 | |||
103 | public function count(){ |
||
106 | /** |
||
107 | * @param boolean $dropdown |
||
108 | */ |
||
109 | public function asDropdown($dropdown){ |
||
119 | |||
120 | public function setVertical(){ |
||
123 | |||
124 | public function setSimple(){ |
||
127 | |||
128 | public function asButton($floating=false){ |
||
133 | |||
134 | public function asSelect($name=NULL,$multiple=false,$selection=true){ |
||
143 | |||
144 | public function asSearch($name=NULL,$multiple=false,$selection=false){ |
||
148 | |||
149 | public function setSelect($name=NULL,$multiple=false){ |
||
166 | |||
167 | public function asSubmenu($pointing=NULL){ |
||
174 | |||
175 | public function setPointing($value=Direction::NONE){ |
||
178 | |||
179 | public function setValue($value){ |
||
190 | |||
191 | /* |
||
192 | * (non-PHPdoc) |
||
193 | * @see BaseHtml::run() |
||
194 | */ |
||
195 | public function run(JsUtils $js) { |
||
203 | |||
204 | public function setCompact(){ |
||
207 | |||
208 | public function setAction($action){ |
||
211 | |||
212 | public function setFullTextSearch($value){ |
||
215 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.