Total Complexity | 66 |
Total Lines | 490 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CreateHtmlCode 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 CreateHtmlCode, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class CreateHtmlCode |
||
33 | { |
||
34 | /** |
||
35 | * @static function getInstance |
||
36 | * |
||
37 | * @param null |
||
38 | * |
||
39 | * @return CreateHtmlCode |
||
40 | */ |
||
41 | public static function getInstance() |
||
42 | { |
||
43 | static $instance = false; |
||
44 | if (!$instance) { |
||
45 | $instance = new self(); |
||
46 | } |
||
47 | |||
48 | return $instance; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @public function getHtmlTag |
||
53 | * @param string $tag |
||
54 | * @param array $attributes |
||
55 | * @param string $content |
||
56 | * @param bool $noClosed |
||
57 | * @param string $t |
||
58 | * @param string $n |
||
59 | * @param bool $multiLine |
||
60 | * @return string |
||
61 | */ |
||
62 | public function getHtmlTag($tag = '', $attributes = [], $content = '', $noClosed = false, $t = '', $n = "\n", $multiLine = false) |
||
63 | { |
||
64 | if (empty($attributes)) { |
||
65 | $attributes = []; |
||
66 | } |
||
67 | $attr = $this->getAttributes($attributes); |
||
68 | if ('br' === $tag) { |
||
69 | $ret = "{$t}<{$tag}{$attr}>{$n}"; |
||
70 | } elseif ($noClosed) { |
||
71 | if ('img' === $tag) { |
||
72 | $ret = "{$t}<{$tag}{$attr} >{$n}"; |
||
73 | } else { |
||
74 | $ret = "{$t}<{$tag}{$attr} />{$n}"; |
||
75 | } |
||
76 | } elseif ($multiLine) { |
||
77 | $ret = "{$t}<{$tag}{$attr}>{$n}"; |
||
78 | $ret .= "{$content}"; |
||
79 | $ret .= "{$t}</{$tag}>{$n}"; |
||
80 | } else { |
||
81 | $ret = "{$t}<{$tag}{$attr}>{$content}</{$tag}>{$n}"; |
||
82 | } |
||
83 | |||
84 | return $ret; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @private function setAttributes |
||
89 | * @param array $attributes |
||
90 | * @return string |
||
91 | */ |
||
92 | private function getAttributes($attributes) |
||
93 | { |
||
94 | $str = ''; |
||
95 | foreach ($attributes as $name => $value) { |
||
96 | if ('_' !== $name) { |
||
97 | $str .= ' ' . $name . '="' . $value . '"'; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | return $str; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @public function getHtmlEmpty |
||
106 | * @param string $empty |
||
107 | * @param string $t |
||
108 | * @param string $n |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getHtmlEmpty($empty = '', $t = '', $n = '') |
||
112 | { |
||
113 | return "{$t}{$empty}{$n}"; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @public function getHtmlComment |
||
118 | * @param string $htmlComment |
||
119 | * @param string $t |
||
120 | * @param string $n |
||
121 | * @return string |
||
122 | */ |
||
123 | public function getHtmlComment($htmlComment = '', $t = '', $n = '') |
||
124 | { |
||
125 | return "{$t}<!-- {$htmlComment} -->{$n}"; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @public function getHtmlBr |
||
130 | * @param int $brNumb |
||
131 | * @param string $htmlClass |
||
132 | * @param string $t |
||
133 | * @param string $n |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getHtmlBr($brNumb = 1, $htmlClass = '', $t = '', $n = "\n") |
||
137 | { |
||
138 | $brClass = ('' != $htmlClass) ? " class='{$htmlClass}'" : ''; |
||
139 | $ret = ''; |
||
140 | for ($i = 0; $i < $brNumb; ++$i) { |
||
141 | $ret .= "{$t}<br{$brClass}>{$n}"; |
||
142 | } |
||
143 | |||
144 | return $ret; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @public function getHtmlHNumb |
||
149 | * @param string $content |
||
150 | * @param string $l |
||
151 | * @param string $htmlHClass |
||
152 | * @param string $t |
||
153 | * @param string $n |
||
154 | * @return string |
||
155 | */ |
||
156 | public function getHtmlHNumb($content = '', $l = '1', $htmlHClass = '', $t = '', $n = "\n") |
||
157 | { |
||
158 | $hClass = ('' != $htmlHClass) ? " class='{$htmlHClass}'" : ''; |
||
159 | $ret = "{$t}<h{$l}{$hClass}>{$content}</h{$l}>{$n}"; |
||
160 | |||
161 | return $ret; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @public function getHtmlDiv |
||
166 | * @param string $content |
||
167 | * @param string $divClass |
||
168 | * @param string $t |
||
169 | * @param string $n |
||
170 | * @param bool $split |
||
171 | * @return string |
||
172 | */ |
||
173 | public function getHtmlDiv($content = '', $divClass = '', $t = '', $n = "\n", $split = true) |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @public function getHtmlPre |
||
190 | * @param string $content |
||
191 | * @param string $preClass |
||
192 | * @param string $t |
||
193 | * @param string $n |
||
194 | * @return string |
||
195 | */ |
||
196 | public function getHtmlPre($content = '', $preClass = '', $t = '', $n = "\n") |
||
197 | { |
||
198 | $rPreClass = ('' != $preClass) ? " class='{$preClass}'" : ''; |
||
199 | $ret = "{$t}<pre{$rPreClass}>{$n}"; |
||
200 | $ret .= "{$content}"; |
||
201 | $ret .= "{$t}</pre>{$n}"; |
||
202 | |||
203 | return $ret; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @public function getHtmlSpan |
||
208 | * @param string $content |
||
209 | * @param string $spanClass |
||
210 | * @param string $t |
||
211 | * @param string $n |
||
212 | * @param bool $split |
||
213 | * @return string |
||
214 | */ |
||
215 | public function getHtmlSpan($content = '', $spanClass = '', $t = '', $n = "\n", $split = false) |
||
216 | { |
||
217 | $rSpanClass = ('' != $spanClass) ? " class='{$spanClass}'" : ''; |
||
218 | $ret = "{$t}<span{$rSpanClass}>"; |
||
219 | if ($split) { |
||
220 | $ret .= "\n"; |
||
221 | } |
||
222 | $ret .= "{$content}"; |
||
223 | if ($split) { |
||
224 | $ret .= "\n{$t}"; |
||
225 | } |
||
226 | $ret .= "</span>{$n}"; |
||
227 | |||
228 | return $ret; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @public function getHtmlParagraph |
||
233 | * @param string $content |
||
234 | * @param string $pClass |
||
235 | * @param string $t |
||
236 | * @param string $n |
||
237 | * @return string |
||
238 | */ |
||
239 | public function getHtmlParagraph($content = '', $pClass = '', $t = '', $n = "\n") |
||
240 | { |
||
241 | $rPClass = ('' != $pClass) ? " class='{$pClass}'" : ''; |
||
242 | $ret = "{$t}<p{$rPClass}>{$n}"; |
||
243 | $ret .= "{$content}"; |
||
244 | $ret .= "{$t}</p>{$n}"; |
||
245 | |||
246 | return $ret; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * @public function getHtmlI |
||
251 | * @param string $content |
||
252 | * @param string $iClass |
||
253 | * @param string $iId |
||
254 | * @param string $t |
||
255 | * @param string $n |
||
256 | * @return string |
||
257 | */ |
||
258 | public function getHtmlI($content = '', $iClass = '', $iId = '', $t = '', $n = "\n") |
||
259 | { |
||
260 | $rIClass = ('' != $iClass) ? " class='{$iClass}'" : ''; |
||
261 | $rIId = ('' != $iId) ? " id='{$iId}'" : ''; |
||
262 | $ret = "{$t}<i{$rIClass}{$rIId}>{$content}</i>{$n}"; |
||
263 | |||
264 | return $ret; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @public function getHtmlUl |
||
269 | * @param string $content |
||
270 | * @param string $ulClass |
||
271 | * @param string $t |
||
272 | * @param string $n |
||
273 | * @return string |
||
274 | */ |
||
275 | public function getHtmlUl($content = '', $ulClass = '', $t = '', $n = "\n") |
||
276 | { |
||
277 | $rUlClass = ('' != $ulClass) ? " class='{$ulClass}'" : ''; |
||
278 | $ret = "{$t}<ul{$rUlClass}>{$n}"; |
||
279 | $ret .= "{$content}"; |
||
280 | $ret .= "{$t}</ul>{$n}"; |
||
281 | |||
282 | return $ret; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @public function getHtmlOl |
||
287 | * @param string $content |
||
288 | * @param string $olClass |
||
289 | * @param string $t |
||
290 | * @param string $n |
||
291 | * @return string |
||
292 | */ |
||
293 | public function getHtmlOl($content = '', $olClass = '', $t = '', $n = "\n") |
||
294 | { |
||
295 | $rOlClass = ('' != $olClass) ? " class='{$olClass}'" : ''; |
||
296 | $ret = "{$t}<ol{$rOlClass}>{$n}"; |
||
297 | $ret .= "{$content}"; |
||
298 | $ret .= "{$t}</ol>{$n}"; |
||
299 | |||
300 | return $ret; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @public function getHtmlLi |
||
305 | * @param string $content |
||
306 | * @param string $liClass |
||
307 | * @param string $t |
||
308 | * @param string $n |
||
309 | * @param bool $split |
||
310 | * @return string |
||
311 | */ |
||
312 | public function getHtmlLi($content = '', $liClass = '', $t = '', $n = "\n", $split = false) |
||
313 | { |
||
314 | $rLiClass = ('' != $liClass) ? " class='{$liClass}'" : ''; |
||
315 | if ($split) { |
||
316 | $ret = "{$t}<li{$rLiClass}>{$n}"; |
||
317 | $ret .= "{$content}"; |
||
318 | $ret .= "{$t}</li>{$n}"; |
||
319 | } else { |
||
320 | $ret = "{$t}<li{$rLiClass}>{$content}</li>{$n}"; |
||
321 | } |
||
322 | |||
323 | return $ret; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * @public function getHtmlStrong |
||
328 | * @param string $content |
||
329 | * @param string $strongClass |
||
330 | * @param string $t |
||
331 | * @param string $n |
||
332 | * @return string |
||
333 | */ |
||
334 | public function getHtmlStrong($content = '', $strongClass = '', $t = '', $n = '') |
||
335 | { |
||
336 | $rStrongClass = ('' != $strongClass) ? " class='{$strongClass}'" : ''; |
||
337 | |||
338 | return "{$t}<strong{$rStrongClass}>{$content}</strong>{$n}"; |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * @public function getHtmlAnchor |
||
343 | * @param string $url |
||
344 | * @param string $content |
||
345 | * @param string $title |
||
346 | * @param string $target |
||
347 | * @param string $aClass |
||
348 | * @param string $rel |
||
349 | * @param string $t |
||
350 | * @param string $n |
||
351 | * @return string |
||
352 | */ |
||
353 | public function getHtmlAnchor($url = '#', $content = ' ', $title = '', $target = '', $aClass = '', $rel = '', $t = '', $n = '') |
||
354 | { |
||
355 | $target = ('' != $target) ? " target='{$target}'" : ''; |
||
356 | $rAClass = ('' != $aClass) ? " class='{$aClass}'" : ''; |
||
357 | $rel = ('' != $rel) ? " rel='{$rel}'" : ''; |
||
358 | |||
359 | return "{$t}<a{$rAClass} href='{$url}' title='{$title}'{$target}{$rel}>{$content}</a>{$n}"; |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * @public function getHtmlImage |
||
364 | * @param string $src |
||
365 | * @param string $alt |
||
366 | * @param string $imgClass |
||
367 | * @param string $t |
||
368 | * @param string $n |
||
369 | * @return string |
||
370 | */ |
||
371 | public function getHtmlImage($src = 'blank.gif', $alt = 'blank.gif', $imgClass = '', $t = '', $n = '') |
||
372 | { |
||
373 | $rImgClass = ('' != $imgClass) ? " class='{$imgClass}'" : ''; |
||
374 | $ret = "{$t}<img{$rImgClass} src='{$src}' alt='{$alt}' >{$n}"; |
||
375 | |||
376 | return $ret; |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * @public function getHtmlTable |
||
381 | * @param string $content |
||
382 | * @param string $tableClass |
||
383 | * @param string $t |
||
384 | * @param string $n |
||
385 | * @return string |
||
386 | */ |
||
387 | public function getHtmlTable($content = '', $tableClass = '', $t = '', $n = "\n") |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * @public function getHtmlTableThead |
||
399 | * @param string $content |
||
400 | * @param string $theadClass |
||
401 | * @param string $t |
||
402 | * @param string $n |
||
403 | * @return string |
||
404 | */ |
||
405 | public function getHtmlTableThead($content = '', $theadClass = '', $t = '', $n = "\n") |
||
406 | { |
||
407 | $rTheadClass = ('' != $theadClass) ? " class='{$theadClass}'" : ''; |
||
408 | $ret = "{$t}<thead{$rTheadClass}>{$n}"; |
||
409 | $ret .= "{$content}"; |
||
410 | $ret .= "{$t}</thead>{$n}"; |
||
411 | |||
412 | return $ret; |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * @public function getHtmlTableTbody |
||
417 | * @param string $content |
||
418 | * @param string $tbodyClass |
||
419 | * |
||
420 | * @param string $t |
||
421 | * @param string $n |
||
422 | * @return string |
||
423 | */ |
||
424 | public function getHtmlTableTbody($content = '', $tbodyClass = '', $t = '', $n = "\n") |
||
425 | { |
||
426 | $rTbodyClass = ('' != $tbodyClass) ? " class='{$tbodyClass}'" : ''; |
||
427 | $ret = "{$t}<tbody{$rTbodyClass}>{$n}"; |
||
428 | $ret .= "{$content}"; |
||
429 | $ret .= "{$t}</tbody>{$n}"; |
||
430 | |||
431 | return $ret; |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * @public function getHtmlTableTfoot |
||
436 | * @param string $content |
||
437 | * @param string $tfootClass |
||
438 | * |
||
439 | * @param string $t |
||
440 | * @param string $n |
||
441 | * @param bool $split |
||
442 | * @return string |
||
443 | */ |
||
444 | public function getHtmlTableTfoot($content = '', $tfootClass = '', $t = '', $n = "\n", $split = true) |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * @public function getHtmlTableRow |
||
460 | * @param string $content |
||
461 | * @param string $trClass |
||
462 | * @param string $t |
||
463 | * @param string $n |
||
464 | * @return string |
||
465 | */ |
||
466 | public function getHtmlTableRow($content = '', $trClass = '', $t = '', $n = "\n") |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * @public function getHtmlTableHead |
||
478 | * @param string $content |
||
479 | * @param string $thClass |
||
480 | * @param string $colspan |
||
481 | * @param string $t |
||
482 | * @param string $n |
||
483 | * @param bool $split |
||
484 | * @return string |
||
485 | */ |
||
486 | public function getHtmlTableHead($content = '', $thClass = '', $colspan = '', $t = '', $n = "\n", $split = false) |
||
487 | { |
||
488 | $rThClass = ('' != $thClass) ? " class='{$thClass}'" : ''; |
||
489 | $colspan = ('' != $colspan) ? " colspan='{$colspan}'" : ''; |
||
490 | if ($split) { |
||
491 | $ret = "{$t}<th{$colspan}{$rThClass}>{$n}"; |
||
492 | $ret .= "{$content}"; |
||
493 | $ret .= "{$t}</th>{$n}"; |
||
494 | } else { |
||
495 | $ret = "{$t}<th{$colspan}{$rThClass}>{$content}</th>{$n}"; |
||
496 | } |
||
497 | return $ret; |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * @public function getHtmlTableData |
||
502 | * @param string $content |
||
503 | * @param string $tdClass |
||
504 | * @param string $colspan |
||
505 | * @param string $t |
||
506 | * @param string $n |
||
507 | * @param bool $split |
||
508 | * @return string |
||
509 | */ |
||
510 | public function getHtmlTableData($content = '', $tdClass = '', $colspan = '', $t = '', $n = "\n", $split = false) |
||
522 | } |
||
523 | } |
||
524 |