| Total Complexity | 60 |
| Total Lines | 232 |
| 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 |
||
| 16 | abstract class BaseHtml extends BaseWidget { |
||
| 17 | use BaseHtmlEventsTrait,BaseHtmlPropertiesTrait; |
||
| 18 | protected $_template; |
||
| 19 | protected $tagName; |
||
| 20 | protected $_wrapBefore=array (); |
||
| 21 | protected $_wrapAfter=array (); |
||
| 22 | protected $_bsComponent; |
||
| 23 | protected $_compiled=false; |
||
| 24 | protected $_postCompile; |
||
| 25 | protected $_preCompile; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * |
||
| 29 | * @param JsUtils $js |
||
| 30 | * @return SimpleExtComponent |
||
| 31 | */ |
||
| 32 | abstract public function run(JsUtils $js); |
||
| 33 | |||
| 34 | private function _callSetter($setter,$key,$value,&$array){ |
||
| 35 | $result=false; |
||
| 36 | if (method_exists($this, $setter) && substr($setter, 0, 1) !== "_") { |
||
| 37 | try { |
||
| 38 | $this->$setter($value); |
||
| 39 | unset($array[$key]); |
||
| 40 | $result=true; |
||
| 41 | } catch ( \Exception $e ) { |
||
| 42 | $result=false; |
||
| 43 | } |
||
| 44 | } |
||
| 45 | return $result; |
||
| 46 | } |
||
| 47 | |||
| 48 | protected function getTemplate(JsUtils $js=NULL) { |
||
| 49 | return PropertyWrapper::wrap($this->_wrapBefore, $js) . $this->_template . PropertyWrapper::wrap($this->_wrapAfter, $js); |
||
| 50 | } |
||
| 51 | |||
| 52 | protected function ctrl($name, $value, $typeCtrl) { |
||
| 53 | if (\is_array($typeCtrl)) { |
||
| 54 | if (array_search($value, $typeCtrl) === false) { |
||
| 55 | throw new \Exception("La valeur passée `" . $value . "` à la propriété `" . $name . "` ne fait pas partie des valeurs possibles : {" . implode(",", $typeCtrl) . "}"); |
||
| 56 | } |
||
| 57 | } else { |
||
| 58 | if (!$typeCtrl($value)) { |
||
| 59 | throw new \Exception("La fonction " . $typeCtrl . " a retourné faux pour l'affectation de la propriété " . $name); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | return true; |
||
| 63 | } |
||
| 64 | |||
| 65 | |||
| 66 | |||
| 67 | protected function setMemberCtrl(&$name, $value, $typeCtrl) { |
||
| 68 | if ($this->ctrl($name, $value, $typeCtrl) === true) { |
||
|
|
|||
| 69 | return $name=$value; |
||
| 70 | } |
||
| 71 | return $this; |
||
| 72 | } |
||
| 73 | |||
| 74 | protected function addToMemberUnique(&$name, $value, $typeCtrl, $separator=" ") { |
||
| 75 | if (\is_array($typeCtrl)) { |
||
| 76 | $this->removeOldValues($name, $typeCtrl); |
||
| 77 | $name.=$separator . $value; |
||
| 78 | } |
||
| 79 | return $this; |
||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | |||
| 84 | protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ") { |
||
| 85 | if ($this->ctrl($name, $value, $typeCtrl) === true) { |
||
| 86 | if (\is_array($typeCtrl)) { |
||
| 87 | $this->removeOldValues($name, $typeCtrl); |
||
| 88 | } |
||
| 89 | $name.=$separator . $value; |
||
| 90 | } |
||
| 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) { |
||
| 180 | return $this->_getElementBy(function(BaseWidget $element) use ($identifier){return $element->getIdentifier()===$identifier;}, $elements); |
||
| 181 | } |
||
| 182 | |||
| 183 | public function getBsComponent() { |
||
| 184 | return $this->_bsComponent; |
||
| 185 | } |
||
| 186 | |||
| 187 | public function setBsComponent($bsComponent) { |
||
| 188 | $this->_bsComponent=$bsComponent; |
||
| 189 | return $this; |
||
| 190 | } |
||
| 191 | |||
| 192 | protected function compile_once(JsUtils $js=NULL, &$view=NULL) { |
||
| 193 | if(!$this->_compiled){ |
||
| 194 | if(isset($js)){ |
||
| 195 | $beforeCompile=$js->getParam("beforeCompileHtml"); |
||
| 196 | if(\is_callable($beforeCompile)){ |
||
| 197 | $beforeCompile($this,$js,$view); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | if(\is_callable($this->_preCompile)){ |
||
| 201 | $pc=$this->_preCompile; |
||
| 202 | $pc($this); |
||
| 203 | } |
||
| 204 | unset($this->properties["jsCallback"]); |
||
| 205 | $this->_compiled=true; |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
| 236 | } |
||
| 237 | |||
| 238 | public function __toString() { |
||
| 239 | return $this->compile(); |
||
| 240 | } |
||
| 241 | |||
| 242 | public function onPostCompile($callback){ |
||
| 243 | $this->_postCompile=$callback; |
||
| 244 | } |
||
| 245 | |||
| 246 | public function onPreCompile($callback){ |
||
| 248 | } |
||
| 249 | } |
||
| 250 |