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 each($callBack){ |
||
218 | |||
219 | public function getItem($index){ |
||
222 | |||
223 | /** |
||
224 | * @return int |
||
225 | */ |
||
226 | public function count(){ |
||
229 | /** |
||
230 | * @param boolean $dropdown |
||
231 | */ |
||
232 | public function asDropdown($dropdown){ |
||
242 | |||
243 | public function setVertical(){ |
||
246 | |||
247 | public function setInline(){ |
||
250 | |||
251 | public function setSimple(){ |
||
254 | |||
255 | public function asButton($floating=false){ |
||
262 | |||
263 | public function asSelect($name=NULL,$multiple=false,$selection=true){ |
||
276 | |||
277 | public function asSearch($name=NULL,$multiple=false,$selection=true){ |
||
281 | |||
282 | public function setSelect($name=NULL,$multiple=false){ |
||
299 | |||
300 | public function asSubmenu($pointing=NULL){ |
||
307 | |||
308 | public function setPointing($value=Direction::NONE){ |
||
311 | |||
312 | public function setValue($value){ |
||
328 | |||
329 | /* |
||
330 | * (non-PHPdoc) |
||
331 | * @see BaseHtml::run() |
||
332 | */ |
||
333 | public function run(JsUtils $js) { |
||
343 | |||
344 | public function setCompact(){ |
||
347 | |||
348 | public function setAction($action){ |
||
351 | |||
352 | public function setFullTextSearch($value){ |
||
355 | |||
356 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
360 | |||
361 | public function getInput() { |
||
367 | |||
368 | public function setIcon($icon="dropdown"){ |
||
372 | |||
373 | public function jsAddItem($caption){ |
||
377 | } |
||
378 |
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: