Completed
Pull Request — master (#48)
by Gino
03:19
created

TDMCreatePhpCode   C

Complexity

Total Complexity 54

Size/Duplication

Total Lines 452
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0
Metric Value
wmc 54
lcom 0
cbo 0
dl 0
loc 452
rs 6.8539

25 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getPhpCodeCommentLine() 0 4 1
A getInstance() 0 9 2
C getPhpCodeGlobalsVariables() 0 29 7
A getPhpCodeRemoveCarriageReturn() 0 4 1
A getPhpCodeFileExists() 0 4 1
A getPhpCodeIncludeDir() 0 18 4
A getPhpCodeConditions() 0 20 2
B getPhpCodeForeach() 0 19 7
A getPhpCodeFor() 0 10 1
A getPhpCodeWhile() 0 10 1
A getPhpCodeSwitch() 0 11 1
C getPhpCodeCaseSwitch() 0 28 8
A getPhpCodeIsset() 0 4 1
A getPhpCodeUnset() 0 4 1
A getPhpCodeImplode() 0 4 1
A getPhpCodeSprintf() 0 4 1
A getPhpCodeEmpty() 0 4 1
A getPhpCodeHeader() 0 4 1
A getPhpCodeRawurlencode() 0 4 1
A getPhpCodePregReplace() 0 10 2
A getPhpCodeStrReplace() 0 10 2
A getPhpCodeStripTags() 0 10 2
A getPhpCodeHtmlentities() 0 7 2
A getPhpCodeHtmlspecialchars() 0 7 2

How to fix   Complexity   

Complex Class

Complex classes like TDMCreatePhpCode 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 TDMCreatePhpCode, and based on these observations, apply Extract Interface, too.

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 28 and the first side effect is on line 24.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
/**
13
 * tdmcreate module.
14
 *
15
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 *
18
 * @since           2.5.0
19
 *
20
 * @author          Txmod Xoops http://www.txmodxoops.org
21
 *
22
 * @version         $Id: TDMCreatePhpCode.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
defined('XOOPS_ROOT_PATH') || die('Restricted access');
25
/**
26
 * Class TDMCreatePhpCode.
27
 */
28
class TDMCreatePhpCode
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
29
{
30
    /*
31
    *  @public function constructor
32
    *  @param null
33
    */
34
    /**
35
     *
36
     */
37
    public function __construct()
38
    {
39
    }
40
41
    /*
42
    *  @static function &getInstance
43
    *  @param null
44
    */
45
    /**
46
     * @return TDMCreatePhpCode
47
     */
48
    public static function &getInstance()
49
    {
50
        static $instance = false;
51
        if (!$instance) {
52
            $instance = new self();
53
        }
54
55
        return $instance;
56
    }
57
58
    /*
59
    *  @public function getPhpCodeCommentLine
60
    *  @param $comment
61
    *  @param $var
62
    *  @return string
63
    */
64
    public function getPhpCodeCommentLine($comment = '', $var = '')
65
    {
66
        return "// {$comment} {$var}\n";
67
    }
68
69
    /*
70
    *  @public function getPhpCodeGlobalsVariables
71
    *  @param $type
72
    *  @param $var
73
	*
74
    *  @return string
75
    */
76
    public function getPhpCodeGlobalsVariables($type = 'REQUEST', $var = '')
77
    {
78
        $type = strtoupper($type);
79
        switch ($type) {
80
            case 'GET':
81
                $ret = "\$_GET['{$var}']";
82
                break;
83
            case 'POST':
84
                $ret = "\$_POST['{$var}']";
85
                break;
86
            case 'FILES':
87
                $ret = "\$_FILES['{$var}']";
88
                break;
89
            case 'COOKIE':
90
                $ret = "\$_COOKIE['{$var}']";
91
                break;
92
            case 'ENV':
93
                $ret = "\$_ENV['{$var}']";
94
                break;
95
            case 'SERVER':
96
                $ret = "\$_SERVER['{$var}']";
97
                break;
98
            default:
99
                $ret = "\$_REQUEST['{$var}']";
100
                break;
101
        }
102
103
        return $ret;
104
    }
105
	
106
	/*
107
     * @public function getPhpCodeRemoveCarriageReturn
108
     * @param $string     
109
     *
110
     * @return string
111
     */
112
    public function getPhpCodeRemoveCarriageReturn($string)
113
    {
114
        return str_replace(array("\n", "\r"), '', $string);
115
    }
116
117
    /*
118
    *  @public function getPhpCodeFileExists
119
    *  @param $filename
120
    *
121
    *  @return string
122
    */
123
    public function getPhpCodeFileExists($filename)
124
    {
125
        return "file_exists({$filename})";
126
    }
127
128
    /*
129
    *  @public function getPhpCodeIncludeDir
130
    *  @param $directory
131
    *  @param $filename
132
    *  @param $once
133
    *  @param $isPath
134
    *
135
    *  @return string
136
    */
137
    public function getPhpCodeIncludeDir($directory = '', $filename = '', $once = false, $isPath = false)
138
    {
139
        if ($once === false) {
140
            if ($isPath === false) {
141
                $ret = "include {$directory} .'/{$filename}.php';\n";
142
            } else {
143
                $ret = "include {$directory};\n";
144
            }
145
        } else {
146
            if ($isPath === false) {
147
                $ret = "include_once {$directory} .'/{$filename}.php';\n";
148
            } else {
149
                $ret = "include_once {$directory};\n";
150
            }
151
        }
152
153
        return $ret;
154
    }
155
156
    /*
157
     * @public function getPhpCodeConditions
158
     * @param string $condition
159
     * @param string $operator
160
     * @param string $type
161
     * @param string $contentIf
162
     * @param mixed  $contentElse
163
     *
164
     * @return string
165
     */
166
    public function getPhpCodeConditions($condition = '', $operator = '', $type = '', $contentIf = '', $contentElse = false, $t = '')
167
    {
168
        if (false === $contentElse) {
169
            $ret = <<<EOT
170
{$t}if ({$condition}{$operator}{$type}) {
171
{$t}{$t}{$contentIf}
172
{$t}}\n
173
EOT;
174
        } else {
175
            $ret = <<<EOT
176
{$t}if ({$condition}{$operator}{$type}) {
177
{$t}{$t}{$contentIf}
178
{$t}} else {
179
{$t}{$t}{$contentElse}
180
{$t}}\n
181
EOT;
182
        }
183
184
        return $ret;
185
    }
186
187
    /*
188
     * @public function getPhpCodeForeach
189
     * @param string $array
190
     * @param string $arrayKey     
191
     * @param string $key
192
     * @param string $value     
193
     * @param string $content
194
     *
195
     * @return string
196
     */
197
    public function getPhpCodeForeach($array = '', $arrayKey = false, $key = false, $value = false, $content = '', $t = '')
198
    {
199
        $vars = '';
200
        if ((false === $arrayKey) && (false === $key)) {
201
            $vars = "\${$array} as \${$value}";
202
        } elseif ((false === $arrayKey) && (false !== $key)) {
203
            $vars = "\${$array} as \${$key} => \${$value}";
204
        } elseif ((false !== $arrayKey) && (false === $key)) {
205
            $vars = "array_keys(\${$array}) as \${$value}";
206
        }
207
208
        $ret = <<<EOT
209
{$t}foreach({$vars}) {
210
{$t}{$t}{$content}
211
{$t}}\n
212
EOT;
213
214
        return $ret;
215
    }
216
217
    /*
218
     * @public function getPhpCodeFor
219
     * @param $var
220
     * @param $content
221
     * @param $value
222
     * @param $initVal
223
     * @param $operator
224
     *
225
     * @return string
226
     */
227
    public function getPhpCodeFor($var = '', $content = '', $value = '', $initVal = '', $operator = '', $t = '')
228
    {
229
        $ret = <<<EOT
230
{$t}for(\${$var} = {$initVal}; \${$var} {$operator} \${$value}; \${$var}++) {
231
{$t}{$t}{$content}
232
{$t}}\n
233
EOT;
234
235
        return $ret;
236
    }
237
238
    /*
239
     * @public function getPhpCodeWhile
240
     * @param $var
241
     * @param $content
242
     * @param $value
243
     * @param $operator
244
	 *  @param $t
245
     *
246
     * @return string
247
     */
248
    public function getPhpCodeWhile($var = '', $content = '', $value = '', $operator = '', $t = '')
249
    {
250
        $ret = <<<EOT
251
{$t}while(\${$var} {$operator} {$value}) {
252
{$t}{$t}{$content}
253
{$t}}\n
254
EOT;
255
256
        return $ret;
257
    }
258
259
    /**
260
     *  @public function getPhpCodeSwitch
261
     *
262
     *  @param $op
263
     *  @param $content
264
	 *  @param $t
265
     *
266
     *  @return string
267
     */
268
    public function getPhpCodeSwitch($op = '', $content = '', $t = '')
269
    {
270
        $ret = <<<EOT
271
// Switch options
272
{$t}switch (\${$op}){
273
{$t}{$t}{$content}
274
{$t}}\n
275
EOT;
276
277
        return $ret;
278
    }
279
280
    /**
281
     *  @public function getPhpCodeCaseSwitch     
282
     *
283
     *  @param $cases
284
     *  @param $defaultAfterCase
285
     *  @param $default
286
	 *  @param $t
287
     *
288
     *  @return string
289
     */
290
    public function getPhpCodeCaseSwitch($cases = array(), $defaultAfterCase = false, $default = false, $t = "\t")
291
    {
292
        $ret = '';
293
        $def = "{$t}default:\n";
294
        foreach ($cases as $case => $value) {
295
            $case = is_string($case) ? "'{$case}'" : $case;
296
            if (!empty($case)) {
297
                $ret .= "{$t}case {$case}:\n";
298
                if ($defaultAfterCase != false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
299
                    $ret .= $def;
300
                }
301
                if (is_array($value)) {
302
                    foreach ($value as $content) {
303
                        $ret .= "{$t}{$t}{$content}\n";
304
                    }
305
                }
306
                $ret .= "{$t}break;\n";
307
            }
308
            $defaultAfterCase = false;
309
        }
310
        if ($default !== false) {
311
            $ret .= $def;
312
            $ret .= "{$t}{$default}\n";
313
            $ret .= "{$t}break;\n";
314
        }
315
316
        return $ret;
317
    }
318
319
    /*
320
    *  @public function getPhpCodeIsset
321
    *  @param $var
322
    *  @return string
323
    */
324
    public function getPhpCodeIsset($var)
325
    {
326
        return "isset(\${$var})";
327
    }
328
329
    /*
330
    *  @public function getPhpCodeUnset
331
    *  @param $var
332
    *  @return string
333
    */
334
    public function getPhpCodeUnset($var = '')
335
    {
336
        return "unset(\${$var});\n";
337
    }
338
339
    /*
340
    *  @public function getPhpCodeImplode
341
    *  @param $left
342
    *  @param $right
343
    *  @return string
344
    */
345
    public function getPhpCodeImplode($left, $right)
346
    {
347
        return "implode('{$left} ', {$right})";
348
    }
349
350
    /*
351
    *  @public function getPhpCodeSprintf
352
    *  @param $left
353
    *  @param $right
354
    *  @return string
355
    */
356
    public function getPhpCodeSprintf($left, $right)
357
    {
358
        return "sprintf({$left}, {$right})";
359
    }
360
361
    /*
362
    *  @public function getPhpCodeEmpty
363
    *  @param $var
364
    *  @return string
365
    */
366
    public function getPhpCodeEmpty($var)
367
    {
368
        return "empty({$var})";
369
    }
370
371
    /*
372
    *  @public function getPhpCodeHeader
373
    *  @param $var
374
    *  @return string
375
    */
376
    public function getPhpCodeHeader($var)
377
    {
378
        return "header({$var})";
379
    }
380
381
    /*
382
    *  @public function getPhpCodeRawurlencode
383
    *  @param $var
384
    *  @return string
385
    */
386
    public function getPhpCodeRawurlencode($var)
387
    {
388
        return "rawurlencode({$var})";
389
    }
390
391
    /*
392
    *  @public function getPhpCodePregReplace
393
    *  @param $return
394
    *  @param $exp
395
    *  @param $str
396
    *  @param $val
397
    *  @param $isParam
398
    *
399
    *  @return string
400
    */
401
    public function getPhpCodePregReplace($return, $exp, $str, $val, $isParam = false)
402
    {
403
        if ($isParam === false) {
404
            $ret = "\${$return} = preg_replace( '{$exp}' , '{$str}' , {$val});\n";
405
        } else {
406
            $ret = "preg_replace( '{$exp}' , '{$str}' , {$val})";
407
        }
408
409
        return $ret;
410
    }
411
412
    /*
413
    *  @public function getPhpCodeStrReplace
414
    *  @param $left
415
    *  @param $var
416
    *  @param $str
417
    *  @param $value
418
    *  @param $isParam
419
    *
420
    *  @return string
421
    */
422
    public function getPhpCodeStrReplace($left, $var, $str, $value, $isParam = false)
423
    {
424
        if ($isParam === false) {
425
            $ret = "\${$left} = str_replace( '{$var}' , '{$str}' , {$value});\n";
426
        } else {
427
            $ret = "str_replace( '{$var}' , '{$str}' , {$value})";
428
        }
429
430
        return $ret;
431
    }
432
433
    /*
434
    *  @public function getPhpCodeStripTags
435
    *  @param $left
436
    *  @param $value
437
    *  @param $isParam
438
    *
439
    *  @return string
440
    */
441
    public function getPhpCodeStripTags($left, $value, $isParam = false)
442
    {
443
        if ($isParam === false) {
444
            $ret = "\${$left} = strip_tags({$value});\n";
445
        } else {
446
            $ret = "strip_tags({$value})";
447
        }
448
449
        return $ret;
450
    }
451
452
    /*
453
    *  @public function getPhpCodeHtmlentities
454
    *  @param $entitiesVar
455
    *  @param $entitiesQuote
456
    *  @return string
457
    */
458
    public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false)
459
    {
460
        $entitiesVar = ($entitiesQuote !== false) ? $entitiesVar.', '.$entitiesQuote : $entitiesVar;
461
        $entities = "htmlentities({$entitiesVar})";
462
463
        return $entities;
464
    }
465
466
    /*
467
    *  @public function getPhpCodeHtmlspecialchars
468
    *  @param $specialVar
469
    *  @param $specialQuote
470
    *  @return string
471
    */
472
    public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false)
473
    {
474
        $specialVar = ($specialQuote !== false) ? $specialVar.', '.$specialQuote : $specialVar;
475
        $specialchars = "htmlspecialchars({$specialVar})";
476
477
        return $specialchars;
478
    }
479
}
480