Passed
Push — master ( 4674d5...c94053 )
by Goffy
40s queued 13s
created

CreatePhpCode   F

Complexity

Total Complexity 87

Size/Duplication

Total Lines 700
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 189
dl 0
loc 700
rs 2
c 0
b 0
f 0
wmc 87

39 Methods

Rating   Name   Duplication   Size   Complexity  
A getPhpCodeClass() 0 10 3
A getPhpCodeIsset() 0 3 1
B getPhpCodeForeach() 0 17 8
A getPhpCodeTernaryOperator() 0 3 1
A getPhpCodePregFunzions() 0 10 2
A getPhpCodeSwitch() 0 8 1
A getPhpCodeFor() 0 7 1
A getPhpCodeExplode() 0 3 1
A getPhpCodeWhile() 0 7 1
A getPhpCodeConditions() 0 15 2
A getPhpCodeStripTags() 0 9 2
A getPhpCodeGlobals() 0 9 2
A getPhpCodeHtmlspecialchars() 0 6 2
A getPhpCodeVariableClass() 0 3 1
A getInstance() 0 8 2
A getPhpCodeSprintf() 0 3 1
A getPhpCodeRemoveCarriageReturn() 0 3 1
A getPhpCodeIncludeDir() 0 20 5
A getPhpCodeUnset() 0 3 1
A getPhpCodeFileExists() 0 3 1
A getPhpCodeCommentMultiLine() 0 14 5
A getPhpCodeCommentLine() 0 6 2
A getPhpCodeDefine() 0 3 1
A getPhpCodeEmpty() 0 3 1
A getPhpCodeIsDir() 0 3 1
A getPhpCodeMkdir() 0 3 1
A getPhpCodeFunction() 0 10 3
A getPhpCodeChmod() 0 3 1
A getPhpCodeImplode() 0 3 1
A getPhpCodeHeader() 0 3 1
A getPhpCodeDefined() 0 3 1
A getPhpCodeCopy() 0 3 1
A getPhpCodeArrayType() 0 10 3
A getPhpCodeArray() 0 24 6
A getPhpCodeRawurlencode() 0 3 1
A getPhpCodeStrType() 0 10 2
A getPhpCodeHtmlentities() 0 6 2
B getPhpCodeGlobalsVariables() 0 28 7
B getPhpCodeCaseSwitch() 0 27 8

How to fix   Complexity   

Complex Class

Complex classes like CreatePhpCode 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 CreatePhpCode, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace XoopsModules\Tdmcreate\Files;
4
5
use XoopsModules\Tdmcreate;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
 */
16
/**
17
 * tdmcreate module.
18
 *
19
 * @copyright       XOOPS Project (https://xoops.org)
20
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
21
 *
22
 * @since           2.5.0
23
 *
24
 * @author          Txmod Xoops http://www.txmodxoops.org
25
 *
26
 */
27
28
/**
29
 * Class CreatePhpCode.
30
 */
