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 BaseHtml 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 BaseHtml, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | abstract class BaseHtml extends BaseWidget { |
||
| 16 | protected $_template; |
||
| 17 | protected $tagName; |
||
| 18 | protected $properties=array (); |
||
| 19 | protected $events=array (); |
||
| 20 | protected $wrapBefore=""; |
||
| 21 | protected $wrapAfter=""; |
||
| 22 | protected $_bsComponent; |
||
| 23 | |||
| 24 | public function getBsComponent() { |
||
| 27 | |||
| 28 | public function setBsComponent($bsComponent) { |
||
| 32 | |||
| 33 | protected function getTemplate() { |
||
| 36 | |||
| 37 | public function getProperties() { |
||
| 40 | |||
| 41 | public function setProperties($properties) { |
||
| 45 | |||
| 46 | public function setProperty($name, $value) { |
||
| 50 | |||
| 51 | public function getProperty($name) { |
||
| 55 | |||
| 56 | public function addToProperty($name, $value, $separator=" ") { |
||
| 65 | |||
| 66 | public function addProperties($properties) { |
||
| 70 | |||
| 71 | public function compile(JsUtils $js=NULL, View $view=NULL) { |
||
| 96 | |||
| 97 | View Code Duplication | protected function ctrl($name, $value, $typeCtrl) { |
|
| 109 | |||
| 110 | protected function setPropertyCtrl($name, $value, $typeCtrl) { |
||
| 115 | |||
| 116 | protected function setMemberCtrl(&$name, $value, $typeCtrl) { |
||
| 122 | |||
| 123 | protected function addToMemberUnique(&$name, $value, $typeCtrl, $separator=" ") { |
||
| 130 | |||
| 131 | protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ") { |
||
| 140 | |||
| 141 | protected function addToMember(&$name, $value, $separator=" ") { |
||
| 145 | |||
| 146 | protected function addToPropertyUnique($name, $value, $typeCtrl) { |
||
| 154 | |||
| 155 | public function addToPropertyCtrl($name, $value, $typeCtrl) { |
||
| 161 | |||
| 162 | protected function removeOldValues(&$oldValue, $allValues) { |
||
| 166 | |||
| 167 | abstract public function run(JsUtils $js); |
||
| 168 | |||
| 169 | public function getTagName() { |
||
| 172 | |||
| 173 | public function setTagName($tagName) { |
||
| 177 | |||
| 178 | public function fromArray($array) { |
||
| 208 | |||
| 209 | public function fromDatabaseObjects($objects,$function) { |
||
| 217 | |||
| 218 | public function fromDatabaseObject($object,$function){ |
||
| 221 | |||
| 222 | public function wrap($before, $after="") { |
||
| 229 | |||
| 230 | public function addEvent($event, $jsCode, $stopPropagation=false, $preventDefault=false) { |
||
| 240 | |||
| 241 | public function _addEvent($event, $jsCode) { |
||
| 255 | |||
| 256 | public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) { |
||
| 259 | |||
| 260 | public function onClick($jsCode, $stopPropagation=false, $preventDefault=false) { |
||
| 263 | |||
| 264 | public function setClick($jsCode) { |
||
| 267 | |||
| 268 | public function addEventsOnRun(JsUtils $js) { |
||
| 289 | |||
| 290 | public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) { |
||
| 299 | |||
| 300 | public function getOn($event, $url, $responseElement="", $parameters=array()) { |
||
| 303 | |||
| 304 | public function getOnClick($url, $responseElement="", $parameters=array()) { |
||
| 307 | |||
| 308 | public function postOn($event, $url, $params="{}", $responseElement="", $parameters=array()) { |
||
| 312 | |||
| 313 | public function postOnClick($url, $params="{}", $responseElement="", $parameters=array()) { |
||
| 316 | |||
| 317 | public function postFormOn($event, $url, $form, $responseElement="", $parameters=array()) { |
||
| 321 | |||
| 322 | public function postFormOnClick($url, $form, $responseElement="", $parameters=array()) { |
||
| 325 | |||
| 326 | public function getElementById($identifier, $elements) { |
||
| 343 | |||
| 344 | public function __toString(){ |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Puts HTML values in quotes for use in jQuery code |
||
| 350 | * unless the supplied value contains the Javascript 'this' or 'event' |
||
| 351 | * object, in which case no quotes are added |
||
| 352 | * |
||
| 353 | * @param string $value |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | public function _prep_value($value) { |
||
| 365 | |||
| 366 | public function jsDoJquery($jqueryCall, $param=""){ |
||
| 369 | |||
| 370 | public function jsHtml($content=""){ |
||
| 373 | |||
| 374 | public function jsShow(){ |
||
| 377 | |||
| 378 | public function jsHide(){ |
||
| 381 | } |