| Total Complexity | 63 |
| Total Lines | 266 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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.
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 | use BaseHtmlEventsTrait,BaseHtmlPropertiesTrait; |
||
| 19 | protected $_template; |
||
| 20 | protected $tagName; |
||
| 21 | protected $_wrapBefore=array (); |
||
| 22 | protected $_wrapAfter=array (); |
||
| 23 | protected $_bsComponent; |
||
| 24 | protected $_compiled=false; |
||
| 25 | protected $_runned=false; |
||
| 26 | protected $_postCompile; |
||
| 27 | protected $_preCompile; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * |
||
| 31 | * @param JsUtils $js |
||
| 32 | * @return SimpleExtComponent |
||
| 33 | */ |
||
| 34 | abstract public function run(JsUtils $js); |
||
| 35 | |||
| 36 | private function _callSetter($setter,$key,$value,&$array){ |
||
| 37 | $result=false; |
||
| 38 | if (method_exists($this, $setter) && substr($setter, 0, 1) !== "_") { |
||
| 39 | try { |
||
| 40 | $this->$setter($value); |
||
| 41 | unset($array[$key]); |
||
| 42 | $result=true; |
||
| 43 | } catch ( \Exception $e ) { |
||
| 44 | $result=false; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | return $result; |
||
| 48 | } |
||
| 49 | |||
| 50 | protected function getTemplate(JsUtils $js=NULL,$view=null) { |
||
| 51 | return PropertyWrapper::wrap($this->_wrapBefore, $js,$view) . $this->_template . PropertyWrapper::wrap($this->_wrapAfter, $js,$view); |
||
| 52 | } |
||
| 53 | |||
| 54 | protected function ctrl($name, $value, $typeCtrl) { |
||
| 55 | if (\is_array($typeCtrl)) { |
||
| 56 | if (array_search($value, $typeCtrl) === false) { |
||
| 57 | throw new \Exception("La valeur passée `" . $value . "` à la propriété `" . $name . "` ne fait pas partie des valeurs possibles : {" . implode(",", $typeCtrl) . "}"); |
||
| 58 | } |
||
| 59 | } else { |
||
| 60 | if (!$typeCtrl($value)) { |
||
| 61 | throw new \Exception("La fonction " . $typeCtrl . " a retourné faux pour l'affectation de la propriété " . $name); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | return true; |
||
| 65 | } |
||
| 66 | |||
| 67 | |||
| 68 | |||
| 69 | protected function setMemberCtrl(&$name, $value, $typeCtrl) { |
||
| 70 | $this->ctrl($name, $value, $typeCtrl); |
||
| 71 | $name=$value; |
||
| 72 | return $this; |
||
| 73 | } |
||
| 74 | |||
| 75 | protected function addToMemberUnique(&$name, $value, $typeCtrl, $separator=" ") { |
||
| 76 | if (\is_array($typeCtrl)) { |
||
| 77 | $this->removeOldValues($name, $typeCtrl); |
||
| 78 | $name.=$separator . $value; |
||
| 79 | } |
||
| 80 | return $this; |
||
| 81 | } |
||
| 82 | |||
| 83 | |||
| 84 | |||
| 85 | protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ") { |
||
| 86 | $this->ctrl($name, $value, $typeCtrl); |
||
| 87 | if (\is_array($typeCtrl)) { |
||
| 88 | $this->removeOldValues($name, $typeCtrl); |
||
| 89 | } |
||
| 90 | $name.=$separator . $value; |
||
| 91 | return $this; |
||
| 92 | } |
||
| 93 | |||
| 94 | protected function addToMember(&$name, $value, $separator=" ") { |
||
| 95 | $name=str_ireplace($value, "", $name) . $separator . $value; |
||
| 96 | return $this; |
||
| 97 | } |
||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | protected function removeOldValues(&$oldValue, $allValues) { |
||
| 102 | $oldValue=str_ireplace($allValues, "", $oldValue); |
||
| 103 | $oldValue=trim($oldValue); |
||
| 104 | } |
||
| 105 | |||
| 106 | protected function _getElementBy($callback,$elements){ |
||
| 107 | if (\is_array($elements)) { |
||
| 108 | $elements=\array_values($elements); |
||
| 109 | $flag=false; |
||
| 110 | $index=0; |
||
| 111 | while ( !$flag && $index < sizeof($elements) ) { |
||
| 112 | if ($elements[$index] instanceof BaseHtml) |
||
| 113 | $flag=($callback($elements[$index])); |
||
| 114 | $index++; |
||
| 115 | } |
||
| 116 | if ($flag === true) |
||
| 117 | return $elements[$index - 1]; |
||
| 118 | } elseif ($elements instanceof BaseHtml) { |
||
| 119 | if ($callback($elements)) |
||
| 120 | return $elements; |
||
| 121 | } |
||
| 122 | return null; |
||
| 123 | } |
||
| 124 | |||
| 125 | protected function setWrapBefore($wrapBefore) { |
||
| 126 | $this->_wrapBefore=$wrapBefore; |
||
| 127 | return $this; |
||
| 128 | } |
||
| 129 | |||
| 130 | protected function setWrapAfter($wrapAfter) { |
||
| 131 | $this->_wrapAfter=$wrapAfter; |
||
| 132 | return $this; |
||
| 133 | } |
||
| 134 | |||
| 135 | public function getTagName() { |
||
| 136 | return $this->tagName; |
||
| 137 | } |
||
| 138 | |||
| 139 | public function setTagName($tagName) { |
||
| 140 | $this->tagName=$tagName; |
||
| 141 | return $this; |
||
| 142 | } |
||
| 143 | |||
| 144 | public function fromArray($array) { |
||
| 155 | } |
||
| 156 | |||
| 157 | public function fromDatabaseObjects($objects, $function) { |
||
| 158 | if (isset($objects)) { |
||
| 159 | foreach ( $objects as $object ) { |
||
| 160 | $this->fromDatabaseObject($object, $function); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | return $this; |
||
| 164 | } |
||
| 165 | |||
| 166 | public function fromDatabaseObject($object, $function) { |
||
| 167 | } |
||
| 168 | |||
| 169 | public function wrap($before, $after="") { |
||
| 170 | if (isset($before)) { |
||
| 171 | array_unshift($this->_wrapBefore, $before); |
||
| 172 | } |
||
| 173 | $this->_wrapAfter[]=$after; |
||
| 174 | return $this; |
||
| 175 | } |
||
| 176 | |||
| 177 | |||
| 178 | |||
| 179 | public function getElementById($identifier, $elements) { |
||
| 181 | } |
||
| 182 | |||
| 183 | public function getBsComponent() { |
||
| 184 | return $this->_bsComponent; |
||
| 185 | } |
||
| 186 | |||
| 187 | public function setBsComponent($bsComponent) { |
||
| 190 | } |
||
| 191 | |||
| 192 | protected function compile_once(JsUtils $js=NULL, &$view=NULL) { |
||
| 193 | if(!$this->_compiled){ |
||
| 194 | if(isset($js)){ |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
| 210 | $this->compile_once($js,$view); |
||
| 211 | $result=$this->getTemplate($js,$view); |
||
| 212 | foreach ( $this as $key => $value ) { |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Sets the element draggable, and eventualy defines the dropzone (HTML5 drag and drop) |
||
| 242 | * @param string $attr default: "id" |
||
| 243 | * @param BaseHtml $dropZone the dropzone element |
||
| 244 | * @param array $parameters default: ["jsCallback"=>"","jqueryDone"=>"append"] |
||
| 245 | * @return \Ajax\common\html\BaseHtml |
||
| 246 | */ |
||
| 247 | public function setDraggable($attr="id",$dropZone=null,$parameters=[]){ |
||
| 248 | $this->setProperty("draggable", "true"); |
||
| 249 | $this->addEvent("dragstart",Javascript::draggable($attr)); |
||
| 250 | if(isset($dropZone)&& $dropZone instanceof BaseHtml){ |
||
| 251 | $jqueryDone="append";$jsCallback=""; |
||
| 252 | extract($parameters); |
||
| 253 | $dropZone->asDropZone($jsCallback,$jqueryDone,$parameters); |
||
| 254 | } |
||
| 255 | return $this; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Declares the element as a drop zone (HTML5 drag and drop) |
||
| 260 | * @param string $jsCallback |
||
| 261 | * @param string $jqueryDone |
||
| 262 | * @param array $parameters |
||
| 263 | * @return \Ajax\common\html\BaseHtml |
||
| 264 | */ |
||
| 265 | public function asDropZone($jsCallback="",$jqueryDone="append",$parameters=[]){ |
||
| 266 | $stopPropagation=false; |
||
| 267 | $this->addEvent("dragover", '', $stopPropagation,true); |
||
| 268 | extract($parameters); |
||
| 269 | $this->addEvent("drop",Javascript::dropZone($jqueryDone,$jsCallback),$stopPropagation,true); |
||
| 270 | return $this; |
||
| 271 | } |
||
| 272 | |||
| 273 | public function __toString() { |
||
| 274 | return $this->compile(); |
||
| 275 | } |
||
| 276 | |||
| 277 | public function onPostCompile($callback){ |
||
| 278 | $this->_postCompile=$callback; |
||
| 279 | } |
||
| 280 | |||
| 281 | public function onPreCompile($callback){ |
||
| 283 | } |
||
| 284 | } |
||
| 285 |