Completed
Push — master ( b8d8a2...060750 )
by Gino
06:18 queued 02:27
created

TDMCreatePhpCode::getPhpCodeFileExists()   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 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') or die('Restricted access');
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
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 getPhpCodeVariables
71
    *  @param $type
72
    *  @param $var
73
    *  @return string
74
    */
75
    public function getPhpCodeVariables($type = 'REQUEST', $var = '')
76
    {
77
        $type = strtoupper($type);
78
        switch ($type) {
79
            case 'GET':
80
                $ret = "\$_GET['{$var}']";
81
                break;
82
            case 'POST':
83
                $ret = "\$_POST['{$var}']";
84
                break;
85
            case 'FILES':
86
                $ret = "\$_FILES['{$var}']";
87
                break;
88
            case 'COOKIE':
89
                $ret = "\$_COOKIE['{$var}']";
90
                break;
91
            case 'ENV':
92
                $ret = "\$_ENV['{$var}']";
93
                break;
94
            case 'SERVER':
95
                $ret = "\$_SERVER['{$var}']";
96
                break;
97
            default:
98
                $ret = "\$_REQUEST['{$var}']";
99
                break;
100
        }
101
102
        return $ret;
103
    }
104
105
    /*
106
    *  @public function getPhpCodeFileExists
107
    *  @param $filename
108
    *
109
    *  @return string
110
    */
111
    public function getPhpCodeFileExists($filename)
112
    {
113
        return "file_exists({$filename})";
114
    }
115
116
    /*
117
    *  @public function getPhpCodeIncludeDir
118
    *  @param $directory
119
    *  @param $filename
120
    *  @param $once
121
    *  @param $isPath
122
    *
123
    *  @return string
124
    */
125
    public function getPhpCodeIncludeDir($directory = '', $filename = '', $once = false, $isPath = false)
126
    {
127
        if ($once === false) {
128
            if ($isPath === false) {
129
                $ret = "include {$directory} .'/{$filename}.php';\n";
130
            } else {
131
                $ret = "include {$directory};\n";
132
            }
133
        } else {
134
            if ($isPath === false) {
135
                $ret = "include_once {$directory} .'/{$filename}.php';\n";
136
            } else {
137
                $ret = "include_once {$directory};\n";
138
            }
139
        }
140
141
        return $ret;
142
    }
143
144
    /*
145
     * @public function getPhpCodeConditions
146
     * @param string $condition
147
     * @param string $operator
148
     * @param string $type
149
     * @param string $contentIf
150
     * @param mixed  $contentElse
151
     *
152
     * @return string
153
     */
154
    public function getPhpCodeConditions($condition = '', $operator = '', $type = '', $contentIf = '', $contentElse = false)
155
    {
156
        if (false === $contentElse) {
157
            $ret = <<<EOT
158
if ({$condition}{$operator}{$type}) {
159
	{$contentIf}
160
}\n
161
EOT;
162
        } else {
163
            $ret = <<<EOT
164
if ({$condition}{$operator}{$type}) {
165
	{$contentIf}
166
} else {
167
	{$contentElse}
168
}\n
169
EOT;
170
        }
171
172
        return $ret;
173
    }
174
175
    /*
176
     * @public function getPhpCodeForeach
177
     * @param string $array
178
     * @param string $content
179
     * @param string $value
180
     * @param string $arrayKey
181
     * @param string $key
182
     *
183
     * @return string
184
     */
185
    public function getPhpCodeForeach($array = '', $content = '', $value = false, $arrayKey = false, $key = false)
186
    {
187
        if ((false === $arrayKey) && (false === $key)) {
188
            $vars = "{$array} as {$value}";
189
        } elseif ((false === $arrayKey) && (false !== $key)) {
190
            $vars = "{$array} as {$key} => {$value}";
191
        } elseif ((false !== $arrayKey) && (false === $key)) {
192
            $vars = "array_keys({$array}) as {$value}";
193
        }
194
195
        $ret = <<<EOT
196
foreach({$vars}) {
0 ignored issues
show
Bug introduced by
The variable $vars does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
197
	{$content}
198
}\n
199
EOT;
200
201
        return $ret;
202
    }
203
204
    /*
205
     * @public function getPhpCodeFor
206
     * @param $var
207
     * @param $content
208
     * @param $value
209
     * @param $initVal
210
     * @param $operator
211
     *
212
     * @return string
213
     */
214
    public function getPhpCodeFor($var = '', $content = '', $value = '', $initVal = '', $operator = '')
215
    {
216
        $ret = <<<EOT
217
for(\${$var} = {$initVal}; \${$var} {$operator} \${$value}; \${$var}++) {
218
	{$content}
219
}\n
220
EOT;
221
222
        return $ret;
223
    }
224
225
    /*
226
     * @public function getPhpCodeWhile
227
     * @param $var
228
     * @param $content
229
     * @param $value
230
     * @param $operator
231
     *
232
     * @return string
233
     */
234
    public function getPhpCodeWhile($var = '', $content = '', $value = '', $operator = '')
235
    {
236
        $ret = <<<EOT
237
while(\${$var} {$operator} {$value}) {
238
	{$content}
239
}\n
240
EOT;
241
242
        return $ret;
243
    }
244
245
    /**
246
     *  @public function getPhpCodeSwitch
247
     *
248
     *  @param $op
249
     *  @param $content
250
     *
251
     *  @return string
252
     */
253
    public function getPhpCodeSwitch($op = '', $content = '')
254
    {
255
        $ret = <<<EOT
256
// Switch options
257
switch (\${$op}){
258
	{$content}
259
}\n
260
EOT;
261
262
        return $ret;
263
    }
264
265
    /**
266
     *  @public function getPhpCodeCaseSwitch
267
     *
268
     *  @param $case
269
     *  @param $content
270
     *
271
     *  @return string
272
     */
273
    public function getPhpCodeCaseSwitch($case = 'list', $content, $defaultAfterCase = false, $default = false)
274
    {
275
        if (is_string($case)) {
276
            $ret = "\tcase '{$case}':\n";
277
        } else {
278
            $ret = "\tcase {$case}:\n";
279
        }
280
        if ($defaultAfterCase) {
281
            $ret .= <<<EOT
282
    default:
283
		{$content}
284
	break;\n
285
EOT;
286
        } else {
287
            $ret .= <<<EOT
288
		{$content}
289
	break;\n
290
EOT;
291
        }
292
        if ($default !== false) {
293
            $ret = <<<EOT
294
    default:
295
		{$default}
296
	break;\n
297
EOT;
298
        }
299
300
        return $ret;
301
    }
302
303
    /*
304
    *  @public function getPhpCodeIsset
305
    *  @param $var
306
    *  @return string
307
    */
308
    public function getPhpCodeIsset($var)
309
    {
310
        return "isset(\${$var})";
311
    }
312
313
    /*
314
    *  @public function getPhpCodeImplode
315
    *  @param $left
316
    *  @param $right
317
    *  @return string
318
    */
319
    public function getPhpCodeImplode($left, $right)
320
    {
321
        return "implode('{$left} ', {$right})";
322
    }
323
324
    /*
325
    *  @public function getPhpCodeSprintf
326
    *  @param $left
327
    *  @param $right
328
    *  @return string
329
    */
330
    public function getPhpCodeSprintf($left, $right)
331
    {
332
        return "sprintf({$left}, {$right})";
333
    }
334
335
    /*
336
    *  @public function getPhpCodeEmpty
337
    *  @param $var
338
    *  @return string
339
    */
340
    public function getPhpCodeEmpty($var)
341
    {
342
        return "empty({$var})";
343
    }
344
345
    /*
346
    *  @public function getPhpCodeHeader
347
    *  @param $var
348
    *  @return string
349
    */
350
    public function getPhpCodeHeader($var)
351
    {
352
        return "header({$var})";
353
    }
354
355
    /*
356
    *  @public function getPhpCodeRawurlencode
357
    *  @param $var
358
    *  @return string
359
    */
360
    public function getPhpCodeRawurlencode($var)
361
    {
362
        return "rawurlencode({$var})";
363
    }
364
365
    /*
366
    *  @public function getPhpCodeHtmlentities
367
    *  @param $entitiesVar
368
    *  @param $entitiesQuote
369
    *  @return string
370
    */
371
    public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false)
372
    {
373
        $entitiesVar = $entitiesQuote !== false ? $entitiesVar.', '.$entitiesQuote : $entitiesVar;
374
        $entities = "htmlentities({$entitiesVar})";
375
376
        return $entities;
377
    }
378
379
    /*
380
    *  @public function getPhpCodeHtmlspecialchars
381
    *  @param $specialVar
382
    *  @param $specialQuote
383
    *  @return string
384
    */
385
    public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false)
386
    {
387
        $specialVar = $specialQuote !== false ? $specialVar.', '.$specialQuote : $specialVar;
388
        $specialchars = "htmlspecialchars({$specialVar})";
389
390
        return $specialchars;
391
    }
392
}
393