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 |
||
| 17 | abstract class BaseHtml extends BaseWidget { |
||
| 18 | protected $_template; |
||
| 19 | protected $tagName; |
||
| 20 | protected $properties=array (); |
||
| 21 | protected $_events=array (); |
||
| 22 | protected $_wrapBefore=array(); |
||
| 23 | protected $_wrapAfter=array(); |
||
| 24 | protected $_bsComponent; |
||
| 25 | |||
| 26 | public function getBsComponent() { |
||
| 29 | |||
| 30 | public function setBsComponent($bsComponent) { |
||
| 34 | |||
| 35 | protected function getTemplate(JsUtils $js=NULL) { |
||
| 36 | return PropertyWrapper::wrap($this->_wrapBefore, $js).$this->_template.PropertyWrapper::wrap($this->_wrapAfter, $js); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function getProperties() { |
||
| 42 | |||
| 43 | public function setProperties($properties) { |
||
| 47 | |||
| 48 | public function setProperty($name, $value) { |
||
| 52 | |||
| 53 | public function getProperty($name) { |
||
| 57 | |||
| 58 | public function addToProperty($name, $value, $separator=" ") { |
||
| 67 | |||
| 68 | public function addProperties($properties) { |
||
| 72 | |||
| 73 | public function compile(JsUtils $js=NULL, View $view=NULL) { |
||
| 98 | |||
| 99 | protected function ctrl($name, $value, $typeCtrl) { |
||
| 111 | |||
| 112 | protected function propertyContains($propertyName,$value){ |
||
| 119 | |||
| 120 | protected function setPropertyCtrl($name, $value, $typeCtrl) { |
||
| 125 | |||
| 126 | protected function setMemberCtrl(&$name, $value, $typeCtrl) { |
||
| 132 | |||
| 133 | protected function addToMemberUnique(&$name, $value, $typeCtrl, $separator=" ") { |
||
| 140 | |||
| 141 | protected function removePropertyValue($name,$value){ |
||
| 145 | |||
| 146 | protected function removePropertyValues($name,$values){ |
||
| 150 | |||
| 151 | protected function removeProperty($name){ |
||
| 156 | |||
| 157 | protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ") { |
||
| 166 | |||
| 167 | protected function addToMember(&$name, $value, $separator=" ") { |
||
| 171 | |||
| 172 | protected function addToPropertyUnique($name, $value, $typeCtrl) { |
||
| 180 | |||
| 181 | public function addToPropertyCtrl($name, $value, $typeCtrl) { |
||
| 187 | |||
| 188 | protected function removeOldValues(&$oldValue, $allValues) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param JsUtils $js |
||
| 195 | * @return SimpleExtComponent |
||
| 196 | */ |
||
| 197 | public abstract function run(JsUtils $js); |
||
| 198 | |||
| 199 | public function getTagName() { |
||
| 202 | |||
| 203 | public function setTagName($tagName) { |
||
| 207 | |||
| 208 | public function fromArray($array) { |
||
| 238 | |||
| 239 | public function fromDatabaseObjects($objects,$function) { |
||
| 247 | |||
| 248 | public function fromDatabaseObject($object,$function){ |
||
| 251 | |||
| 252 | public function wrap($before, $after="") { |
||
| 259 | |||
| 260 | public function addEvent($event, $jsCode, $stopPropagation=false, $preventDefault=false) { |
||
| 269 | |||
| 270 | public function _addEvent($event, $jsCode) { |
||
| 285 | |||
| 286 | public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) { |
||
| 289 | |||
| 290 | public function onClick($jsCode, $stopPropagation=false, $preventDefault=true) { |
||
| 293 | |||
| 294 | public function setClick($jsCode) { |
||
| 297 | |||
| 298 | public function addEventsOnRun(JsUtils $js) { |
||
| 319 | |||
| 320 | public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) { |
||
| 329 | |||
| 330 | public function getOn($event, $url, $responseElement="", $parameters=array()) { |
||
| 333 | |||
| 334 | public function getOnClick($url, $responseElement="", $parameters=array()) { |
||
| 337 | |||
| 338 | public function postOn($event, $url, $params="{}", $responseElement="", $parameters=array()) { |
||
| 342 | |||
| 343 | public function postOnClick($url, $params="{}", $responseElement="", $parameters=array()) { |
||
| 346 | |||
| 347 | public function postFormOn($event, $url, $form, $responseElement="", $parameters=array()) { |
||
| 351 | |||
| 352 | public function postFormOnClick($url, $form, $responseElement="", $parameters=array()) { |
||
| 355 | |||
| 356 | public function getElementById($identifier, $elements) { |
||
| 373 | |||
| 374 | public function __toString(){ |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Puts HTML values in quotes for use in jQuery code |
||
| 380 | * unless the supplied value contains the Javascript 'this' or 'event' |
||
| 381 | * object, in which case no quotes are added |
||
| 382 | * |
||
| 383 | * @param string $value |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | public function _prep_value($value) { |
||
| 395 | |||
| 396 | public function jsDoJquery($jqueryCall, $param=""){ |
||
| 399 | |||
| 400 | public function executeOnRun($jsCode){ |
||
| 403 | |||
| 404 | public function jsHtml($content=""){ |
||
| 407 | |||
| 408 | public function jsShow(){ |
||
| 411 | |||
| 412 | public function jsHide(){ |
||
| 415 | |||
| 416 | protected function setWrapBefore($wrapBefore) { |
||
| 420 | |||
| 421 | protected function setWrapAfter($wrapAfter) { |
||
| 425 | |||
| 426 | } |