Passed
Branch timgno (61fada)
by Gino
05:14
created

TDMCreatePhpCode::getPhpCodeCommentMultiLine()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 3
eloc 7
nc 4
nop 1
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
        $value = !empty($var) ? ' '.$var : '';
72
        $ret = "// {$comment}{$value}\n";
73
74
        return $ret;
75
    }
76
77
    /*
78
     * @public function getPhpCodeCommentMultiLine
79
     * @param $multiLine     
80
     *
81
     * @return string
82
     */
83
    public function getPhpCodeCommentMultiLine($multiLine = array())
84
    {
85
        $values = !empty($multiLine) ? $multiLine : array();
86
        $ret = '/**\n';
87
        foreach ($values as $string => $value) {
88
            $ret .= " * {$string}{$value}\n";
89
        }
90
        $ret .= " */\n";
91
92
        return $ret;
93
    }
94
95
    /*
96
    *  @public function getPhpCodeDefine
97
    *  @param $left
98
    *  @param $right
99
    *
100
    *  @return string
101
    */
102
    public function getPhpCodeDefine($left, $right)
103
    {
104
        return "define('{$left}', {$right});\n";
105
    }
106
107
    /*
108
    *  @public function getPhpCodeGlobalsVariables
109
    *  @param $type
110
    *  @param $var
111
    *
112
    *  @return string
113
    */
114
    public function getPhpCodeGlobalsVariables($type = 'REQUEST', $var = '')
115
    {
116
        $type = strtoupper($type);
117
        switch ($type) {
118
            case 'GET':
119
                $ret = "\$_GET['{$var}']";
120
                break;
121
            case 'POST':
122
                $ret = "\$_POST['{$var}']";
123
                break;
124
            case 'FILES':
125
                $ret = "\$_FILES['{$var}']";
126
                break;
127
            case 'COOKIE':
128
                $ret = "\$_COOKIE['{$var}']";
129
                break;
130
            case 'ENV':
131
                $ret = "\$_ENV['{$var}']";
132
                break;
133
            case 'SERVER':
134
                $ret = "\$_SERVER['{$var}']";
135
                break;
136
            default:
137
                $ret = "\$_REQUEST['{$var}']";
138
                break;
139
        }
140
141
        return $ret;
142
    }
143
144
    /*
145
     * @public function getPhpCodeRemoveCarriageReturn
146
     * @param $string     
147
     *
148
     * @return string
149
     */
150
    public function getPhpCodeRemoveCarriageReturn($string)
151
    {
152
        return str_replace(array("\n", "\r"), '', $string);
153
    }
154
155
    /*
156
    *  @public function getPhpCodeFileExists
157
    *  @param $filename
158
    *
159
    *  @return string
160
    */
161
    public function getPhpCodeFileExists($filename)
162
    {
163
        return "file_exists({$filename})";
164
    }
165
166
    /*
167
    *  @public function getPhpCodeIncludeDir
168
    *  @param $directory
169
    *  @param $filename
170
    *  @param $once
171
    *  @param $isPath
172
    *
173
    *  @return string
174
    */
175
    public function getPhpCodeIncludeDir($directory = '', $filename = '', $once = false, $isPath = false, $type = 'include')
176
    {
177
        if ($once == 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...
178 View Code Duplication
            if ($isPath === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
179
                $ret = "{$type} {$directory} .'/{$filename}.php';\n";
180
            } else {
181
                $ret = "{$type} {$directory};\n";
182
            }
183 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
184
            if ($isPath === false) {
185
                $ret = "{$type}_once {$directory} .'/{$filename}.php';\n";
186
            } else {
187
                $ret = "{$type}_once {$directory};\n";
188
            }
189
        }
190
191
        return $ret;
192
    }
193
194
    /*
195
    *  @public function getPhpCodeFunction
196
    *  @param $name
197
    *  @param $param
198
    *  @param $content
199
    *  @param $type
200
    *  @param $t
201
    *
202
    *  @return string
203
    */
204
    public function getPhpCodeFunction($name = '', $param = '', $content = '', $type = 'public ', $t = "\t")
205
    {
206
        $ptype = $type !== '' ? $type : '';
207
        $ret = <<<EOF
208
{$t}{$ptype}function {$name}({$param})
209
{$t}{
210
{$t}\t{$content}
211
{$t}}\n
212
EOF;
213
214
        return $ret;
215
    }
216
217
    /*
218
     * @public function getPhpCodeConditions
219
     * @param string $condition
220
     * @param string $operator
221
     * @param string $type
222
     * @param string $contentIf
223
     * @param mixed  $contentElse
224
     *
225
     * @return string
226
     */
227
    public function getPhpCodeConditions($condition = '', $operator = '', $type = '', $contentIf = '', $contentElse = false, $t = '')
228
    {
229
        if (false === $contentElse) {
230
            $ret = <<<EOT
231
{$t}if ({$condition}{$operator}{$type}) {
232
{$t}\t{$contentIf}
233
{$t}}\n
234
EOT;
235
        } else {
236
            $ret = <<<EOT
237
{$t}if ({$condition}{$operator}{$type}) {
238
{$t}\t{$contentIf}
239
{$t}} else {
240
{$t}\t{$contentElse}
241
{$t}}\n
242
EOT;
243
        }
244
245
        return $ret;
246
    }
247
248
    /*
249
     * @public function getPhpCodeForeach
250
     * @param string $array
251
     * @param string $arrayKey     
252
     * @param string $key
253
     * @param string $value     
254
     * @param string $content
255
     *
256
     * @return string
257
     */
258
    public function getPhpCodeForeach($array = '', $arrayKey = false, $key = false, $value = false, $content = '', $t = '')
259
    {
260
        $vars = '';
261
        if ((false === $arrayKey) && (false === $key)) {
262
            $vars = "\${$array} as \${$value}";
263
        } elseif ((false === $arrayKey) && (false !== $key)) {
264
            $vars = "\${$array} as \${$key} => \${$value}";
265
        } elseif ((false !== $arrayKey) && (false === $key)) {
266
            $vars = "array_keys(\${$array}) as \${$value}";
267
        }
268
269
        $ret = <<<EOT
270
{$t}foreach({$vars}) {
271
{$t}\t{$content}
272
{$t}}\n
273
EOT;
274
275
        return $ret;
276
    }
277
278
    /*
279
     * @public function getPhpCodeFor
280
     * @param $var
281
     * @param $content
282
     * @param $value
283
     * @param $initVal
284
     * @param $operator
285
     *
286
     * @return string
287
     */
288
    public function getPhpCodeFor($var = '', $content = '', $value = '', $initVal = '', $operator = '', $t = '')
289
    {
290
        $ret = <<<EOT
291
{$t}for(\${$var} = {$initVal}; \${$var} {$operator} \${$value}; \${$var}++) {
292
{$t}\t{$content}
293
{$t}}\n
294
EOT;
295
296
        return $ret;
297
    }
298
299
    /*
300
     * @public function getPhpCodeWhile
301
     * @param $var
302
     * @param $content
303
     * @param $value
304
     * @param $operator
305
     *  @param $t
306
     *
307
     * @return string
308
     */
309
    public function getPhpCodeWhile($var = '', $content = '', $value = '', $operator = '', $t = '')
310
    {
311
        $ret = <<<EOT
312
{$t}while(\${$var} {$operator} {$value}) {
313
{$t}\t{$content}
314
{$t}}\n
315
EOT;
316
317
        return $ret;
318
    }
319
320
    /**
321
     *  @public function getPhpCodeSwitch
322
     *
323
     *  @param $op
324
     *  @param $content
325
     *  @param $t
326
     *
327
     *  @return string
328
     */
329
    public function getPhpCodeSwitch($op = '', $content = '', $t = '')