31
class CreatePhpCode
32
{
33
    /**
34
     * @static function getInstance
35
     * @param null
36
     * @return CreatePhpCode
37
     */
38
    public static function getInstance()
39
    {
40
        static $instance = false;
41
        if (!$instance) {
42
            $instance = new self();
43
        }
44
45
        return $instance;
46
    }
47
48
    /**
49
     * @public function getPhpCodeCommentLine
50
     * @param        $comment
51
     * @param        $var
52
     * @param string $t
53
     * @return string
54
     */
55
    public function getPhpCodeCommentLine($comment = null, $var = null, $t = '')
56
    {
57
        $value = !empty($var) ? ' ' . $var : '';
58
        $ret   = "{$t}// {$comment}{$value}\n";
59
60
        return $ret;
61
    }
62
63
    /**
64
     * @public function getPhpCodeCommentMultiLine
65
     * @param array  $multiLine
66
     *
67
     * @param string $t
68
     * @return string
69
     */
70
    public function getPhpCodeCommentMultiLine($multiLine = [], $t = '')
71
    {
72
        $values = !empty($multiLine) ? $multiLine : [];
73
        $ret    = "\n{$t}/**\n";
74
        foreach ($values as $string => $value) {
75
            if ('' === $string && '' === $value) {
76
                $ret .= "{$t} *\n";
77
            } else {
78
                $ret .= "{$t} * {$string} {$value}\n";
79
            }
80
        }
81
        $ret .= "{$t} */\n";
82
83
        return $ret;
84
    }
85
86
    /**
87
     * @public function getPhpCodeDefine
88
     * @param $left
89
     * @param $right
90
     *
91
     * @return string
92
     */
93
    public function getPhpCodeDefine($left, $right)
94
    {
95
        return "define('{$left}', {$right});\n";
96
    }
97
98
    /**
99
     * @public function getPhpCodeDefine
100
     * @param $left
101
     * @param $right
102
     *
103
     * @return string
104
     */
105
    public function getPhpCodeDefined($left = 'XOOPS_ROOT_PATH', $right = 'Restricted access')
106
    {
107
        return "defined('{$left}') || die('{$right}');\n";
108
    }
109
110
    /**
111
     * @public function getPhpCodeGlobals
112
     * @param $var
113
     * @param $value
114
     *
115
     * @return string
116
     */
117
    public function getPhpCodeGlobals($var, $value = '')
118
    {
119
        if ('' != $value) {
120
            $ret = "\$GLOBALS['{$var}'] = \${$value};\n";
121
        } else {
122
            $ret = "\$GLOBALS['{$var}']";
123
        }
124
125
        return $ret;
126
    }
127
128
    /**
129
     * @public function getPhpCodeGlobalsVariables
130
     * @param $var
131
     * @param $type
132
     *
133
     * @return string
134
     */
135
    public function getPhpCodeGlobalsVariables($var = null, $type = 'REQUEST')
136
    {
137
        $type = mb_strtoupper($type);
138
        switch ($type) {
139
            case 'GET':
140
                $ret = "\$_GET['{$var}']";
141
                break;
142
            case 'POST':
143
                $ret = "\$_POST['{$var}']";
144
                break;
145
            case 'FILES':
146
                $ret = "\$_FILES['{$var}']";
147
                break;
148
            case 'COOKIE':
149
                $ret = "\$_COOKIE['{$var}']";
150
                break;
151
            case 'ENV':
152
                $ret = "\$_ENV['{$var}']";
153
                break;
154
            case 'SERVER':
155
                $ret = "\$_SERVER['{$var}']";
156
                break;
157
            default:
158
                $ret = "\$_REQUEST['{$var}']";
159
                break;
160
        }
161
162
        return $ret;
163
    }
164
165
    /**
166
     * @public function getPhpCodeRemoveCarriageReturn
167
     * @param        $string
168
     *
169
     * @param string $n
170
     * @param string $t
171
     * @return string
172
     */
173
    public function getPhpCodeRemoveCarriageReturn($string, $n = "\n", $t = "\r")
174
    {
175
        return str_replace([(string)$n, (string)$t], '', $string);
176
    }
177
178
    /**
179
     * @public function getPhpCodeFileExists
180
     * @param $filename
181
     *
182
     * @return string
183
     */
184
    public function getPhpCodeFileExists($filename)
185
    {
186
        return "file_exists({$filename})";
187
    }
188
189
    /**
190
     * @public function getPhpCodeIncludeDir
191
     * @param        $directory
192
     * @param        $filename
193
     * @param bool   $once
194
     * @param bool   $isPath
195
     *
196
     * @param string $type
197
     * @param string $t
198
     * @return string
199
     */
200
    public function getPhpCodeIncludeDir($directory = null, $filename = null, $once = false, $isPath = false, $type = 'include', $t = '')
201
    {
202
        if ('' === $type) {
203
            $type = 'include';
204
        }
205
        if (false === $once) {
206
            if (!$isPath) {
207
                $ret = "{$t}{$type} {$directory} .'/{$filename}.php';\n";
208
            } else {
209
                $ret = "{$t}{$type} {$directory};\n";
210
            }
211
        } else {
212
            if (!$isPath) {
213
                $ret = "{$t}{$type}_once {$directory} .'/{$filename}.php';\n";
214
            } else {
215
                $ret = "{$t}{$type}_once {$directory};\n";
216
            }
217
        }
218
219
        return $ret;
220
    }
221
222
    /**
223
     * @public function getPhpCodeTernaryOperator
224
     * @param $return
225
     * @param $condition
226
     * @param $one
227
     * @param $two
228
     * @param $t - Indentation
0 ignored issues
show
Documentation Bug introduced by
The doc comment - at position 0 could not be parsed: Unknown type name '-' at position 0 in -.
Loading history...
229
     *
230
     * @return string
231
     */
232
    public function getPhpCodeTernaryOperator($return, $condition, $one, $two, $t = '')
233
    {
234
        return "{$t}\${$return} = {$condition} ? {$one} : {$two};\n";
235
    }
236
237
    /**
238
     * @public function getPhpCodeClass
239
     * @param $name
240
     * @param $content
241
     * @param $extends
242
     * @param $type
243
     *
244
     * @return string
245
     */
246
    public function getPhpCodeClass($name = null, $content = null, $extends = null, $type = null)
247
    {
248
        $typ = (null != $type) ? "{$type} " : '';
249
        $ext = (null != $extends) ? " extends {$extends}" : '';
250
        $ret = "{$typ}class {$name}{$ext}\n";
251
        $ret .= '{';
252
        $ret .= $content;
253
        $ret .= "}\n";
254
255
        return $ret;
256
    }
257
258
    /**
259
     * @public function getPhpCodeClass
260
     * @param $type
261
     * @param $name
262
     * @param $assign
263
     * @param $t - Indentation
0 ignored issues
show
Documentation Bug introduced by
The doc comment - at position 0 could not be parsed: Unknown type name '-' at position 0 in -.
Loading history...
264
     *
265
     * @return string
266
     */
267
    public function getPhpCodeVariableClass($type = 'private', $name = null, $assign = 'null', $t = '')
268
    {
269
        return "{$t}{$type} \${$name} = {$assign};\n";
270
    }
271
272
    /**
273
     * @public function getPhpCodeFunction
274
     * @param        $name
275
     * @param        $params
276
     * @param        $content
277
     * @param        $method
278
     * @param bool   $isRef
279
     * @param string $t - Indentation
280
     * @return string
281
     */
282
    public function getPhpCodeFunction($name = null, $params = null, $content = null, $method = null, $isRef = false, $t = '')
283
    {
284
        $inClass = (null != $method) ? $method : '';
285
        $ref     = (false !== $isRef) ? '&' : '';
286
        $ret     = "{$t}{$inClass}function {$ref}{$name}({$params})\n";
287
        $ret     .= "{$t}{\n";
288
        $ret     .= $content;
289
        $ret     .= "{$t}}\n";
290
291
        return $ret;
292
    }
293
294
    /**
295
     * @public function getPhpCodeConditions
296
     * @param string $condition
297
     * @param string $operator
298
     * @param string $type
299
     * @param string $contentIf
300
     * @param mixed  $contentElse
301
     * @param        $t - Indentation
0 ignored issues
show
Documentation Bug introduced by
The doc comment - at position 0 could not be parsed: Unknown type name '-' at position 0 in -.
Loading history...
302
     *
303
     * @return string
304
     */
305
    public function getPhpCodeConditions($condition = null, $operator = null, $type = null, $contentIf = null, $contentElse = false, $t = '')
306
    {
307
        if (false === $contentElse) {
308
            $ret = "{$t}if({$condition}{$operator}{$type}) {\n";
309
            $ret .= $contentIf;
310
            $ret .= "{$t}}\n";
311
        } else {
312
            $ret = "{$t}if({$condition}{$operator}{$type}) {\n";
313
            $ret .= $contentIf;
314
            $ret .= "{$t}} else {\n";
315
            $ret .= $contentElse;
316
            $ret .= "{$t}}\n";
317
        }
318
319
        return $ret;
320
    }
321
322
    /**
323
     * @public function getPhpCodeForeach
324
     * @param string      $array
325
     * @param bool|string $arrayKey
326
     * @param bool|string $key
327
     * @param bool|string $value
328
     * @param string      $content
329
     *
330
     * @param string      $t
331
     * @return string
332
     */
333
    public function getPhpCodeForeach($array, $arrayKey = false, $key = false, $value = false, $content = null, $t = '')
334
    {
335
        $vars  = '';
336
        $value = '' !== $value ? $value : 'i';
337
        if ((false === $arrayKey) && (false === $key)) {
338
            $vars = "\${$array} as \${$value}";
339
        } elseif ((false === $arrayKey) && (false !== $key)) {
340
            $vars = "\${$array} as \${$key} => \${$value}";
341
        } elseif ((false !== $arrayKey) && (false === $key)) {
342
            $vars = "array_keys(\${$array}) as \${$value}";
343
        }
344
345
        $ret = "{$t}foreach({$vars}) {\n";
346
        $ret .= "{$t}{$content}";
347
        $ret .= "{$t}}\n";
348
349
        return $ret;
350
    }
351
352
    /**
353
     * @public function getPhpCodeFor
354
     * @param        $var
355
     * @param        $content
356
     * @param        $value
357
     * @param        $initVal
358
     * @param        $operator
359
     *
360
     * @param string $t
361
     * @return string
362
     */
363
    public function getPhpCodeFor($var = null, $content = null, $value = null, $initVal = null, $operator = null, $t = '')
364
    {
365
        $ret = "{$t}for(\${$var} = {$initVal}; {$var} {$operator} \${$value}; \${$var}++) {\n";
366
        $ret .= "{$t}{$content}";
367
        $ret .= "{$t}}\n";
368
369
        return $ret;
370
    }
371
372
    /**
373
     * @public function getPhpCodeWhile
374
     * @param $var
375
     * @param $content
376
     * @param $value
377
     * @param $operator
378
     * @param $t
379
     *
380
     * @return string
381
     */
382
    public function getPhpCodeWhile($var = null, $content = null, $value = null, $operator = null, $t = '')
383
    {
384
        $ret = "{$t}while(\${$var} {$operator} {$value}) {\n";
385
        $ret .= "{$t}{$content}";
386
        $ret .= "{$t}}\n";
387
388
        return $ret;
389
    }
390
391
    /**
392
     * @public function getPhpCodeSwitch
393
     *
394
     * @param $op
395
     * @param $content
396
     * @param $t
397
     *
398
     * @return string
399
     */
400
    public function getPhpCodeSwitch($op = null, $content = null, $t = '')
401
    {
402
        //$ret = "{$t}switch(\${$op}) {\n"; test goffy
403
        $ret = "switch(\${$op}) {\n";
404
        $ret .= $content;
405
        $ret .= "}\n";
406
407
        return $ret;
408
    }
409
410
    /**
411
     * @public function getPhpCodeCaseSwitch
412
     *
413
     * @param $cases
414
     * @param $defaultAfterCase
415
     * @param $default
416
     * @param $t
417
     *
418
     * @return string
419
     */
420
    public function getPhpCodeCaseSwitch($cases = [], $defaultAfterCase = false, $default = false, $t = '')
421
    {
422
        $ret = '';
423
        $def = "{$t}default:\n";
424
        foreach ($cases as $case => $value) {
425
            $case = is_string($case) ? "'{$case}'" : $case;
426
            if (!empty($case)) {
427
                $ret .= "{$t}case {$case}:\n";
428
                if (false !== $defaultAfterCase) {
429
                    $ret .= $def;
430
                }
431
                if (is_array($value)) {
432
                    foreach ($value as $content) {
433
                        $ret .= "{$t}{$t}{$content}\n";
434
                    }
435
                }
436
                $ret .= "{$t}break;\n";
437
            }
438
            $defaultAfterCase = false;
439
        }
440
        if (false !== $default) {
441
            $ret .= $def;
442
            $ret .= "{$t}{$default}\n";
443
            $ret .= "{$t}break;\n";
444
        }
445
446
        return $ret;
447
    }
448
449
    /**
450
     * @public function getPhpCodeIsset
451
     * @param $var
452
     * @return string
453
     */
454
    public function getPhpCodeIsset($var)
455
    {
456
        return "isset(\${$var})";
457
    }
458
459
    /**
460
     * @public function getPhpCodeUnset
461
     * @param string $var
462
     * @param string $t
463
     * @return string
464
     */
465
    public function getPhpCodeUnset($var = '', $t = '')
466
    {
467
        return "{$t}unset(\${$var});\n";
468
    }
469
470
    /**
471
     * @public function getPhpCodeIsDir
472
     * @param $var
473
     * @return string
474
     */
475
    public function getPhpCodeIsDir($var)
476
    {
477
        return "is_dir({$var})";
478
    }
479
480
    /**
481
     * @public function getPhpCodeImplode
482
     * @param $left
483
     * @param $right
484
     * @return string
485
     */
486
    public function getPhpCodeImplode($left, $right)
487
    {
488
        return "implode('{$left}', {$right})";
489
    }
490
491
    /**
492
     * @public function getPhpCodeExplode
493
     * @param $left
494
     * @param $right
495
     * @return string
496
     */
497
    public function getPhpCodeExplode($left, $right)
498
    {
499
        return "explode('{$left}', {$right})";
500
    }
501
502
    /**
503
     * @public function getPhpCodeChmod
504
     * @param        $var
505
     * @param string $perm
506
     * @param string $t
507
     * @return string
508
     */
509
    public function getPhpCodeChmod($var, $perm = '0777', $t = '')
510
    {
511
        return "{$t}chmod(\${$var}, {$perm});\n";
512
    }
513
514
    /**
515
     * @public function getPhpCodeMkdir
516
     * @param        $var
517
     * @param string $perm
518
     * @param string $t
519
     * @return string
520
     */
521
    public function getPhpCodeMkdir($var, $perm = '0777', $t = '')
522
    {
523
        return "{$t}mkdir(\${$var}, {$perm});\n";
524
    }
525
526
    /**
527
     * @public function getPhpCodeCopy
528
     * @param        $file
529
     * @param string $newfile
530
     * @param string $t
531
     * @return string
532
     */
533
    public function getPhpCodeCopy($file, $newfile = '', $t = '')
534
    {
535
        return "{$t}copy({$file}, {$newfile});\n";
536
    }
537
538
    /**
539
     * @public function getPhpCodeArray
540
     * @param        $var
541
     * @param        $array
542
     * @param bool   $isParam
543
     *
544
     * @param string $t
545
     * @return string
546
     */
547
    public function getPhpCodeArray($var, $array = null, $isParam = false, $t = "\t\t")
548
    {
549
        $retArray = [];
550
        if (is_array($array) && !empty($array)) {
551
            foreach ($array as $k => $v) {
552
                if (is_numeric($k)) {
553
                    $retArray[] = $v;
554
                } else {
555
                    $retArray[] = "{$k} => {$v}";
556
                }
557
            }
558
            $arrayContent = implode(', ', $retArray);
559
        } else {
560
            $arrayContent = '';
561
        }
562
        unset($retArray);
563
564
        if (!$isParam) {
565
            $ret = "{$t}\${$var} = array({$arrayContent});\n";
566
        } else {
567
            $ret = "array({$array})";
568
        }
569
570
        return $ret;
571
    }
572
573
    /**
574
     * @public function getPhpCodeArrayType
575
     * @param        $var
576
     * @param        $type
577
     * @param        $left
578
     * @param        $right
579
     * @param bool   $isParam
580
     *
581
     * @param string $t
582
     * @return string
583
     */
584
    public function getPhpCodeArrayType($var, $type, $left, $right = null, $isParam = false, $t = "\t\t")
585
    {
586
        $vars = (null != $right) ? "\${$left}, {$right}" : "\${$left}";
587
        if (!$isParam) {
588
            $ret = "{$t}\${$var}[] = array_{$type}({$vars});\n";
589
        } else {
590
            $ret = "array_{$type}({$vars})";
591
        }
592
593
        return $ret;
594
    }
595
596
    /**
597
     * @public function getPhpCodeSprintf
598
     * @param $left
599
     * @param $right
600
     * @return string
601
     */
602
    public function getPhpCodeSprintf($left, $right)
603
    {
604
        return "sprintf({$left}, {$right})";
605
    }
606
607
    /**
608
     * @public function getPhpCodeEmpty
609
     * @param $var
610
     * @return string
611
     */
612
    public function getPhpCodeEmpty($var)
613
    {
614
        return "empty({$var})";
615
    }
616
617
    /**
618
     * @public function getPhpCodeHeader
619
     * @param $var
620
     * @return string
621
     */
622
    public function getPhpCodeHeader($var)
623
    {
624
        return "header({$var})";
625
    }
626
627
    /**
628
     * @public function getPhpCodeRawurlencode
629
     * @param $var
630
     * @return string
631
     */
632
    public function getPhpCodeRawurlencode($var)
633
    {
634
        return "rawurlencode({$var})";
635
    }
636
637
    /**
638
     * @public function getPhpCodePregFunzions
639
     * @param        $var
640
     * @param        $exp
641
     * @param        $str
642
     * @param        $val
643
     * @param string $type
644
     * @param bool   $isParam
645
     *
646
     * @param string $t
647
     * @return string
648
     */
649
    public function getPhpCodePregFunzions($var, $exp, $str, $val, $type = 'match', $isParam = false, $t = "\t")
650
    {
651
        $pregFunz = "preg_{$type}('";
652
        if (!$isParam) {
653
            $ret = "{$t}\${$var} = {$pregFunz}{$exp}', '{$str}', {$val});\n";
654
        } else {
655
            $ret = "{$pregFunz}{$exp}', '{$str}', {$val})";
656
        }
657
658
        return $ret;
659
    }
660
661
    /**
662
     * @public function getPhpCodeStrType
663
     * @param        $left
664
     * @param        $var
665
     * @param        $str
666
     * @param        $value
667
     * @param string $type
668
     * @param bool   $isParam
669
     *
670
     * @param string $t
671
     * @return string
672
     */
673
    public function getPhpCodeStrType($left, $var, $str, $value, $type = 'replace', $isParam = false, $t = "\t")
674
    {
675
        $strType = "str_{$type}('";
676
        if (!$isParam) {
677
            $ret = "{$t}\${$left} = {$strType}{$var}', '{$str}', {$value});\n";
678
        } else {
679
            $ret = "{$strType}{$var}', '{$str}', {$value})";
680
        }
681
682
        return $ret;
683
    }
684
685
    /**
686
     * @public function getPhpCodeStripTags
687
     * @param        $left
688
     * @param        $value
689
     * @param bool   $isParam
690
     *
691
     * @param string $t
692
     * @return string
693
     */
694
    public function getPhpCodeStripTags($left, $value, $isParam = false, $t = '')
695
    {
696
        if (!$isParam) {
697
            $ret = "{$t}\${$left} = strip_tags({$value});\n";
698
        } else {
699
            $ret = "strip_tags({$value})";
700
        }
701
702
        return $ret;
703
    }
704
705
    /**
706
     * @public function getPhpCodeHtmlentities
707
     * @param $entitiesVar
708
     * @param $entitiesQuote
709
     * @return string
710
     */
711
    public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false)
712
    {
713
        $entitiesVar = (false !== $entitiesQuote) ? $entitiesVar . ', ' . $entitiesQuote : $entitiesVar;
714
        $entities    = "htmlentities({$entitiesVar})";
715
716
        return $entities;
717
    }
718
719
    /**
720
     * @public function getPhpCodeHtmlspecialchars
721
     * @param $specialVar
722
     * @param $specialQuote
723
     * @return string
724
     */
725
    public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false)
726
    {
727
        $specialVar   = (false !== $specialQuote) ? $specialVar . ', ' . $specialQuote : $specialVar;
728
        $specialchars = "htmlspecialchars({$specialVar})";
729
730
        return $specialchars;
731
    }
732
}
733