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 | public function getItem($index){ |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return int |
||
| 195 | */ |
||
| 196 | public function count(){ |
||
| 199 | /** |
||
| 200 | * @param boolean $dropdown |
||
| 201 | */ |
||
| 202 | public function asDropdown($dropdown){ |
||
| 212 | |||
| 213 | public function setVertical(){ |
||
| 216 | |||
| 217 | public function setInline(){ |
||
| 220 | |||
| 221 | public function setSimple(){ |
||
| 224 | |||
| 225 | public function asButton($floating=false){ |
||
| 232 | |||
| 233 | public function asSelect($name=NULL,$multiple=false,$selection=true){ |
||
| 246 | |||
| 247 | public function asSearch($name=NULL,$multiple=false,$selection=true){ |
||
| 251 | |||
| 252 | public function setSelect($name=NULL,$multiple=false){ |
||
| 269 | |||
| 270 | public function asSubmenu($pointing=NULL){ |
||
| 277 | |||
| 278 | public function setPointing($value=Direction::NONE){ |
||
| 281 | |||
| 282 | public function setValue($value){ |
||
| 298 | |||
| 299 | /* |
||
| 300 | * (non-PHPdoc) |
||
| 301 | * @see BaseHtml::run() |
||
| 302 | */ |
||
| 303 | public function run(JsUtils $js) { |
||
| 313 | |||
| 314 | public function setCompact(){ |
||
| 317 | |||
| 318 | public function setAction($action){ |
||
| 321 | |||
| 322 | public function setFullTextSearch($value){ |
||
| 325 | |||
| 326 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
| 330 | |||
| 331 | public function getInput() { |
||
| 337 | |||
| 338 | public function jsAddItem($caption){ |
||
| 342 | } |
||
| 343 |
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: