Completed
Pull Request — master (#144)
by Michael
04:31
created

CreateHtmlCode   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 417
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 107
dl 0
loc 417
rs 5.04
c 0
b 0
f 0
wmc 57

25 Methods

Rating   Name   Duplication   Size   Complexity  
A getHtmlTableTbody() 0 8 2
A getAttributes() 0 10 3
A getHtmlTable() 0 8 2
A getHtmlTableRow() 0 8 2
A getHtmlOl() 0 8 2
A getHtmlAnchor() 0 7 4
A getHtmlComment() 0 3 1
A getHtmlImage() 0 6 2
A getHtmlHNumb() 0 6 2
A getHtmlBr() 0 9 3
A getHtmlTag() 0 19 5
A getHtmlPre() 0 8 2
A getHtmlTableTfoot() 0 8 2
A getHtmlStrong() 0 5 2
A getHtmlParagraph() 0 8 2
A getHtmlTableThead() 0 8 2
A getHtmlLi() 0 5 2
A getHtmlTableHead() 0 6 3
A getHtmlEmpty() 0 3 1
A getHtmlSpan() 0 6 2
A getHtmlDiv() 0 8 2
A getHtmlI() 0 6 2
A getHtmlTableData() 0 6 3
A getHtmlUl() 0 8 2
A getInstance() 0 8 2

How to fix   Complexity   

Complex Class

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 namespace XoopsModules\Tdmcreate\Files;
2
3
use XoopsModules\Tdmcreate;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
/**
15
 * tdmcreate module.
16
 *
17
 * @copyright       XOOPS Project (https://xoops.org)
18
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
19
 *
20
 * @since           2.5.0
21
 *
22
 * @author          Txmod Xoops http://www.txmodxoops.org
23
 *
24
 * @version         $Id: CreateHtmlCode.php 12258 2014-01-02 09:33:29Z timgno $
25
 */
26
27
/**
28
 * Class CreateHtmlCode.
29
 */
30
class CreateHtmlCode
31
{
32
    /**
33
     *  @static function getInstance
34
     *
35
     *  @param null
36
     *
37
     * @return CreateHtmlCode
38
     */
39
    public static function getInstance()
40
    {
41
        static $instance = false;
42
        if (!$instance) {
43
            $instance = new self();
44
        }
45
46
        return $instance;
47
    }
48
49
    /**
50
    *  @public function getHtmlTag
51
     * @param string $tag
52
     * @param array  $attributes
53
     * @param string $content
54
     * @param bool   $noClosed
55
     * @param bool   $noBreak
56
     * @param string $t
57
     * @return string
58
     */
59
    public function getHtmlTag($tag = '', $attributes = [], $content = '', $noClosed = false, $noBreak = false, $t = '')
60
    {
61
        if (empty($attributes)) {
62
            $attributes = [];
63
        }
64
        $attr = $this->getAttributes($attributes);
65
        if ('br' === $tag) {
66
            $ret = "{$t}<{$tag}{$attr}>\n";
67
        } elseif ($noClosed) {
68
            $ret = "{$t}<{$tag}{$attr} />\n";
69
        } elseif ($noBreak) {
70
            $ret = "{$t}<{$tag}{$attr}>{$content}</{$tag}>\n";
71
        } else {
72
            $ret = "{$t}<{$tag}{$attr}>\n";
73
            $ret .= "{$t}{$content}";
74
            $ret .= "{$t}</{$tag}>\n";
75
        }
76
77
        return $ret;
78
    }
79
80
    /**
81
    *  @private function setAttributes
82
    *  @param array $attributes
83
    *
84
    * @return string
85
    */
86
    private function getAttributes($attributes)
87
    {
88
        $str = '';
89
        foreach ($attributes as $name => $value) {
90
            if ('_' !== $name) {
91
                $str .= ' '.$name.'="'.$value.'"';
92
            }
93
        }
94
95
        return $str;
96
    }
97
98
    /**
99
    *  @public function getHtmlEmpty
100
    *  @param string $empty
101
     *
102
     * @return string
103
     */
104
    public function getHtmlEmpty($empty = '')
105
    {
106
        return (string)$empty;
107
    }
108
109
    /**
110
    *  @public function getHtmlComment
111
    *  @param string $htmlComment
112
       * @return string
113
     */
114
    public function getHtmlComment($htmlComment = '')
115
    {
116
        return "<!-- {$htmlComment} -->";
117
    }
118
119
    /**
120
    *  @public function getHtmlBr
121
    *  @param int $brNumb
122
     * @param string $htmlClass
123
     * @param string $t
124
     * @return string
125
     */
126
    public function getHtmlBr($brNumb = 1, $htmlClass = '', $t = '')
127
    {
128
        $brClass = ('' != $htmlClass) ? " class='{$htmlClass}'" : '';
129
        $ret = '';
130
        for ($i = 0; $i < $brNumb; ++$i) {
131
            $ret .= "{$t}<br{$brClass} />\n";
132
        }
133
134
        return $ret;
135
    }
136
137
    /**
138
    *  @public function getHtmlHNumb
139
     * @param string $content
140
     * @param string $n
141
     * @param string $htmlHClass
142
     * @param string $t
143
     * @return string
144
     */
145
    public function getHtmlHNumb($content = '', $n = '1', $htmlHClass = '', $t = '')
146
    {
147
        $hClass = ('' != $htmlHClass) ? " class='{$htmlHClass}'" : '';
148
        $ret = "{$t}<h{$n}{$hClass}>{$content}</h{$n}>\n";
149
150
        return $ret;
151
    }
152
153
    /**
154
    *  @public function getHtmlDiv
155
     * @param string $content
156
     * @param string $divClass
157
     * @param string $t
158
     * @return string
159
     */
160
    public function getHtmlDiv($content = '', $divClass = '', $t = '')
161
    {
162
        $rDivClass = ('' != $divClass) ? " class='{$divClass}'" : '';
163
        $ret = "{$t}<div{$rDivClass}>\n";
164
        $ret .= "{$t}{$content}";
165
        $ret .= "{$t}</div>\n";
166
167
        return $ret;
168
    }
169
170
    /**
171
    *  @public function getHtmlPre
172
     * @param string $content
173
     * @param string $preClass
174
     * @param string $t
175
     * @return string
176
     */
177
    public function getHtmlPre($content = '', $preClass = '', $t = '')
178
    {
179
        $rPreClass = ('' != $preClass) ? " class='{$preClass}'" : '';
180
        $ret = "{$t}<pre{$rPreClass}>\n";
181
        $ret .= "{$t}{$content}";
182
        $ret .= "{$t}</pre>\n";
183
184
        return $ret;
185
    }
186
187
    /**
188
    *  @public function getHtmlSpan
189
    *  @param string $content
190
     * @param string $spanClass
191
     * @param string $t
192
     * @return string
193
     */
194
    public function getHtmlSpan($content = '', $spanClass = '', $t = '')
195
    {
196
        $rSpanClass = ('' != $spanClass) ? " class='{$spanClass}'" : '';
197
        $ret = "{$t}<span{$rSpanClass}>{$content}</span>\n";
198
199
        return $ret;
200
    }
201
202
    /**
203
    *  @public function getHtmlParagraph
204
     * @param string $content
205
     * @param string $pClass
206
     * @param string $t
207
     * @return string
208
     */
209
    public function getHtmlParagraph($content = '', $pClass = '', $t = '')
210
    {
211
        $rPClass = ('' != $pClass) ? " class='{$pClass}'" : '';
212
        $ret = "{$t}<p{$rPClass}>\n";
213
        $ret .= "{$t}{$content}";
214
        $ret .= "{$t}</p>\n";
215
216
        return $ret;
217
    }
218
219
    /**
220
    *  @public function getHtmlI
221
     * @param string $content
222
     * @param string $iClass
223
     * @param string $t
224
     * @return string
225
     */
226
    public function getHtmlI($content = '', $iClass = '', $t = '')
227
    {
228
        $rIClass = ('' != $iClass) ? " class='{$iClass}'" : '';
229
        $ret = "{$t}<i{$rIClass}>{$content}</i>";
230
231
        return $ret;
232
    }
233
234
    /**
235
    *  @public function getHtmlUl
236
     * @param string $content
237
     * @param string $ulClass
238
     * @param string $t
239
     * @return string
240
     */
241
    public function getHtmlUl($content = '', $ulClass = '', $t = '')
242
    {
243
        $rUlClass = ('' != $ulClass) ? " class='{$ulClass}'" : '';
244
        $ret = "{$t}<ul{$rUlClass}>\n";
245
        $ret .= "{$t}{$content}";
246
        $ret .= "{$t}</ul>\n";
247
248
        return $ret;
249
    }
250
251
    /**
252
    *  @public function getHtmlOl
253
     * @param string $content
254
     * @param string $olClass
255
     * @param string $t
256
     * @return string
257
     */
258
    public function getHtmlOl($content = '', $olClass = '', $t = '')
259
    {
260
        $rOlClass = ('' != $olClass) ? " class='{$olClass}'" : '';
261
        $ret = "{$t}<ol{$rOlClass}>\n";
262
        $ret .= "{$t}{$content}";
263
        $ret .= "{$t}</ol>\n";
264
265
        return $ret;
266
    }
267
268
    /**
269
    *  @public function getHtmlLi
270
     * @param string $content
271
     * @param string $liClass
272
     * @param string $t
273
     * @return string
274
     */
275
    public function getHtmlLi($content = '', $liClass = '', $t = '')
276
    {
277
        $rLiClass = ('' != $liClass) ? " class='{$liClass}'" : '';
278
279
        return "{$t}<li{$rLiClass}>{$content}</li>\n";
280
    }
281
282
    /**
283
    *  @public function getHtmlStrong
284
     * @param string $content
285
     * @param string $strongClass
286
     * @param string $t
287
     * @return string
288
     */
289
    public function getHtmlStrong($content = '', $strongClass = '', $t = '')
290
    {
291
        $rStrongClass = ('' != $strongClass) ? " class='{$strongClass}'" : '';
292
293
        return "{$t}<strong{$rStrongClass}>{$content}</strong>\n";
294
    }
295
296
    /**
297
    *  @public function getHtmlAnchor
298
     * @param string $url
299
     * @param string $content
300
     * @param string $title
301
     * @param string $target
302
     * @param string $aClass
303
     * @param string $rel
304
     * @param string $t
305
     * @return string
306
     */
307
    public function getHtmlAnchor($url = '#', $content = '&nbsp;', $title = '', $target = '', $aClass = '', $rel = '', $t = '')
308
    {
309
        $target = ('' != $target) ? " target='{$target}'" : '';
310
        $rAClass = ('' != $aClass) ? " class='{$aClass}'" : '';
311
        $rel = ('' != $rel) ? " rel='{$rel}'" : '';
312
313
        return "{$t}<a{$rAClass} href='{$url}' title='{$title}'{$target}{$rel}>{$content}</a>\n";
314
    }
315
316
    /**
317
    *  @public function getHtmlImage
318
    *  @param string $src
319
    *  @param string $alt
320
     * @param string $imgClass
321
     * @param string $t
322
     * @return string
323
     */
324
    public function getHtmlImage($src = 'blank.gif', $alt = 'blank.gif', $imgClass = '', $t = '')
325
    {
326
        $rImgClass = ('' != $imgClass) ? " class='{$imgClass}'" : '';
327
        $ret = "{$t}<img{$rImgClass} src='{$src}' alt='{$alt}' />\n";
328
329
        return $ret;
330
    }
331
332
    /**
333
    *  @public function getHtmlTable
334
     * @param string $content
335
     * @param string $tableClass
336
     * @param string $t
337
     * @return string
338
     */
339
    public function getHtmlTable($content = '', $tableClass = '', $t = '')
340
    {
341
        $rTableClass = ('' != $tableClass) ? " class='{$tableClass}'" : '';
342
        $ret = "{$t}<table{$rTableClass}>\n";
343
        $ret .= "{$t}{$content}";
344
        $ret .= "{$t}</table>\n";
345
346
        return $ret;
347
    }
348
349
    /**
350
    *  @public function getHtmlTableThead
351
     * @param string $content
352
     * @param string $theadClass
353
     * @param string $t
354
     * @return string
355
     */
356
    public function getHtmlTableThead($content = '', $theadClass = '', $t = '')
357
    {
358
        $rTheadClass = ('' != $theadClass) ? " class='{$theadClass}'" : '';
359
        $ret = "{$t}\t<thead{$rTheadClass}>\n";
360
        $ret .= "{$t}\t{$content}";
361
        $ret .= "{$t}\t</thead>\n";
362
363
        return $ret;
364
    }
365
366
    /**
367
    *  @public function getHtmlTableTbody
368
     * @param string $content
369
     * @param string $tbodyClass
370
     * @param string $t
371
     * @return string
372
     */
373
    public function getHtmlTableTbody($content = '', $tbodyClass = '', $t = '')
374
    {
375
        $rTbodyClass = ('' != $tbodyClass) ? " class='{$tbodyClass}'" : '';
376
        $ret = "{$t}\t<tbody{$rTbodyClass}>\n";
377
        $ret .= "{$t}\t{$content}";
378
        $ret .= "{$t}\t</tbody>\n";
379
380
        return $ret;
381
    }
382
383
    /**
384
    *  @public function getHtmlTableTfoot
385
     * @param string $content
386
     * @param string $tfootClass
387
     * @param string $t
388
     * @return string
389
     */
390
    public function getHtmlTableTfoot($content = '', $tfootClass = '', $t = '')
391
    {
392
        $rTfootClass = ('' != $tfootClass) ? " class='{$tfootClass}'" : '';
393
        $ret = "{$t}\t<tfoot{$rTfootClass}>\n";
394
        $ret .= "{$t}\t{$content}";
395
        $ret .= "{$t}\t</tfoot>\n";
396
397
        return $ret;
398
    }
399
400
    /**
401
    *  @public function getHtmlTableRow
402
     * @param string $content
403
     * @param string $trClass
404
     * @param string $t
405
     * @return string
406
      */
407
    public function getHtmlTableRow($content = '', $trClass = '', $t = '')
408
    {
409
        $rTrClass = ('' != $trClass) ? " class='{$trClass}'" : '';
410
        $ret = "{$t}\t<tr{$rTrClass}>\n";
411
        $ret .= "{$t}\t{$content}";
412
        $ret .= "{$t}\t</tr>\n";
413
414
        return $ret;
415
    }
416
417
    /**
418
    *  @public function getHtmlTableHead
419
     * @param string $content
420
     * @param string $thClass
421
     * @param string $colspan
422
     * @param string $t
423
     * @return string
424
     */
425
    public function getHtmlTableHead($content = '', $thClass = '', $colspan = '', $t = '')
426
    {
427
        $rThClass = ('' != $thClass) ? " class='{$thClass}'" : '';
428
        $colspan = ('' != $colspan) ? " colspan='{$colspan}'" : '';
429
430
        return "{$t}<th{$colspan}{$rThClass}>{$content}</th>\n";
431
    }
432
433
    /**
434
    *  @public function getHtmlTableData
435
     * @param string $content
436
     * @param string $tdClass
437
     * @param string $colspan
438
     * @param string $t
439
     * @return string
440
     */
441
    public function getHtmlTableData($content = '', $tdClass = '', $colspan = '', $t = '')
442
    {
443
        $rTdClass = ('' != $tdClass) ? " class='{$tdClass}'" : '';
444
        $colspan = ('' != $colspan) ? " colspan='{$colspan}'" : '';
445
446
        return "{$t}<td{$colspan}{$rTdClass}>{$content}</td>\n";
447
    }
448
}
449