Completed
Pull Request — master (#65)
by Gino
04:16
created

TDMCreatePhpCode::getPhpCodeStrType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 2
eloc 7
nc 2
nop 6
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 $var
110
	*  @param $type
111
    *
112
    *  @return string
113
    */
114
    public function getPhpCodeGlobalsVariables($var = '', $type = 'REQUEST')
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 getPhpCodeTernaryOperator
196
    *  @param $return
197
    *  @param $condition
198
    *  @param $one
199
    *  @param $two
200
    *
201
    *  @return string
202
    */
203
    public function getPhpCodeTernaryOperator($return, $condition, $one, $two)
204
    {
205
        return "{$return} = {$condition} ? {$one} : {$two};";
206
    }
207
	
208
	/*
209
    *  @public function getPhpCodeFunction
210
    *  @param $name
211
    *  @param $params
212
    *  @param $content
213
    *  @param $type
214
    *  @param $t - Indentation 
215
    *
216
    *  @return string
217
    */
218
    public function getPhpCodeFunction($name = '', $params = '', $content = '', $type = 'public ', $t = "\t")
219
    {
220
        $ret = <<<EOF
221
{$t}{$type}function {$name}({$params})
222
{$t}{
223
{$t}\t{$content}
224
{$t}}\n
225
EOF;
226
227
        return $ret;
228
    }
229
230
    /*
231
     * @public function getPhpCodeConditions
232
     * @param string $condition
233
     * @param string $operator
234
     * @param string $type
235
     * @param string $contentIf
236
     * @param mixed  $contentElse
237
     * @param $t - Indentation 
238
     *
239
     * @return string
240
     */
241
    public function getPhpCodeConditions($condition = '', $operator = '', $type = '', $contentIf = '', $contentElse = false, $t = '')
242
    {
243
        if (false === $contentElse) {
244
            $ret = <<<EOT
245
{$t}if ({$condition}{$operator}{$type}) {
246
{$t}\t{$contentIf}
247
{$t}}\n
248
EOT;
249
        } else {
250
            $ret = <<<EOT
251
{$t}if ({$condition}{$operator}{$type}) {
252
{$t}\t{$contentIf}
253
{$t}} else {
254
{$t}\t{$contentElse}
255
{$t}}\n
256
EOT;
257
        }
258
259
        return $ret;
260
    }
261
262
    /*
263
     * @public function getPhpCodeForeach
264
     * @param string $array
265
     * @param string $arrayKey     
266
     * @param string $key
267
     * @param string $value     
268
     * @param string $content
269
     *
270
     * @return string
271
     */
272
    public function getPhpCodeForeach($array = '', $arrayKey = false, $key = false, $value = false, $content = '', $t = '')
273
    {
274
        $vars = '';
275
        if ((false === $arrayKey) && (false === $key)) {
276
            $vars = "\${$array} as \${$value}";
277
        } elseif ((false === $arrayKey) && (false !== $key)) {
278
            $vars = "\${$array} as \${$key} => \${$value}";
279
        } elseif ((false !== $arrayKey) && (false === $key)) {
280
            $vars = "array_keys(\${$array}) as \${$value}";
281
        }
282
283
        $ret = <<<EOT
284
{$t}foreach({$vars}) {
285
{$t}\t{$content}
286
{$t}}\n
287
EOT;
288
289
        return $ret;
290
    }
291
292
    /*
293
     * @public function getPhpCodeFor
294
     * @param $var
295
     * @param $content
296
     * @param $value
297
     * @param $initVal
298
     * @param $operator
299
     *
300
     * @return string
301
     */
302
    public function getPhpCodeFor($var = '', $content = '', $value = '', $initVal = '', $operator = '', $t = '')
303
    {
304
        $ret = <<<EOT
305
{$t}for(\${$var} = {$initVal}; \${$var} {$operator} \${$value}; \${$var}++) {
306
{$t}\t{$content}
307
{$t}}\n
308
EOT;
309
310
        return $ret;
311
    }
312
313
    /*
314
     * @public function getPhpCodeWhile
315
     * @param $var
316
     * @param $content
317
     * @param $value
318
     * @param $operator
319
     *  @param $t
320
     *
321
     * @return string
322
     */
323
    public function getPhpCodeWhile($var = '', $content = '', $value = '', $operator = '', $t = '')
324
    {
325
        $ret = <<<EOT
326
{$t}while(\${$var} {$operator} {$value}) {
327
{$t}\t{$content}
328
{$t}}\n
329
EOT;
330
331
        return $ret;
332
    }
333
334
    /**
335
     *  @public function getPhpCodeSwitch
336
     *
337
     *  @param $op
338
     *  @param $content
339
     *  @param $t
340
     *
341
     *  @return string
342
     */
343
    public function getPhpCodeSwitch($op = '', $content = '', $t = '')
344
    {
345
        $ret = <<<EOT
346
// Switch options
347
{$t}switch (\${$op}){
348
{$t}\t{$content}
349
{$t}}\n
350
EOT;
351
352
        return $ret;
353
    }
354
355
    /**
356
     *  @public function getPhpCodeCaseSwitch     
357
     *
358
     *  @param $cases
359
     *  @param $defaultAfterCase
360
     *  @param $default
361
     *  @param $t
362
     *
363
     *  @return string
364
     */
365
    public function getPhpCodeCaseSwitch($cases = array(), $defaultAfterCase = false, $default = false, $t = "\t")
366
    {
367
        $ret = '';
368
        $def = "{$t}default:\n";
369
        foreach ($cases as $case => $value) {
370
            $case = is_string($case) ? "'{$case}'" : $case;
371
            if (!empty($case)) {
372
                $ret .= "{$t}case {$case}:\n";
373
                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...
374
                    $ret .= $def;
375
                }
376
                if (is_array($value)) {
377
                    foreach ($value as $content) {
378
                        $ret .= "{$t}{$t}{$content}\n";
379
                    }
380
                }
381
                $ret .= "{$t}break;\n";
382
            }
383
            $defaultAfterCase = false;
384
        }
385
        if ($default !== false) {
386
            $ret .= $def;
387
            $ret .= "{$t}{$default}\n";
388
            $ret .= "{$t}break;\n";
389
        }
390
391
        return $ret;
392
    }