330
    {
331
        $ret = <<<EOT
332
// Switch options
333
{$t}switch (\${$op}){
334
{$t}\t{$content}
335
{$t}}\n
336
EOT;
337
338
        return $ret;
339
    }
340
341
    /**
342
     *  @public function getPhpCodeCaseSwitch     
343
     *
344
     *  @param $cases
345
     *  @param $defaultAfterCase
346
     *  @param $default
347
     *  @param $t
348
     *
349
     *  @return string
350
     */
351
    public function getPhpCodeCaseSwitch($cases = array(), $defaultAfterCase = false, $default = false, $t = "\t")
352
    {
353
        $ret = '';
354
        $def = "{$t}default:\n";
355
        foreach ($cases as $case => $value) {
356
            $case = is_string($case) ? "'{$case}'" : $case;
357
            if (!empty($case)) {
358
                $ret .= "{$t}case {$case}:\n";
359
                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...
360
                    $ret .= $def;
361
                }
362
                if (is_array($value)) {
363
                    foreach ($value as $content) {
364
                        $ret .= "{$t}{$t}{$content}\n";
365
                    }
366
                }
367
                $ret .= "{$t}break;\n";
368
            }
369
            $defaultAfterCase = false;
370
        }
371
        if ($default !== false) {
372
            $ret .= $def;
373
            $ret .= "{$t}{$default}\n";
374
            $ret .= "{$t}break;\n";
375
        }
376
377
        return $ret;
378
    }
379
380
    /*
381
    *  @public function getPhpCodeIsset
382
    *  @param $var
383
    *  @return string
384
    */
385
    public function getPhpCodeIsset($var)
386
    {
387
        return "isset(\${$var})";
388
    }
389
390
    /*
391
    *  @public function getPhpCodeUnset
392
    *  @param $var
393
    *  @return string
394
    */
395
    public function getPhpCodeUnset($var = '')
396
    {
397
        return "unset(\${$var});\n";
398
    }
399
400
    /*
401
    *  @public function getPhpCodeImplode
402
    *  @param $left
403
    *  @param $right
404
    *  @return string
405
    */
406
    public function getPhpCodeImplode($left, $right)
407
    {
408
        return "implode('{$left}', {$right})";
409
    }
410
411
    /*
412
    *  @public function getPhpCodeExplode
413
    *  @param $left
414
    *  @param $right
415
    *  @return string
416
    */
417
    public function getPhpCodeExplode($left, $right)
418
    {
419
        return "explode('{$left}', {$right})";
420
    }
421
422
    /*
423
    *  @public function getPhpCodeArray
424
    *  @param $return
425
    *  @param $left
426
    *  @param $right
427
    *  @param $isParam
428
    *
429
    *  @return string
430
    */
431
    public function getPhpCodeArray($return, $left, $right = '', $isParam = false)
432
    {
433
        $array = ($right !== '') ? "{$left}, {$right}" : (($right !== '') && is_string($left) ? "{$left} => {$right}" : "{$left}");
434
        if ($isParam === false) {
435
            $ret = "\${$return} = array({$array});\n";
436
        } else {
437
            $ret = "array({$array})";
438
        }
439
440
        return $ret;
441
    }
442
443
    /*
444
    *  @public function getPhpCodeArrayMerge
445
    *  @param $return
446
    *  @param $left
447
    *  @param $right
448
    *  @param $isParam
449
    *
450
    *  @return string
451
    */
452 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...
453
    {
454
        if ($isParam === false) {
455
            $ret = "\${$return} = array_merge({$left}, {$right});\n";
456
        } else {
457
            $ret = "array_merge({$left}, {$right})";
458
        }
459
460
        return $ret;
461
    }
462
463
    /*
464
    *  @public function getPhpCodeSprintf
465
    *  @param $left
466
    *  @param $right
467
    *  @return string
468
    */
469
    public function getPhpCodeSprintf($left, $right)
470
    {
471
        return "sprintf({$left}, {$right})";
472
    }
