Completed
Push — master ( 4e5fd8...358131 )
by Gino
08:01 queued 03:42
created

TDMCreatePhpCode::getPhpCodeImplode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

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