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 |
||
15 | class HtmlDropdown extends HtmlSemDoubleElement { |
||
16 | use FieldTrait,LabeledIconTrait { |
||
17 | addIcon as addIconP; |
||
18 | } |
||
19 | protected $mClass="menu"; |
||
20 | protected $mTagName="div"; |
||
21 | protected $items=array (); |
||
22 | protected $_params=array("action"=>"nothing","on"=>"hover"); |
||
23 | protected $input; |
||
24 | protected $value; |
||
25 | protected $_associative; |
||
26 | protected $_multiple; |
||
27 | |||
28 | public function __construct($identifier, $value="", $items=array(),$associative=true) { |
||
50 | |||
51 | public function getField(){ |
||
54 | |||
55 | public function getDataField(){ |
||
58 | |||
59 | public function addItem($item,$value=NULL,$image=NULL,$description=NULL){ |
||
64 | |||
65 | public function addIcon($icon,$before=true,$labeled=false){ |
||
70 | |||
71 | public function addIcons($icons){ |
||
77 | |||
78 | /** |
||
79 | * Insert an item at a position |
||
80 | * @param mixed $item |
||
81 | * @param number $position |
||
82 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
83 | */ |
||
84 | public function insertItem($item,$position=0){ |
||
92 | |||
93 | protected function removeArrow(){ |
||
99 | |||
100 | protected function beforeAddItem($item,$value=NULL,$image=NULL,$description=NULL){ |
||
115 | |||
116 | /* (non-PHPdoc) |
||
117 | * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
||
118 | */ |
||
119 | public function fromDatabaseObject($object, $function) { |
||
122 | |||
123 | public function addInput($name){ |
||
129 | |||
130 | /** |
||
131 | * Adds a search input item |
||
132 | * @param string $placeHolder |
||
133 | * @param string $icon |
||
134 | * @return \Ajax\semantic\html\content\HtmlDropdownItem |
||
135 | */ |
||
136 | public function addSearchInputItem($placeHolder=NULL,$icon=NULL){ |
||
139 | |||
140 | /** |
||
141 | * Adds a divider item |
||
142 | * @return \Ajax\semantic\html\content\HtmlDropdownItem |
||
143 | */ |
||
144 | public function addDividerItem(){ |
||
147 | |||
148 | /** |
||
149 | * Adds an header item |
||
150 | * @param string $caption |
||
151 | * @param string $icon |
||
152 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
153 | */ |
||
154 | public function addHeaderItem($caption=NULL,$icon=NULL){ |
||
157 | |||
158 | /** |
||
159 | * Adds an item with a circular label |
||
160 | * @param string $caption |
||
161 | * @param string $color |
||
162 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
163 | */ |
||
164 | public function addCircularLabelItem($caption,$color){ |
||
167 | |||
168 | /** |
||
169 | * @param string $caption |
||
170 | * @param string $image |
||
171 | * @return \Ajax\semantic\html\content\HtmlDropdownItem |
||
172 | */ |
||
173 | public function addMiniAvatarImageItem($caption,$image){ |
||
176 | |||
177 | public function addItems($items){ |
||
188 | |||
189 | /** |
||
190 | * Sets the values of a property for each item in the collection |
||
191 | * @param string $property |
||
192 | * @param array $values |
||
193 | * @return HtmlCollection |
||
194 | */ |
||
195 | View Code Duplication | public function setPropertyValues($property,$values){ |
|
211 | |||
212 | public function getItem($index){ |
||
215 | |||
216 | /** |
||
217 | * @return int |
||
218 | */ |
||
219 | public function count(){ |
||
222 | /** |
||
223 | * @param boolean $dropdown |
||
224 | */ |
||
225 | public function asDropdown($dropdown){ |
||
235 | |||
236 | public function setVertical(){ |
||
239 | |||
240 | public function setInline(){ |
||
243 | |||
244 | public function setSimple(){ |
||
247 | |||
248 | public function asButton($floating=false){ |
||
255 | |||
256 | public function asSelect($name=NULL,$multiple=false,$selection=true){ |
||
269 | |||
270 | public function asSearch($name=NULL,$multiple=false,$selection=true){ |
||
274 | |||
275 | public function setSelect($name=NULL,$multiple=false){ |
||
292 | |||
293 | public function asSubmenu($pointing=NULL){ |
||
300 | |||
301 | public function setPointing($value=Direction::NONE){ |
||
304 | |||
305 | public function setValue($value){ |
||
321 | |||
322 | /* |
||
323 | * (non-PHPdoc) |
||
324 | * @see BaseHtml::run() |
||
325 | */ |
||
326 | public function run(JsUtils $js) { |
||
336 | |||
337 | public function setCompact(){ |
||
340 | |||
341 | public function setAction($action){ |
||
344 | |||
345 | public function setFullTextSearch($value){ |
||
348 | |||
349 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
353 | |||
354 | public function getInput() { |
||
360 | |||
361 | public function setIcon($icon="dropdown"){ |
||
365 | |||
366 | public function jsAddItem($caption){ |
||
370 | } |
||
371 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: