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 | |||
| 26 | public function __construct($identifier, $value="", $items=array()) { |
||
| 38 | |||
| 39 | public function getField(){ |
||
| 42 | |||
| 43 | public function addItem($item,$value=NULL,$image=NULL,$description=NULL){ |
||
| 48 | |||
| 49 | public function addIcon($icon,$before=true,$labeled=false){ |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Insert an item at a position |
||
| 56 | * @param mixed $item |
||
| 57 | * @param number $position |
||
| 58 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
| 59 | */ |
||
| 60 | public function insertItem($item,$position=0){ |
||
| 68 | |||
| 69 | protected function beforeAddItem($item,$value=NULL,$image=NULL,$description=NULL){ |
||
| 84 | |||
| 85 | /* (non-PHPdoc) |
||
| 86 | * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
||
| 87 | */ |
||
| 88 | public function fromDatabaseObject($object, $function) { |
||
| 91 | |||
| 92 | public function addInput($name){ |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Adds a search input item |
||
| 101 | * @param string $placeHolder |
||
| 102 | * @param string $icon |
||
| 103 | * @return \Ajax\semantic\html\content\HtmlDropdownItem |
||
| 104 | */ |
||
| 105 | public function addSearchInputItem($placeHolder=NULL,$icon=NULL){ |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Adds a divider item |
||
| 111 | * @return \Ajax\semantic\html\content\HtmlDropdownItem |
||
| 112 | */ |
||
| 113 | public function addDividerItem(){ |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Adds an header item |
||
| 119 | * @param string $caption |
||
| 120 | * @param string $icon |
||
| 121 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
| 122 | */ |
||
| 123 | public function addHeaderItem($caption=NULL,$icon=NULL){ |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Adds an item with a circular label |
||
| 129 | * @param string $caption |
||
| 130 | * @param string $color |
||
| 131 | * @return \Ajax\semantic\html\content\HtmlDropdownItem|unknown |
||
| 132 | */ |
||
| 133 | public function addCircularLabelItem($caption,$color){ |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $caption |
||
| 139 | * @param string $image |
||
| 140 | * @return \Ajax\semantic\html\content\HtmlDropdownItem |
||
| 141 | */ |
||
| 142 | public function addMiniAvatarImageItem($caption,$image){ |
||
| 145 | |||
| 146 | public function addItems($items){ |
||
| 147 | if(\is_array($items) && JArray::isAssociative($items)){ |
||
| 148 | foreach ($items as $k=>$v){ |
||
| 149 | $this->addItem($v)->setData($k); |
||
| 150 | } |
||
| 151 | }else{ |
||
| 152 | foreach ($items as $item){ |
||
| 153 | $this->addItem($item); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | public function getItem($index){ |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @return int |
||
| 164 | */ |
||
| 165 | public function count(){ |
||
| 168 | /** |
||
| 169 | * @param boolean $dropdown |
||
| 170 | */ |
||
| 171 | public function asDropdown($dropdown){ |
||
| 181 | |||
| 182 | public function setVertical(){ |
||
| 185 | |||
| 186 | public function setInline(){ |
||
| 189 | |||
| 190 | public function setSimple(){ |
||
| 193 | |||
| 194 | public function asButton($floating=false){ |
||
| 200 | |||
| 201 | public function asSelect($name=NULL,$multiple=false,$selection=true){ |
||
| 212 | |||
| 213 | public function asSearch($name=NULL,$multiple=false,$selection=true){ |
||
| 217 | |||
| 218 | public function setSelect($name=NULL,$multiple=false){ |
||
| 235 | |||
| 236 | public function asSubmenu($pointing=NULL){ |
||
| 243 | |||
| 244 | public function setPointing($value=Direction::NONE){ |
||
| 247 | |||
| 248 | public function setValue($value){ |
||
| 264 | |||
| 265 | /* |
||
| 266 | * (non-PHPdoc) |
||
| 267 | * @see BaseHtml::run() |
||
| 268 | */ |
||
| 269 | public function run(JsUtils $js) { |
||
| 277 | |||
| 278 | public function setCompact(){ |
||
| 281 | |||
| 282 | public function setAction($action){ |
||
| 285 | |||
| 286 | public function setFullTextSearch($value){ |
||
| 289 | |||
| 290 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
| 294 | |||
| 295 | public function getInput() { |
||
| 298 | |||
| 299 | } |
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: