Completed
Pull Request — master (#51)
by Gino
06:03
created

TDMCreatePhpCode::getPhpCodeSwitch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 1
eloc 8
nc 1
nop 3
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
    * @var string
32
    */
33
    protected $phpcode;
34
35
    /*
36
    *  @public function constructor
37
    *  @param null
38
    */
39
    /**
40
     *
41
     */
42
    public function __construct()
43
    {
44
    }
45
46
    /*
47
    *  @static function &getInstance
48
    *  @param null
49
    */
50
    /**
51
     * @return TDMCreatePhpCode
52
     */
53
    public static function &getInstance()
54
    {
55
        static $instance = false;
56
        if (!$instance) {
57
            $instance = new self();
58
        }
59
60
        return $instance;
61
    }
62
63
    /*
64
    *  @public function getPhpCodeCommentLine
65
    *  @param $comment
66
    *  @param $var
67
    *  @return string
68
    */
69
    public function getPhpCodeCommentLine($comment = '', $var = '')
70
    {
71
        return "// {$comment} {$var}\n";
72
    }
73
74
    /*
75
    *  @public function getPhpCodeGlobalsVariables
76
    *  @param $type
77
    *  @param $var
78
    *
79
    *  @return string
80
    */
81
    public function getPhpCodeGlobalsVariables($type = 'REQUEST', $var = '')
82
    {
83
        $type = strtoupper($type);
84
        switch ($type) {
85
            case 'GET':
86
                $ret = "\$_GET['{$var}']";
87
                break;
88
            case 'POST':
89
                $ret = "\$_POST['{$var}']";
90
                break;
91
            case 'FILES':
92
                $ret = "\$_FILES['{$var}']";
93
                break;
94
            case 'COOKIE':
95
                $ret = "\$_COOKIE['{$var}']";
96
                break;
97
            case 'ENV':
98
                $ret = "\$_ENV['{$var}']";
99
                break;
100
            case 'SERVER':
101
                $ret = "\$_SERVER['{$var}']";
102
                break;
103
            default:
104
                $ret = "\$_REQUEST['{$var}']";
105
                break;
106
        }
107
108
        return $ret;
109
    }
110
111
    /*
112
     * @public function getPhpCodeRemoveCarriageReturn
113
     * @param $string     
114
     *
115
     * @return string
116
     */
117
    public function getPhpCodeRemoveCarriageReturn($string)
118
    {
119
        return str_replace(array("\n", "\r"), '', $string);
120
    }
121
122
    /*
123
    *  @public function getPhpCodeFileExists
124
    *  @param $filename
125
    *
126
    *  @return string
127
    */
128
    public function getPhpCodeFileExists($filename)
129
    {
130
        return "file_exists({$filename})";
131
    }
132
133
    /*
134
    *  @public function getPhpCodeIncludeDir
135
    *  @param $directory
136
    *  @param $filename
137
    *  @param $once
138
    *  @param $isPath
139
    *
140
    *  @return string
141
    */
142
    public function getPhpCodeIncludeDir($directory = '', $filename = '', $once = false, $isPath = false)
143
    {
144
        if ($once === false) {
145
            if ($isPath === false) {
146
                $ret = "include {$directory} .'/{$filename}.php';\n";
147
            } else {
148
                $ret = "include {$directory};\n";
149
            }
150
        } else {
151
            if ($isPath === false) {
152
                $ret = "include_once {$directory} .'/{$filename}.php';\n";
153
            } else {
154
                $ret = "include_once {$directory};\n";
155
            }
156
        }
157
158
        return $ret;
159
    }
160
161
    /*
162
     * @public function getPhpCodeConditions
163
     * @param string $condition
164
     * @param string $operator
165
     * @param string $type
166
     * @param string $contentIf
167
     * @param mixed  $contentElse
168
     *
169
     * @return string
170
     */
171
    public function getPhpCodeConditions($condition = '', $operator = '', $type = '', $contentIf = '', $contentElse = false, $t = '')
172
    {
173
        if (false === $contentElse) {
174
            $ret = <<<EOT
175
{$t}if ({$condition}{$operator}{$type}) {
176
{$t}\t{$contentIf}
177
{$t}}\n
178
EOT;
179
        } else {
180
            $ret = <<<EOT
181
{$t}if ({$condition}{$operator}{$type}) {
182
{$t}\t{$contentIf}
183
{$t}} else {
184
{$t}\t{$contentElse}
185
{$t}}\n
186
EOT;
187
        }
188
189
        return $ret;
190
    }
191
192
    /*
193
     * @public function getPhpCodeForeach
194
     * @param string $array
195
     * @param string $arrayKey     
196
     * @param string $key
197
     * @param string $value     
198
     * @param string $content
199
     *
200
     * @return string
201
     */
202
    public function getPhpCodeForeach($array = '', $arrayKey = false, $key = false, $value = false, $content = '', $t = '')
203
    {
204
        $vars = '';
205
        if ((false === $arrayKey) && (false === $key)) {
206
            $vars = "\${$array} as \${$value}";
207
        } elseif ((false === $arrayKey) && (false !== $key)) {
208
            $vars = "\${$array} as \${$key} => \${$value}";
209
        } elseif ((false !== $arrayKey) && (false === $key)) {
210
            $vars = "array_keys(\${$array}) as \${$value}";
211
        }
212
213
        $ret = <<<EOT
214
{$t}foreach({$vars}) {
215
{$t}\t{$content}
216
{$t}}\n
217
EOT;
218
219
        return $ret;
220
    }
221
222
    /*
223
     * @public function getPhpCodeFor
224
     * @param $var
225
     * @param $content
226
     * @param $value
227
     * @param $initVal
228
     * @param $operator
229
     *
230
     * @return string
231
     */
232
    public function getPhpCodeFor($var = '', $content = '', $value = '', $initVal = '', $operator = '', $t = '')
233
    {
234
        $ret = <<<EOT
235
{$t}for(\${$var} = {$initVal}; \${$var} {$operator} \${$value}; \${$var}++) {
236
{$t}\t{$content}
237
{$t}}\n
238
EOT;
239
240
        return $ret;
241
    }
242
243
    /*
244
     * @public function getPhpCodeWhile
245
     * @param $var
246
     * @param $content
247
     * @param $value
248
     * @param $operator
249
     *  @param $t
250
     *
251
     * @return string
252
     */
253
    public function getPhpCodeWhile($var = '', $content = '', $value = '', $operator = '', $t = '')
254
    {
255
        $ret = <<<EOT
256
{$t}while(\${$var} {$operator} {$value}) {
257
{$t}\t{$content}
258
{$t}}\n
259
EOT;
260
261
        return $ret;
262
    }
263
264
    /**
265
     *  @public function getPhpCodeSwitch
266
     *
267
     *  @param $op
268
     *  @param $content
269
     *  @param $t
270
     *
271
     *  @return string
272
     */
273
    public function getPhpCodeSwitch($op = '', $content = '', $t = '')
274
    {
275
        $ret = <<<EOT
276
// Switch options
277
{$t}switch (\${$op}){
278
{$t}\t{$content}
279
{$t}}\n
280
EOT;
281
282
        return $ret;
283
    }
284
285
    /**
286
     *  @public function getPhpCodeCaseSwitch     
287
     *
288
     *  @param $cases
289
     *  @param $defaultAfterCase
290
     *  @param $default
291
     *  @param $t
292
     *
293
     *  @return string
294
     */
295
    public function getPhpCodeCaseSwitch($cases = array(), $defaultAfterCase = false, $default = false, $t = "\t")
296
    {
297
        $ret = '';
298
        $def = "{$t}default:\n";
299
        foreach ($cases as $case => $value) {
300
            $case = is_string($case) ? "'{$case}'" : $case;
301
            if (!empty($case)) {
302
                $ret .= "{$t}case {$case}:\n";
303
                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...
304
                    $ret .= $def;
305
                }
306
                if (is_array($value)) {
307
                    foreach ($value as $content) {
308
                        $ret .= "{$t}{$t}{$content}\n";
309
                    }
310
                }
311
                $ret .= "{$t}break;\n";
312
            }
313
            $defaultAfterCase = false;
314
        }
315
        if ($default !== false) {
316
            $ret .= $def;
317
            $ret .= "{$t}{$default}\n";
318
            $ret .= "{$t}break;\n";
319
        }
320
321
        return $ret;
322
    }
323
324
    /*
325
    *  @public function getPhpCodeIsset
326
    *  @param $var
327
    *  @return string
328
    */
329
    public function getPhpCodeIsset($var)
330
    {
331
        return "isset(\${$var})";
332
    }
333
334
    /*
335
    *  @public function getPhpCodeUnset
336
    *  @param $var
337
    *  @return string
338
    */
339
    public function getPhpCodeUnset($var = '')
340
    {
341
        return "unset(\${$var});\n";
342
    }
343
344
    /*
345
    *  @public function getPhpCodeImplode
346
    *  @param $left
347
    *  @param $right
348
    *  @return string
349
    */
350
    public function getPhpCodeImplode($left, $right)
351
    {
352
        return "implode('{$left}', {$right})";
353
    }
354
355
    /*
356
    *  @public function getPhpCodeExplode
357
    *  @param $left
358
    *  @param $right
359
    *  @return string
360
    */
361
    public function getPhpCodeExplode($left, $right)
362
    {
363
        return "explode('{$left}', {$right})";
364
    }
365
366
    /*
367
    *  @public function getPhpCodeArray
368
    *  @param $return
369
    *  @param $left
370
    *  @param $right
371
    *  @param $isParam
372
    *
373
    *  @return string
374
    */
375
    public function getPhpCodeArray($return, $left, $right = '', $isParam = false)
376
    {
377
        $array = ($right !== '') ? "{$left}, {$right}" : (($right !== '') && is_string($left) ? "{$left} => {$right}" : "{$left}");
378
        if ($isParam === false) {
379
            $ret = "\${$return} = array({$array});\n";
380
        } else {
381
            $ret = "array({$array})";
382
        }
383
384
        return $ret;
385
    }
386
387
    /*
388
    *  @public function getPhpCodeArrayMerge
389
    *  @param $return
390
    *  @param $left
391
    *  @param $right
392
    *  @param $isParam
393
    *
394
    *  @return string
395
    */
396 View Code Duplication
    public function getPhpCodeArrayMerge($return, $left, $right, $isParam = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
397
    {
398
        if ($isParam === false) {
399
            $ret = "\${$return} = array_merge({$left}, {$right});\n";
400
        } else {
401
            $ret = "array_merge({$left}, {$right})";
402
        }
403
404
        return $ret;
405
    }
406
407
    /*
408
    *  @public function getPhpCodeSprintf
409
    *  @param $left
410
    *  @param $right
411
    *  @return string
412
    */
413
    public function getPhpCodeSprintf($left, $right)
414
    {
415
        return "sprintf({$left}, {$right})";
416
    }
417
418
    /*
419
    *  @public function getPhpCodeEmpty
420
    *  @param $var
421
    *  @return string
422
    */
423
    public function getPhpCodeEmpty($var)
424
    {
425
        return "empty({$var})";
426
    }
427
428
    /*
429
    *  @public function getPhpCodeHeader
430
    *  @param $var
431
    *  @return string
432
    */
433
    public function getPhpCodeHeader($var)
434
    {
435
        return "header({$var})";
436
    }
437
438
    /*
439
    *  @public function getPhpCodeRawurlencode
440
    *  @param $var
441
    *  @return string
442
    */
443
    public function getPhpCodeRawurlencode($var)
444
    {
445
        return "rawurlencode({$var})";
446
    }
447
448
    /*
449
    *  @public function getPhpCodePregReplace
450
    *  @param $return
451
    *  @param $exp
452
    *  @param $str
453
    *  @param $val
454
    *  @param $isParam
455
    *
456
    *  @return string
457
    */
458 View Code Duplication
    public function getPhpCodePregReplace($return, $exp, $str, $val, $isParam = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
459
    {
460
        if ($isParam === false) {
461
            $ret = "\${$return} = preg_replace( '{$exp}', '{$str}', {$val});\n";
462
        } else {
463
            $ret = "preg_replace( '{$exp}', '{$str}', {$val})";
464
        }
465
466
        return $ret;
467
    }
468
469
    /*
470
    *  @public function getPhpCodePregMatch
471
    *  @param $return
472
    *  @param $exp
473
    *  @param $str
474
    *  @param $val
475
    *  @param $isParam
476
    *
477
    *  @return string
478
    */
479 View Code Duplication
    public function getPhpCodePregMatch($return, $exp, $str, $val, $isParam = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
480
    {
481
        if ($isParam === false) {
482
            $ret = "\${$return} = preg_match( '{$exp}', '{$str}', {$val});\n";
483
        } else {
484
            $ret = "preg_match( '{$exp}', '{$str}', {$val})";
485
        }
486
487
        return $ret;
488
    }
489
490
    /*
491
    *  @public function getPhpCodeStrReplace
492
    *  @param $left
493
    *  @param $var
494
    *  @param $str
495
    *  @param $value
496
    *  @param $isParam
497
    *
498
    *  @return string
499
    */
500
    public function getPhpCodeStrReplace($left, $var, $str, $value, $isParam = false)
501
    {
502
        if ($isParam === false) {
503
            $ret = "\${$left} = str_replace( '{$var}', '{$str}', {$value});\n";
504
        } else {
505
            $ret = "str_replace( '{$var}', '{$str}', {$value})";
506
        }
507
508
        return $ret;
509
    }
510
511
    /*
512
    *  @public function getPhpCodeStripTags
513
    *  @param $left
514
    *  @param $value
515
    *  @param $isParam
516
    *
517
    *  @return string
518
    */
519
    public function getPhpCodeStripTags($left, $value, $isParam = false)
520
    {
521
        if ($isParam === false) {
522
            $ret = "\${$left} = strip_tags({$value});\n";
523
        } else {
524
            $ret = "strip_tags({$value})";
525
        }
526
527
        return $ret;
528
    }
529
530
    /*
531
    *  @public function getPhpCodeHtmlentities
532
    *  @param $entitiesVar
533
    *  @param $entitiesQuote
534
    *  @return string
535
    */
536
    public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false)
537
    {
538
        $entitiesVar = ($entitiesQuote !== false) ? $entitiesVar.', '.$entitiesQuote : $entitiesVar;
539
        $entities = "htmlentities({$entitiesVar})";
540
541
        return $entities;
542
    }
543
544
    /*
545
    *  @public function getPhpCodeHtmlspecialchars
546
    *  @param $specialVar
547
    *  @param $specialQuote
548
    *  @return string
549
    */
550
    public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false)
551
    {
552
        $specialVar = ($specialQuote !== false) ? $specialVar.', '.$specialQuote : $specialVar;
553
        $specialchars = "htmlspecialchars({$specialVar})";
554
555
        return $specialchars;
556
    }
557
}
558