393
394
    /*
395
    *  @public function getPhpCodeIsset
396
    *  @param $var
397
    *  @return string
398
    */
399
    public function getPhpCodeIsset($var)
400
    {
401
        return "isset(\${$var})";
402
    }
403
404
    /*
405
    *  @public function getPhpCodeUnset
406
    *  @param $var
407
    *  @return string
408
    */
409
    public function getPhpCodeUnset($var = '')
410
    {
411
        return "unset(\${$var});\n";
412
    }
413
414
    /*
415
    *  @public function getPhpCodeImplode
416
    *  @param $left
417
    *  @param $right
418
    *  @return string
419
    */
420
    public function getPhpCodeImplode($left, $right)
421
    {
422
        return "implode('{$left}', {$right})";
423
    }
424
425
    /*
426
    *  @public function getPhpCodeExplode
427
    *  @param $left
428
    *  @param $right
429
    *  @return string
430
    */
431
    public function getPhpCodeExplode($left, $right)
432
    {
433
        return "explode('{$left}', {$right})";
434
    }
435
436
    /*
437
    *  @public function getPhpCodeArray
438
    *  @param $return
439
    *  @param $left
440
    *  @param $right
441
    *  @param $isParam
442
    *
443
    *  @return string
444
    */
445
    public function getPhpCodeArray($return, $left, $right = '', $isParam = false)
446
    {
447
        $array = ($right !== '') ? "{$left}, {$right}" : (($right !== '') && is_string($left) ? "{$left} => {$right}" : "{$left}");
448
        if ($isParam === false) {
449
            $ret = "\${$return} = array({$array});\n";
450
        } else {
451
            $ret = "array({$array})";
452
        }
453
454
        return $ret;
455
    }
456
457
    /*
458
    *  @public function getPhpCodeArrayMerge
459
    *  @param $return
460
    *  @param $left
461
    *  @param $right
462
    *  @param $isParam
463
    *
464
    *  @return string
465
    */
466
    public function getPhpCodeArrayMerge($return, $left, $right, $isParam = false)