473
474
    /*
475
    *  @public function getPhpCodeEmpty
476
    *  @param $var
477
    *  @return string
478
    */
479
    public function getPhpCodeEmpty($var)
480
    {
481
        return "empty({$var})";
482
    }
483
484
    /*
485
    *  @public function getPhpCodeHeader
486
    *  @param $var
487
    *  @return string
488
    */
489
    public function getPhpCodeHeader($var)
490
    {
491
        return "header({$var})";
492
    }
493
494
    /*
495
    *  @public function getPhpCodeRawurlencode
496
    *  @param $var
497
    *  @return string
498
    */
499
    public function getPhpCodeRawurlencode($var)
500
    {
501
        return "rawurlencode({$var})";
502
    }
503
504
    /*
505
    *  @public function getPhpCodePregReplace
506
    *  @param $return
507
    *  @param $exp
508
    *  @param $str
509
    *  @param $val
510
    *  @param $isParam
511
    *
512
    *  @return string
513
    */
514 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...
515
    {
516
        if ($isParam === false) {
517
            $ret = "\${$return} = preg_replace( '{$exp}', '{$str}', {$val});\n";
518
        } else {
519
            $ret = "preg_replace( '{$exp}', '{$str}', {$val})";
520
        }
521
522
        return $ret;
523
    }
524
525
    /*
526
    *  @public function getPhpCodePregMatch
527
    *  @param $return
528
    *  @param $exp
529
    *  @param $str
530
    *  @param $val
531
    *  @param $isParam
532
    *
533
    *  @return string
534
    */
535 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...
536
    {
537
        if ($isParam === false) {
538
            $ret = "\${$return} = preg_match( '{$exp}', '{$str}', {$val});\n";
539
        } else {
540
            $ret = "preg_match( '{$exp}', '{$str}', {$val})";
541
        }
542
543
        return $ret;
544
    }
545
546
    /*
547
    *  @public function getPhpCodeStrReplace
548
    *  @param $left
549
    *  @param $var
550
    *  @param $str
551
    *  @param $value
552
    *  @param $isParam
553
    *
554
    *  @return string
555
    */
556
    public function getPhpCodeStrReplace($left, $var, $str, $value, $isParam = false)
557
    {
558
        if ($isParam === false) {
559
            $ret = "\${$left} = str_replace( '{$var}', '{$str}', {$value});\n";
560
        } else {
561
            $ret = "str_replace( '{$var}', '{$str}', {$value})";
562
        }
563
564
        return $ret;
565
    }
566
567
    /*
568
    *  @public function getPhpCodeStripTags
569
    *  @param $left
570
    *  @param $value
571
    *  @param $isParam
572
    *
573
    *  @return string
574
    */
575
    public function getPhpCodeStripTags($left = '', $value, $isParam = false)
576
    {
577
        if ($isParam === false) {
578
            $ret = "\${$left} = strip_tags({$value});\n";
579
        } else {
580
            $ret = "strip_tags({$value})";
581
        }
582
583
        return $ret;
584
    }
585
586
    /*
587
    *  @public function getPhpCodeHtmlentities
588
    *  @param $entitiesVar
589
    *  @param $entitiesQuote
590
    *  @return string
591
    */
592
    public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false)
593
    {
594
        $entitiesVar = ($entitiesQuote !== false) ? $entitiesVar.', '.$entitiesQuote : $entitiesVar;
595
        $entities = "htmlentities({$entitiesVar})";
596
597
        return $entities;
598
    }
599
600
    /*
601
    *  @public function getPhpCodeHtmlspecialchars
602
    *  @param $specialVar
603
    *  @param $specialQuote
604
    *  @return string
605
    */
606
    public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false)
607
    {
608
        $specialVar = ($specialQuote !== false) ? $specialVar.', '.$specialQuote : $specialVar;
609
        $specialchars = "htmlspecialchars({$specialVar})";
610
611
        return $specialchars;
612
    }
613
}
614