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 removeProperty($name){ |
||
| 151 | |||
| 152 | protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ") { |
||
| 161 | |||
| 162 | protected function addToMember(&$name, $value, $separator=" ") { |
||
| 166 | |||
| 167 | protected function addToPropertyUnique($name, $value, $typeCtrl) { |
||
| 175 | |||
| 176 | public function addToPropertyCtrl($name, $value, $typeCtrl) { |
||
| 182 | |||
| 183 | protected function removeOldValues(&$oldValue, $allValues) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param JsUtils $js |
||
| 190 | * @return SimpleExtComponent |
||
| 191 | */ |
||
| 192 | public abstract function run(JsUtils $js); |
||
| 193 | |||
| 194 | public function getTagName() { |
||
| 197 | |||
| 198 | public function setTagName($tagName) { |
||
| 202 | |||
| 203 | public function fromArray($array) { |
||
| 233 | |||
| 234 | public function fromDatabaseObjects($objects,$function) { |
||
| 242 | |||
| 243 | public function fromDatabaseObject($object,$function){ |
||
| 246 | |||
| 247 | public function wrap($before, $after="") { |
||
| 248 | if(isset($before)){ |
||
| 249 | $this->_wrapBefore[]=$before; |
||
| 250 | } |
||
| 251 | $this->_wrapAfter[]=$after; |
||
| 252 | return $this; |
||
| 253 | } |
||
| 254 | |||
| 255 | public function addEvent($event, $jsCode, $stopPropagation=false, $preventDefault=false) { |
||
| 256 | if ($stopPropagation===true) { |
||
| 257 | $jsCode="event.stopPropagation();".$jsCode; |
||
| 258 | } |
||
| 259 | if ($preventDefault===true) { |
||
| 260 | $jsCode="event.preventDefault();".$jsCode; |
||
| 261 | } |
||
| 262 | return $this->_addEvent($event, $jsCode); |
||
| 263 | } |
||
| 264 | |||
| 265 | public function _addEvent($event, $jsCode) { |
||
| 266 | if (array_key_exists($event, $this->_events)) { |
||
| 267 | if (is_array($this->_events [$event])) { |
||
| 268 | $this->_events [$event] []=$jsCode; |
||
| 269 | } else { |
||
| 270 | $this->_events [$event]=array ( |
||
| 271 | $this->_events [$event], |
||
| 272 | $jsCode |
||
| 273 | ); |
||
| 274 | } |
||
| 275 | } else { |
||
| 276 | $this->_events [$event]=$jsCode; |
||
| 277 | } |
||
| 278 | return $this; |
||
| 279 | } |
||
| 280 | |||
| 281 | public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) { |
||
| 284 | |||
| 285 | public function onClick($jsCode, $stopPropagation=false, $preventDefault=true) { |
||
| 288 | |||
| 289 | public function setClick($jsCode) { |
||
| 292 | |||
| 293 | public function addEventsOnRun(JsUtils $js) { |
||
| 294 | if (isset($this->_bsComponent)) { |
||
| 295 | foreach ( $this->_events as $event => $jsCode ) { |
||
| 296 | $code=$jsCode; |
||
| 297 | if (is_array($jsCode)) { |
||
| 298 | $code=""; |
||
| 299 | foreach ( $jsCode as $jsC ) { |
||
| 300 | if ($jsC instanceof AjaxCall) { |
||
| 301 | $code.="\n".$jsC->compile($js); |
||
| 302 | } else { |
||
| 303 | $code.="\n".$jsC; |
||
| 304 | } |
||
| 305 | } |
||
| 306 | } elseif ($jsCode instanceof AjaxCall) { |
||
| 307 | $code=$jsCode->compile($js); |
||
| 308 | } |
||
| 309 | $this->_bsComponent->addEvent($event, $code); |
||
| 310 | } |
||
| 311 | $this->_events=array(); |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) { |
||
| 324 | |||
| 325 | public function getOn($event, $url, $responseElement="", $parameters=array()) { |
||
| 328 | |||
| 329 | public function getOnClick($url, $responseElement="", $parameters=array()) { |
||
| 332 | |||
| 333 | public function postOn($event, $url, $params="{}", $responseElement="", $parameters=array()) { |
||
| 337 | |||
| 338 | public function postOnClick($url, $params="{}", $responseElement="", $parameters=array()) { |
||
| 341 | |||
| 342 | public function postFormOn($event, $url, $form, $responseElement="", $parameters=array()) { |
||
| 346 | |||
| 347 | public function postFormOnClick($url, $form, $responseElement="", $parameters=array()) { |
||
| 350 | |||
| 351 | public function getElementById($identifier, $elements) { |
||
| 368 | |||
| 369 | public function __toString(){ |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Puts HTML values in quotes for use in jQuery code |
||
| 375 | * unless the supplied value contains the Javascript 'this' or 'event' |
||
| 376 | * object, in which case no quotes are added |
||
| 377 | * |
||
| 378 | * @param string $value |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | public function _prep_value($value) { |
||
| 390 | |||
| 391 | public function jsDoJquery($jqueryCall, $param=""){ |
||
| 394 | |||
| 395 | public function executeOnRun($jsCode){ |
||
| 396 | return $this->_addEvent("execute", $jsCode); |
||
| 397 | } |
||
| 398 | |||
| 399 | public function jsHtml($content=""){ |
||
| 400 | return $this->jsDoJquery("html",$content); |
||
| 402 | |||
| 403 | public function jsShow(){ |
||
| 406 | |||
| 407 | public function jsHide(){ |
||
| 410 | |||
| 411 | protected function setWrapBefore($wrapBefore) { |
||
| 415 | |||
| 416 | protected function setWrapAfter($wrapAfter) { |
||
| 420 | |||
| 421 | } |