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 |
||
| 16 | abstract class BaseHtml extends BaseWidget { |
||
| 17 | use BaseHtmlEventsTrait; |
||
| 18 | protected $_template; |
||
| 19 | protected $tagName; |
||
| 20 | protected $properties=array (); |
||
| 21 | protected $_wrapBefore=array (); |
||
| 22 | protected $_wrapAfter=array (); |
||
| 23 | protected $_bsComponent; |
||
| 24 | |||
| 25 | public function getBsComponent() { |
||
| 28 | |||
| 29 | public function setBsComponent($bsComponent) { |
||
| 33 | |||
| 34 | protected function getTemplate(JsUtils $js=NULL) { |
||
| 37 | |||
| 38 | public function getProperties() { |
||
| 41 | |||
| 42 | public function setProperties($properties) { |
||
| 46 | |||
| 47 | public function setProperty($name, $value) { |
||
| 51 | |||
| 52 | public function getProperty($name) { |
||
| 56 | |||
| 57 | public function addToProperty($name, $value, $separator=" ") { |
||
| 73 | |||
| 74 | public function addProperties($properties) { |
||
| 78 | |||
| 79 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
| 99 | |||
| 100 | protected function ctrl($name, $value, $typeCtrl) { |
||
| 112 | |||
| 113 | protected function propertyContains($propertyName, $value) { |
||
| 120 | |||
| 121 | protected function setPropertyCtrl($name, $value, $typeCtrl) { |
||
| 126 | |||
| 127 | protected function setMemberCtrl(&$name, $value, $typeCtrl) { |
||
| 133 | |||
| 134 | protected function addToMemberUnique(&$name, $value, $typeCtrl, $separator=" ") { |
||
| 141 | |||
| 142 | protected function removePropertyValue($name, $value) { |
||
| 146 | |||
| 147 | protected function removePropertyValues($name, $values) { |
||
| 151 | |||
| 152 | public function removeProperty($name) { |
||
| 157 | |||
| 158 | protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ") { |
||
| 167 | |||
| 168 | protected function addToMember(&$name, $value, $separator=" ") { |
||
| 172 | |||
| 173 | protected function addToPropertyUnique($name, $value, $typeCtrl) { |
||
| 181 | |||
| 182 | public function addToPropertyCtrl($name, $value, $typeCtrl) { |
||
| 185 | |||
| 186 | public function addToPropertyCtrlCheck($name, $value, $typeCtrl) { |
||
| 192 | |||
| 193 | protected function removeOldValues(&$oldValue, $allValues) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * |
||
| 200 | * @param JsUtils $js |
||
| 201 | * @return SimpleExtComponent |
||
| 202 | */ |
||
| 203 | public abstract function run(JsUtils $js); |
||
| 204 | |||
| 205 | public function getTagName() { |
||
| 208 | |||
| 209 | public function setTagName($tagName) { |
||
| 213 | |||
| 214 | public function fromArray($array) { |
||
| 215 | foreach ( $this as $key => $value ) { |
||
|
1 ignored issue
–
show
|
|||
| 216 | $this->_callSetter("set" . ucfirst($key), $key, $value, $array); |
||
| 217 | if (array_key_exists($key, $array) && !JString::startswith($key, "_")) { |
||
| 218 | $setter="set" . ucfirst($key); |
||
| 219 | $this->$setter($array[$key]); |
||
| 220 | unset($array[$key]); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | foreach ( $array as $key => $value ) { |
||
| 224 | if($this->_callSetter($key, $key, $value, $array)===false){ |
||
| 225 | $this->_callSetter("set" . ucfirst($key), $key, $value, $array); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | return $array; |
||
| 229 | } |
||
| 230 | |||
| 231 | private function _callSetter($setter,$key,$value,&$array){ |
||
| 232 | $result=false; |
||
| 233 | if (method_exists($this, $setter) && !JString::startswith($key, "_")) { |
||
| 234 | try { |
||
| 235 | $this->$setter($value); |
||
| 236 | unset($array[$key]); |
||
| 237 | $result=true; |
||
| 238 | } catch ( \Exception $e ) { |
||
| 239 | $result=false; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | return $result; |
||
| 243 | } |
||
| 244 | |||
| 245 | public function fromDatabaseObjects($objects, $function) { |
||
| 253 | |||
| 254 | public function fromDatabaseObject($object, $function) { |
||
| 256 | |||
| 257 | public function wrap($before, $after="") { |
||
| 258 | if (isset($before)) { |
||
| 264 | |||
| 265 | |||
| 266 | |||
| 267 | public function getElementById($identifier, $elements) { |
||
| 284 | |||
| 285 | protected function getElementByPropertyValue($propertyName,$value, $elements) { |
||
| 302 | |||
| 303 | public function __toString() { |
||
| 306 | |||
| 307 | protected function setWrapBefore($wrapBefore) { |
||
| 311 | |||
| 312 | protected function setWrapAfter($wrapAfter) { |
||
| 316 | } |