467
    {
468
        if ($isParam === false) {
469
            $ret = "\${$return} = array_merge({$left}, {$right});\n";
470
        } else {
471
            $ret = "array_merge({$left}, {$right})";
472
        }
473
474
        return $ret;
475
    }
476
477
    /*
478
    *  @public function getPhpCodeSprintf
479
    *  @param $left
480
    *  @param $right
481
    *  @return string
482
    */
483
    public function getPhpCodeSprintf($left, $right)
484
    {
485
        return "sprintf({$left}, {$right})";
486
    }
487
488
    /*
489
    *  @public function getPhpCodeEmpty
490
    *  @param $var
491
    *  @return string
492
    */
493
    public function getPhpCodeEmpty($var)
494
    {
495
        return "empty({$var})";
496
    }
497
498
    /*
499
    *  @public function getPhpCodeHeader
500
    *  @param $var
501
    *  @return string
502
    */
503
    public function getPhpCodeHeader($var)
504
    {
505
        return "header({$var})";
506
    }
507
508
    /*
509
    *  @public function getPhpCodeRawurlencode
510
    *  @param $var
511
    *  @return string
512
    */
513
    public function getPhpCodeRawurlencode($var)
514
    {
515
        return "rawurlencode({$var})";
516
    }
517
518
    /*
519
    *  @public function getPhpCodePregFunzions
520
    *  @param $return
521
    *  @param $exp
522
    *  @param $str
523
    *  @param $val
524
	*  @param $type
525
    *  @param $isParam
526
    *
527
    *  @return string
528
    */
529
    public function getPhpCodePregFunzions($return, $exp = '', $str, $val, $type = 'match', $isParam = false)
530
    {
531
        $pregFunz = "preg_{$type}( '";
532
		if ($isParam === false) {
533
            $ret = "\${$return} = {$pregFunz}{$exp}', '{$str}', {$val});\n";
534
        } else {
535
            $ret = "{$pregFunz}{$exp}', '{$str}', {$val})";
536
        }
537
538
        return $ret;
539
    }
540
    
541
    /*
542
    *  @public function getPhpCodeStrType
543
    *  @param $left
544
    *  @param $var
545
    *  @param $str
546
    *  @param $value
547
	*  @param $type
548
    *  @param $isParam
549
    *
550
    *  @return string
551
    */
552
    public function getPhpCodeStrType($left, $var, $str, $value, $type = 'replace', $isParam = false)
553
    {
554
        $strType = "str_{$type}( '";
555
		if ($isParam === false) {
556
            $ret = "\${$left} = {$strType}{$var}', '{$str}', {$value});\n";
557
        } else {
558
            $ret = "{$strType}{$var}', '{$str}', {$value})";
559
        }
560
561
        return $ret;
562
    }
563
564
    /*
565
    *  @public function getPhpCodeStripTags
566
    *  @param $left
567
    *  @param $value
568
    *  @param $isParam
569
    *
570
    *  @return string
571
    */
572
    public function getPhpCodeStripTags($left = '', $value, $isParam = false)
573
    {
574
        if ($isParam === false) {
575
            $ret = "\${$left} = strip_tags({$value});\n";
576
        } else {
577
            $ret = "strip_tags({$value})";
578
        }
579
580
        return $ret;
581
    }
582
583
    /*
584
    *  @public function getPhpCodeHtmlentities
585
    *  @param $entitiesVar
586
    *  @param $entitiesQuote
587
    *  @return string
588
    */
589
    public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false)
590
    {
591
        $entitiesVar = ($entitiesQuote !== false) ? $entitiesVar.', '.$entitiesQuote : $entitiesVar;
592
        $entities = "htmlentities({$entitiesVar})";
593
594
        return $entities;
595
    }
596
597
    /*
598
    *  @public function getPhpCodeHtmlspecialchars
599
    *  @param $specialVar
600
    *  @param $specialQuote
601
    *  @return string
602
    */
603
    public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false)
604
    {
605
        $specialVar = ($specialQuote !== false) ? $specialVar.', '.$specialQuote : $specialVar;
606
        $specialchars = "htmlspecialchars({$specialVar})";
607
608
        return $specialchars;
609
    }
610
}
611