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