Completed
Push — master ( aeaa5d...9fb24b )
by Gino
23:28 queued 09:34
created

TDMCreateXoopsCode::getXoopsCodeImageListSetVar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 18
rs 9.4286
cc 1
eloc 14
nc 1
nop 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A TDMCreateXoopsCode::getXoopsCodeGroupPermForm() 0 4 1
A TDMCreateXoopsCode::getXoopsCodeAddItem() 0 4 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: TDMCreateXoopsCode.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
defined('XOOPS_ROOT_PATH') || die('Restricted access');
25
/**
26
 * Class TDMCreateXoopsCode.
27
 */
28
class TDMCreateXoopsCode
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 mixed
32
    */
33
    private $phpcode = null;
34
35
    /*
36
    *  @public function constructor
37
    *  @param null
38
    */
39
    /**
40
     *
41
     */
42
    public function __construct()
43
    {
44
        $this->phpcode = TDMCreatePhpCode::getInstance();
45
    }
46
47
    /*
48
    *  @static function &getInstance
49
    *  @param null
50
    */
51
    /**
52
     * @return TDMCreateXoopsCode
53
     */
54
    public static function &getInstance()
55
    {
56
        static $instance = false;
57
        if (!$instance) {
58
            $instance = new self();
59
        }
60
61
        return $instance;
62
    }
63
64
    /*
65
    *  @public function getXoopsCodeEqualsOperator
66
    *  @param string $left
67
    *  @param string $right
68
    *  @param boolean $ref
69
    *  @return string
70
    */
71
    public function getXoopsCodeEqualsOperator($left, $right, $ref = false)
72
    {
73
        if (false === $ref) {
74
            $ret = "{$left} = {$right};\n";
75
        } else {
76
            $ret = "{$left} =& {$right};\n";
77
        }
78
79
        return $ret;
80
    }
81
	
82
	/*
83
     *  @private function getXoopsCodeSwitch
84
     *  @param $op
85
     *  @param $listCases
86
	 *  @param $defaultList
87
     *
88
     * @return string
89
     */
90
    private function getXoopsCodeSwitch($op = 'op', $listCases = array(), $defaultList = false)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
91
    {        		
92
		$switch = $this->phpcode->getPhpCodeCaseSwitch(array($listCases), $defaultList);
93
        
94
		return $this->phpcode->getPhpCodeSwitch($op, $switch);
95
    }
96
97
    /*
98
    *  @public function getXoopsCodeCPHeader
99
    *  @param null
100
    *  @return string
101
    */
102
    public function getXoopsCodeCPHeader()
103
    {
104
        return "xoops_cp_header();\n";
105
    }
106
107
    /*
108
    *  @public function getXoopsCodeCPFooter
109
    *  @param null
110
    *  @return string
111
    */
112
    public function getXoopsCodeCPFooter()
113
    {
114
        return "xoops_cp_footer();\n";
115
    }
116
117
    /**
118
     *  @public function getXoopsCodeLoad
119
     *
120
     *  @param $var
121
     *
122
     *  @return string
123
     */
124
    public function getXoopsCodeLoad($var = '')
125
    {
126
        return "xoops_load('{$var}');\n";
127
    }
128
	
129
	/**
130
     *  @public function getXoopsCodeLoadLanguage
131
     *
132
     *  @param $lang
133
     *
134
     *  @return string
135
     */
136
    public function getXoopsCodeLoadLanguage($lang)
137
    {
138
        return "xoops_loadLanguage('{$lang}');\n";
139
    }
140
141
    /*
142
    *  @public function getXoopsCodeSetVar
143
    *  @param string $tableName
144
    *  @param string $fieldName
145
    *  @param string $var
146
    *  @return string
147
    */
148
    public function getXoopsCodeSetVar($tableName, $fieldName, $var)
149
    {
150
        return "\${$tableName}Obj->setVar('{$fieldName}', {$var});\n";
151
    }
152
	
153
	/*
154
    *  @public function getXoopsCodeGetVar
155
    *  @param string $varLeft
156
	*  @param string $handle
157
	*  @param string $var
158
	*  @param string $isParam
159
	*
160
    *  @return string
161
    */
162
    public function getXoopsCodeGetVar($varLeft = '', $handle = '', $var = '', $isParam = false)
163
    {
164
        if($isParam === false) {
165
			$ret = "\${$varLeft} = \${$handle}->getVar('{$var}');\n";
166
		} else {	
167
			$ret = "\${$handle}->getVar('{$var}')";
168
		}
169
		
170
		return $ret; 
171
    }
172
	
173
	/*
174
    *  @public function getXoopsCodeGroupPermForm
175
    *  @param string $varLeft
176
	*  @param string $formTitle
177
	*  @param string $moduleId
178
	*  @param string $permName
179
	*  @param string $permDesc
180
	*  @param string $filename
181
	*
182
    *  @return string
183
    */
184
    public function getXoopsCodeGroupPermForm($varLeft = '', $formTitle = '', $moduleId = '', $permName = '', $permDesc = '', $filename = '')
185
    {        
186
		return "\${$varLeft} = new XoopsGroupPermForm({$formTitle}, {$moduleId}, {$permName}, {$permDesc}, {$filename});\n";
187
    }
188
	
189
	/*
190
    *  @public function getXoopsCodeAddItem
191
    *  @param string $varLeft
192
	*  @param string $paramLeft
193
	*  @param string $paramRight
194
	*
195
    *  @return string
196
    */
197
    public function getXoopsCodeAddItem($varLeft = '', $paramLeft = '', $paramRight = '')
198
    {		
199
		return "\${$varLeft}->addItem({$paramLeft}, {$paramRight});\n";
200
    }	
201
202
    /*
203
    *  @public function getXoopsCodeTextDateSelectSetVar
204
    *  @param string $tableName
205
    *  @param string $fieldName
206
    *  @return string
207
    */
208
    public function getXoopsCodeTextDateSelectSetVar($tableName, $fieldName)
209
    {
210
        return $this->getXoopsCodeSetVar($tableName, $fieldName, "strtotime(\$_POST['{$fieldName}'])");
211
    }
212
213
    /*
214
    *  @public function getXoopsCodeCheckBoxOrRadioYNSetVar
215
    *  @param $tableName
216
    *  @param $fieldName
217
    *  @return string
218
    */
219
    public function getXoopsCodeCheckBoxOrRadioYNSetVar($tableName, $fieldName)
220
    {
221
        $ret = $this->getXoopsCodeSetVar($tableName, $fieldName, "((1 == \$_REQUEST['{$fieldName}']) ? '1' : '0')");
222
223
        return $ret;
224
    }
225
226
    /*
227
    *  @public function getXoopsCodeGetConfig
228
    *  @param $moduleDirname
229
    *  @param $name
230
    *  @return string
231
    */
232
    public function getXoopsCodeGetConfig($moduleDirname, $name)
233
    {
234
        return "\${$moduleDirname}->getConfig('{$name}')";
235
    }
236
237
    /*
238
    *  @public function getXoopsCodeIdGetVar
239
    *  @param string $lpFieldName
240
    *  @return string
241
    */
242
    public function getXoopsCodeIdGetVar($lpFieldName)
243
    {
244
        return "\${$lpFieldName}['id'] = \$i;\n";
245
    }
246
247
    /*
248
    *  @public function getXoopsCodeGetVarAll
249
    *  @param string $lpFieldName
250
    *  @param string $rpFieldName
251
    *  @param string $tableName
252
    *  @param string $fieldName
253
    *  @return string
254
    */
255
    public function getXoopsCodeGetVarAll($lpFieldName, $rpFieldName, $tableName, $fieldName)
256
    {
257
        return "\${$lpFieldName}['{$rpFieldName}'] = \${$tableName}All[\$i]->getVar('{$fieldName}');\n";
258
    }
259
	
260
	/*
261
    *  @public function getXoopsHandlerLine
262
    *  @param $moduleDirname
263
    *  @param $tableName
264
    *  @return string
265
    */
266
    public function getXoopsHandlerLine($moduleDirname, $tableName)
267
    {
268
        return "\${$tableName}Handler =& \${$moduleDirname}->getHandler('{$tableName}');\n";
269
    }
270
	
271
	/*
272
    *  @public function getXoopsSimpleForm
273
    *  @param $left
274
	*  @param $element
275
	*  @param $elementsContent
276
	*  @param $caption
277
	*  @param $var
278
	*  @param $filename
279
	*  @param $type
280
    *  
281
    *  @return string
282
    */
283
    public function getXoopsSimpleForm($left = '', $element = '', $elementsContent = '', $caption = '', $var = '', $filename = '', $type = 'post')
284
    {
285
        $ret = "\${$left} = new XoopsSimpleForm({$caption}, '{$var}', '{$filename}.php', '{$type}');\n";
286
		if(!empty($elementsContent)) {
287
			$ret .= "{$elementsContent}";
288
		}
289
		$ret .= "\${$left}->addElement(\${$element})\n";
290
		$ret .= "\${$left}->display();\n";
291
292
		return $ret;
293
    }
294
	
295
	/*
296
    *  @public function getXoopsFormSelect
297
    *  @param $varSelect
298
	*  @param $caption
299
	*  @param $var
300
	*  @param $options
301
	*  @param $setExtra
302
    *  
303
    *  @return string
304
    */
305
	public function getXoopsFormSelect($varSelect = '', $caption = '', $var = '', $options = array(), $setExtra = true)
306
    {
307
        $ret = "\${$varSelect} = new XoopsFormSelect({$caption}, '{$var}', \${$var});\n";
308
		if(false !== $setExtra) {
309
			$ret .= "\${$varSelect}->setExtra('{$setExtra}');\n";
310
		}
311
		foreach($options as $key => $value) {
312
			$ret .= "\${$varSelect}->addOption('{$key}', {$value});\n";
313
		}
314
		
315
		return $ret;
316
    }	
317
	
318
    /*
319
    *  @public function getXoopsCodeTopicGetVar
320
    *  @param string $lpFieldName
321
    *  @param string $rpFieldName
322
    *  @param string $tableName
323
    *  @param string $tableNameTopic
324
    *  @param string $fieldNameParent
325
    *  @param string $fieldNameTopic
326
    *  @return string
327
    */
328
    public function getXoopsCodeTopicGetVar($lpFieldName, $rpFieldName, $tableName, $tableNameTopic, $fieldNameParent, $fieldNameTopic)
329
    {
330
        $ret = <<<EOT
331
\t\t\t\t// Get Var {$fieldNameParent}
332
\t\t\t\t\${$rpFieldName} =& \${$tableNameTopic}Handler->get(\${$tableName}All[\$i]->getVar('{$fieldNameParent}'));
333
\t\t\t\t\${$lpFieldName}['{$rpFieldName}'] = \${$rpFieldName}->getVar('{$fieldNameTopic}');\n
334
EOT;
335
336
        return $ret;
337
    }
338
339
    /*
340
    *  @public function getXoopsCodeParentTopicGetVar
341
    *  @param string $moduleDirname
342
    *  @param string $lpFieldName
343
    *  @param string $rpFieldName
344
    *  @param string $tableName
345
    *  @param string $tableSoleNameTopic
346
    *  @param string $tableNameTopic
347
    *  @param string $fieldNameParent
348
    *  @return string
349
    */
350 View Code Duplication
    public function getXoopsCodeParentTopicGetVar($moduleDirname, $lpFieldName, $rpFieldName, $tableName, $tableSoleNameTopic, $tableNameTopic, $fieldNameParent)
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...
351
    {
352
        $ret = <<<EOT
353
\t\t\t\tif(!isset(\${$tableNameTopic}Handler)) {
354
\t\t\t\t\t// Get {$tableNameTopic} Handler
355
\t\t\t\t\t\${$tableNameTopic}Handler =& \${$moduleDirname}->getHandler('{$tableNameTopic}');
356
\t\t\t\t}
357
\t\t\t\t// Get Var {$fieldNameParent}
358
\t\t\t\t\${$lpFieldName}['{$rpFieldName}'] = \${$tableNameTopic}Handler->get{$tableSoleNameTopic}FromId(\${$tableName}All[\$i]->getVar('{$fieldNameParent}'));\n
359
EOT;
360
361
        return $ret;
362
    }
363
364
    /*
365
    *  @public function getXoopsCodeUploadImageGetVar
366
    *  @param string $lpFieldName
367
    *  @param string $rpFieldName
368
    *  @param string $tableName
369
    *  @param string $fieldName
370
    *  @return string
371
    */
372
    public function getXoopsCodeUploadImageGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
373
    {
374
        $ret = <<<EOT
375
\t\t\t\t// Get Var {$fieldName}
376
\t\t\t\t\${$fieldName} = \${$tableName}All[\$i]->getVar('{$fieldName}');
377
\t\t\t\t\${$lpFieldName}['{$rpFieldName}'] = \${$fieldName} ? \${$fieldName} : 'blank.gif';\n
378
EOT;
379
380
        return $ret;
381
    }
382
    /*
383
    *  @public function getXoopsCodeUrlFileGetVar
384
    *  @param string $lpFieldName
385
    *  @param string $rpFieldName
386
    *  @param string $tableName
387
    *  @param string $fieldName
388
    *  @return string
389
    */
390
    public function getXoopsCodeUrlFileGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
391
    {
392
        return $this->getXoopsCodeGetVarAll($lpFieldName, $rpFieldName, $tableName, $fieldName);
393
    }
394
    /*
395
    *  @public function getXoopsCodeTextAreaGetVar
396
    *  @param string $lpFieldName
397
    *  @param string $rpFieldName
398
    *  @param string $tableName
399
    *  @param string $fieldName
400
    *  @return string
401
    */
402
    public function getXoopsCodeTextAreaGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
403
    {
404
        return "\${$lpFieldName}['{$rpFieldName}'] = strip_tags(\${$tableName}All[\$i]->getVar('{$fieldName}'));\n";
405
    }
406
407
    /*
408
    *  @public function getXoopsCodeSelectUserGetVar
409
    *  @param string $lpFieldName
410
    *  @param string $rpFieldName
411
    *  @param string $tableName
412
    *  @param string $fieldName
413
    * @return string
414
    */
415
    public function getXoopsCodeSelectUserGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
416
    {
417
        return "\${$lpFieldName}['{$rpFieldName}'] = XoopsUser::getUnameFromId(\${$tableName}All[\$i]->getVar('{$fieldName}'), 's');\n";
418
    }
419
420
    /*
421
    *  @public function getXoopsCodeTextDateSelectGetVar
422
    *  @param string $lpFieldName
423
    *  @param string $rpFieldName
424
    *  @param string $tableName
425
    *  @param string $fieldName
426
    *  @return string
427
    */
428
    public function getXoopsCodeTextDateSelectGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
429
    {
430
        return "\${$lpFieldName}['{$rpFieldName}'] = formatTimeStamp(\${$tableName}All[\$i]->getVar('{$fieldName}'), 's');\n";
431
    }
432
433
    /*
434
    *  @public function getXoopsCodeUserHeader
435
    *  @param string $moduleDirname
436
    *  @param string $tableName
437
    *  @return string
438
    */
439
    public function getXoopsCodeXoopsOptionTemplateMain($moduleDirname, $tableName)
440
    {
441
        return "\$GLOBALS['xoopsOption']['template_main'] = '{$moduleDirname}_{$tableName}.tpl';\n";
442
    }
443
444
    /*
445
    *  @public function getXoopsCodeUserHeader
446
    *  @param string $moduleDirname
447
    *  @param string $tableName
448
    *  @return string
449
    */
450
    public function getXoopsCodeUserHeader($moduleDirname, $tableName)
451
    {
452
        $ret = $this->phpcode->getPhpCodeIncludeDir('__DIR__', 'header');
453
        $ret .= $this->getXoopsCodeXoopsOptionTemplateMain($moduleDirname, $tableName);
454
        $ret .= $this->phpcode->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'header', true);
455
456
        return $ret;
457
    }
458
459
    /*
460
    *  @public function getXoopsCodePermissionsHeader
461
    *  @param null
462
    */
463
    /**
464
     * @return string
465
     */
466
    public function getXoopsCodePermissionsHeader()
467
    {
468
        $ret = $this->phpcode->getPhpCodeCommentLine('Permission');
469
        $ret .= $this->phpcode->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/xoopsform/grouppermform', true);
470
        $ret .= $this->getXoopsCodeEqualsOperator('$gperm_handler', "xoops_gethandler('groupperm')", true);
471
        $groups = $this->getXoopsCodeEqualsOperator('$groups', '$xoopsUser->getGroups()');
472
        $elseGroups = $this->getXoopsCodeEqualsOperator('$groups', 'XOOPS_GROUP_ANONYMOUS');
473
        $ret .= $this->phpcode->getPhpCodeConditions('is_object($xoopsUser)', '', $type = '', $groups, $elseGroups);
474
475
        return $ret;
476
    }
477
478
    /**
479
     *  @public function getXoopsCodeGetFieldId
480
     *
481
     *  @param $fields
482
     *
483
     *  @return string
484
     */
485 View Code Duplication
    public function getXoopsCodeGetFieldId($fields)
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...
486
    {
487
        $fieldId = 'id';
488
        foreach (array_keys($fields) as $f) {
489
            $fieldName = $fields[$f]->getVar('field_name');
490
            if (0 == $f) {
491
                $fieldId = $fieldName;
492
            }
493
        }
494
495
        return $fieldId;
496
    }
497
498
    /**
499
     *  @public function getXoopsCodeGetFieldName
500
     *
501
     *  @param $fields
502
     *
503
     *  @return string
504
     */
505
    public function getXoopsCodeGetFieldName($fields)
506
    {
507
        foreach (array_keys($fields) as $f) {
508
            $fieldName = $fields[$f]->getVar('field_name');
509
        }
510
511
        return $fieldName;
0 ignored issues
show
Bug introduced by
The variable $fieldName 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...
512
    }
513
514
    /**
515
     *  @public function getXoopsCodeGetFieldParentId
516
     *
517
     *  @param $fields
518
     *
519
     *  @return string
520
     */
521 View Code Duplication
    public function getXoopsCodeGetFieldParentId($fields)
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...
522
    {
523
        $fieldPid = 'pid';
524
        foreach (array_keys($fields) as $f) {
525
            $fieldName = $fields[$f]->getVar('field_name');
526
            if (1 == $fields[$f]->getVar('field_parent')) {
527
                $fieldPid = $fieldName;
528
            }
529
        }
530
531
        return $fieldPid;
532
    }
533
534
    /**
535
     *  @public function getXoopsCodeUserSaveElements
536
     *
537
     *  @param $moduleDirname
538
     *  @param $tableName
539
     *  @param $fields
540
     *
541
     *  @return string
542
     */
543
    public function getXoopsCodeUserSaveElements($moduleDirname, $tableName, $fields)
544
    {
545
        $ret = '';
546
        foreach (array_keys($fields) as $f) {
547
            $fieldName = $fields[$f]->getVar('field_name');
548
            $fieldElement = $fields[$f]->getVar('field_element');
549
            if (1 == $fields[$f]->getVar('field_main')) {
550
                $fieldMain = $fieldName;
551
            }
552
            if ((5 == $fieldElement) || (6 == $fieldElement)) {
553
                $ret .= $this->getXoopsCodeCheckBoxOrRadioYNSetVar($tableName, $fieldName);
554
            } elseif (13 == $fieldElement) {
555
                $ret .= $this->getXoopsCodeUploadImageSetVar($moduleDirname, $tableName, $fieldName, $fieldMain);
0 ignored issues
show
Bug introduced by
The variable $fieldMain 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...
Bug introduced by
The method getXoopsCodeUploadImageSetVar() does not exist on TDMCreateXoopsCode. Did you maybe mean getXoopsCodeUploadImageGetVar()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
556
            } elseif (14 == $fieldElement) {
557
                $ret .= $this->getXoopsCodeUploadFileSetVar($moduleDirname, $tableName, $fieldName);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeUploadFileSetVar() does not seem to exist on object<TDMCreateXoopsCode>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
558
            } elseif (15 == $fieldElement) {
559
                $ret .= $this->getXoopsCodeTextDateSelectSetVar($tableName, $fieldName);
560
            } else {
561
                $ret .= $this->getXoopsCodeSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
562
            }
563
        }
564
565
        return $ret;
566
    }
567
568
    /*
569
    *  @public function getXoopsCodeXoopsRequest
570
    *  @param $left
571
    *  @param $var1
572
    *  @param $var2
573
    *  @param $type
574
    *  @param $metod
575
    *  @return string
576
    */
577
    public function getXoopsCodeXoopsRequest($left = '', $var1 = '', $var2 = '', $type = 'String', $metod = false)
578
    {
579
        if ($type == 'String') {
580
            $ret = "\${$left} = XoopsRequest::getString('{$var1}', '{$var2}');\n";
581
        } elseif ($type == 'Int') {
582
            $ret = "\${$left} = XoopsRequest::getInt('{$var1}', {$var2});\n";
583
        } elseif ($type == 'Int' && $metod !== false) {
584
            $ret = "\${$left} = XoopsRequest::getInt('{$var1}', {$var2}, '{$metod}');\n";
585
        }
586
587
        return $ret;
0 ignored issues
show
Bug introduced by
The variable $ret 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...
588
    }
589
590
    /**
591
     *  @public function getXoopsCodeTplAssign
592
     *
593
     *  @param string $tplString
594
     *  @param string $phpRender
595
     *
596
     *  @return string
597
     */
598
    public function getXoopsCodeTplAssign($tplString, $phpRender)
599
    {
600
        return "\$GLOBALS['xoopsTpl']->assign('{$tplString}', {$phpRender});\n";
601
    }
602
603
    /**
604
     *  @public function getXoopsCodeXoopsTplAppend
605
     *
606
     *  @param string $tplString
607
     *  @param string $phpRender
608
     *
609
     *  @return string
610
     */
611
    public function getXoopsCodeXoopsTplAppend($tplString, $phpRender)
612
    {
613
        return "\$GLOBALS['xoopsTpl']->append('{$tplString}', {$phpRender});\n";
614
    }
615
616
    /**
617
     *  @public function getXoopsCodeXoopsTplAppendByRef
618
     *
619
     *  @param string $tplString
620
     *  @param string $phpRender
621
     *
622
     *  @return string
623
     */
624
    public function getXoopsCodeXoopsTplAppendByRef($tplString, $phpRender)
625
    {
626
        return "\$GLOBALS['xoopsTpl']->appendByRef('{$tplString}', {$phpRender});\n";
627
    }
628
629
    /**
630
     *  @public function getXoopsCodePath
631
     *
632
     *  @param $directory
633
     *  @param $filename
634
     *  @param $condition
635
     *
636
     *  @return string
637
     */
638
    public function getXoopsCodePath($directory, $filename, $condition = false)
639
    {
640
        if ($condition === false) {
641
            $ret = "\$GLOBALS['xoops']->path({$directory}.'/{$filename}.php');\n";
642
        } else {
643
            $ret = "\$GLOBALS['xoops']->path({$directory}.'/{$filename}.php')";
644
        }
645
646
        return $ret;
647
    }
648
649
    /**
650
     *  @public function getXoopsCodeTplDisplay
651
     *
652
     *  @param null
653
     *
654
     *  @return string
655
     */
656
    public function getXoopsCodeTplDisplay()
657
    {
658
        return "\$GLOBALS['xoopsTpl']->display(\"db:{\$templateMain}\");\n";
659
    }
660
661
    /**
662
     *  @public function getXoopsCodeGetInfo
663
     *
664
     *  @param $string
665
     *  @param $isParam
666
     *
667
     *  @return string
668
     */
669
    public function getXoopsCodeGetInfo($string, $isParam = false)
670
    {
671
        if ($isParam === false) {
672
            $ret = "\$GLOBALS['xoopsModule']->getInfo('{$string}');\n";
673
        } else {
674
            $ret = "\$GLOBALS['xoopsModule']->getInfo('{$string}')";
675
        }
676
677
        return $ret;
678
    }
679
680
    /**
681
     *  @public function getXoopsCodeObjHandlerCreate
682
     *
683
     *  @param string $tableName
684
     *
685
     *  @return string
686
     */
687
    public function getXoopsCodeObjHandlerCreate($tableName)
688
    {
689
        $ret = "\${$tableName}Obj =& \${$tableName}Handler->create();\n";
690
691
        return $ret;
692
    }
693
	
694
	/**
695
     *  @public function getXoopsCodeObjHandlerCount
696
     *
697
     *  @param string $tableName
698
     *
699
     *  @return string
700
     */
701
    public function getXoopsCodeObjHandlerCount($tableName)
702
    {
703
        $ucfTableName = ucfirst($tableName);
704
		$ret = "\${$tableName}Count = \${$tableName}Handler->getCount{$ucfTableName}();\n";
705
706
        return $ret;
707
    }
708
	
709
	/**
710
     *  @public function getXoopsCodeObjHandlerAll
711
     *
712
     *  @param string $tableName
713
	 *  @param string $fieldMain
714
     *
715
     *  @return string
716
     */
717
    public function getXoopsCodeObjHandlerAll($tableName, $fieldMain)
718
    {
719
        $ucfTableName = ucfirst($tableName);
720
		$ret = "\${$tableName}All = \${$tableName}Handler->getAll{$ucfTableName}(0, 0, '{$fieldMain}');\n";
721
722
        return $ret;
723
    }
724
725
    /**
726
     *  @public function getXoopsCodeSetVarsObjects
727
     *
728
     *  @param $moduleDirname
729
     *  @param $tableName
730
     *  @param $fields
731
     *
732
     *  @return string
733
     */
734
    public function getXoopsCodeSetVarsObjects($moduleDirname, $tableName, $fields)
735
    {
736
        $ret = '';
737
738
        foreach (array_keys($fields) as $f) {
739
            $fieldName = $fields[$f]->getVar('field_name');
740
            $fieldElement = $fields[$f]->getVar('field_element');
741
            if (1 == $fields[$f]->getVar('field_main')) {
742
                $fieldMain = $fieldName;
743
            }
744
            if ($f > 0) { // If we want to hide field id
745
                switch ($fieldElement) {
746
                    case 5:
747
                    case 6:
748
                        $ret .= $this->getXoopsCodeCheckBoxOrRadioYNSetVar($tableName, $fieldName);
749
                        break;
750
                    case 11:
751
                        $ret .= $this->getXoopsCodeImageListSetVar($moduleDirname, $tableName, $fieldName);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeImageListSetVar() does not seem to exist on object<TDMCreateXoopsCode>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
752
                        break;
753
                    case 12:
754
                        $ret .= $this->getXoopsCodeFileSetVar($moduleDirname, $tableName, $fieldName, true);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeFileSetVar() does not exist on TDMCreateXoopsCode. Did you maybe mean getXoopsCodeSetVar()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
755
                        break;
756
                    case 13:
757
                        $ret .= $this->getXoopsCodeUploadImageSetVar($moduleDirname, $tableName, $fieldName, $fieldMain);
0 ignored issues
show
Bug introduced by
The variable $fieldMain 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...
Bug introduced by
The method getXoopsCodeUploadImageSetVar() does not exist on TDMCreateXoopsCode. Did you maybe mean getXoopsCodeUploadImageGetVar()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
758
                        break;
759
                    case 14:
760
                        $ret .= $this->getXoopsCodeFileSetVar($moduleDirname, $tableName, $fieldName);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeFileSetVar() does not exist on TDMCreateXoopsCode. Did you maybe mean getXoopsCodeSetVar()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
761
                        break;
762
                    case 15:
763
                        $ret .= $this->getXoopsCodeTextDateSelectSetVar($tableName, $fieldName);
764
                        break;
765
                    default:
766
                        $ret .= $this->getXoopsCodeSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
767
                        break;
768
                }
769
            }
770
        }
771
772
        return $ret;
773
    }
774
775
    /**
776
     *  @public function getXoopsCodeSecurity
777
     *
778
     *  @param $tableName
779
     *
780
     *  @return string
781
     */
782
    public function getXoopsCodeSecurity($tableName)
783
    {
784
        $securityError = $this->getXoopsCodeSecurityGetError();
785
        $implode = $this->phpcode->getPhpCodeImplode(',', $securityError);
786
        $content = $this->getXoopsCodeRedirectHeader($tableName, '', 3, $implode);
787
        $securityCheck = $this->getXoopsCodeSecurityCheck();
788
789
        return $this->phpcode->getPhpCodeConditions('!'.$securityCheck, '', '', $content);
790
    }
791
792
    /*
793
    *  @public function getXoopsCodeInsertData
794
    *  @param $tableName
795
    *  @param $language
796
    *  @return string
797
    */
798
    public function getXoopsCodeInsertData($tableName, $language)
799
    {
800
        $content = $this->getXoopsCodeRedirectHeader($tableName, '?op=list', 2, "{$language}FORM_OK");
801
        $handlerInsert = $this->getXoopsCodeHandler($tableName, $tableName, false, true, false, 'Obj');
802
803
        return $this->phpcode->getPhpCodeConditions($handlerInsert, '', '', $content);
804
    }
805
806
    /*
807
    *  @public function getXoopsCodeRedirectHeader
808
    *  @param $tableName
809
    *  @param $options
810
    *  @param $numb
811
    *  @param $var
812
    *  @return string
813
    */
814
    public function getXoopsCodeRedirectHeader($tableName, $options = '', $numb = '2', $var)
815
    {
816
        return "redirect_header('{$tableName}.php{$options}', {$numb}, {$var});\n";
817
    }
818
819
    /*
820
    *  @public function getXoopsCodeXoopsConfirm
821
    *  @param $tableName
822
    *  @param $options
823
    *  @param $fieldId
824
    *  @param $fieldMain
825
    *  @param $language
826
    *
827
    *  @return string
828
    */
829
    public function getXoopsCodeXoopsConfirm($tableName, $options = 'delete', $fieldId, $fieldMain, $language)
830
    {
831
        $stuOptions = strtoupper($options);
832
        $ret = "xoops_confirm(array('ok' => 1, '{$fieldId}' => \${$fieldId}, 'op' => {$options}), \$_SERVER['REQUEST_URI'], sprintf({$language}FORM_SURE_{$stuOptions}, \${$tableName}Obj->getVar('{$fieldMain}')));\n";
833
834
        return $ret;
835
    }
836
837
    /*
838
    *  @public function getXoopsCodeSecurityCheck
839
    *  @param null
840
    *  @return boolean
841
    */
842
    public function getXoopsCodeSecurityCheck()
843
    {
844
        return "\$GLOBALS['xoopsSecurity']->check()";
845
    }
846
847
    /*
848
    *  @public function getXoopsCodeSecurityGetError
849
    *  @param null
850
    *  @return string
851
    */
852
    public function getXoopsCodeSecurityGetError()
853
    {
854
        return "\$GLOBALS['xoopsSecurity']->getErrors()";
855
    }
856
857
    /**
858
     *  @public function getXoopsCodeGetFormError
859
     *
860
     *  @param $tableName
861
     *
862
     *  @return string
863
     */
864
    public function getXoopsCodeGetFormError($tableName)
865
    {
866
        $ret = <<<EOT
867
        // Get Form
868
        \$GLOBALS['xoopsTpl']->assign('error', \${$tableName}Obj->getHtmlErrors());
869
        \$form =& \${$tableName}Obj->getForm();
870
        \$GLOBALS['xoopsTpl']->assign('form', \$form->render());\n
871
EOT;
872
873
        return $ret;
874
    }
875
876
    /**
877
     *  @public function getXoopsCodeGetFormId
878
     *
879
     *  @param string $tableName
880
     *  @param string $fieldId
881
     *
882
     *  @return string
883
     */
884
    public function getXoopsCodeGetFormId($tableName, $fieldId)
885
    {
886
        $ret = <<<EOT
887
        // Get Form
888
        \${$tableName}Obj = \${$tableName}Handler->get(\${$fieldId});
889
        \$form = \${$tableName}Obj->getForm();
890
        \$GLOBALS['xoopsTpl']->assign('form', \$form->render());\n
891
EOT;
892
893
        return $ret;
894
    }
895
896
    /**
897
     *  @public function getXoopsCodeHandler
898
     *
899
     *  @param string $tableName
900
     *  @param string $var
901
     *
902
     *  @return string
903
     */
904
    public function getXoopsCodeHandler($tableName, $var, $get = false, $insert = false, $delete = false, $obj = '')
905
    {
906
        if ($get) {
907
            $ret = "\${$tableName}Handler->get(\${$var});";
908
        } elseif ($insert && ($obj != '')) {
909
            $ret = "\${$tableName}Handler->insert(\${$var}{$obj});";
910
        } elseif ($delete && ($obj != '')) {
911
            $ret = "\${$tableName}Handler->delete(\${$var}{$obj});";
912
        }
913
914
        return $ret;
0 ignored issues
show
Bug introduced by
The variable $ret 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...
915
    }
916
917
    /*
918
    *  @public function getXoopsCodeCaseDelete
919
    *  @param string $tableName
920
    *  @param string $language
921
    *  @param string $fieldId
922
    *  @param string $fieldMain
923
    *  @return string
924
    */
925 View Code Duplication
    public function getXoopsCodeCaseDelete($language, $tableName, $fieldId, $fieldMain)
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...
926
    {
927
        $ret = <<<EOT
928
        \${$tableName}Obj =& \${$tableName}Handler->get(\${$fieldId});
929
        if (isset(\$_REQUEST['ok']) && 1 == \$_REQUEST['ok']) {
930
            if ( !\$GLOBALS['xoopsSecurity']->check() ) {
931
                redirect_header('{$tableName}.php', 3, implode(', ', \$GLOBALS['xoopsSecurity']->getErrors()));
932
            }
933
            if (\${$tableName}Handler->delete(\${$tableName}Obj)) {
934
                redirect_header('{$tableName}.php', 3, {$language}FORM_DELETE_OK);
935
            } else {
936
                echo \${$tableName}Obj->getHtmlErrors();
937
            }
938
        } else {
939
            xoops_confirm(array('ok' => 1, '{$fieldId}' => \${$fieldId}, 'op' => 'delete'), \$_SERVER['REQUEST_URI'], sprintf({$language}FORM_SURE_DELETE, \${$tableName}Obj->getVar('{$fieldMain}')));
940
        }\n
941
EOT;
942
        /*$isset = $this->phpcode->getPhpCodeIsset($fieldId);
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
943
        $if1 = $this->phpcode->getPhpCodeConditions($isset, '', '', "\${$tableName}Obj =& ".$get);
944
        $get = $this->getXoopsCodeHandler($tableName, $fieldId, true);
945
        $if2 = $this->phpcode->getPhpCodeConditions($isset, '', '', "\${$tableName}Obj =& ".$get);
946
        $ret = $this->phpcode->getPhpCodeConditions($isset, '', '', "\${$tableName}Obj =& ".$get);
947
        //$ret .= $this->getXoopsCodeSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
948
        $handlerInsert = $this->$this->getXoopsCodeHandler($tableName, $tableName, false, true, false, 'Obj');
949
        $redirect = $this->getXoopsCodeRedirectHeader($tableName, '', 2, "{$language}FORM_DELETE_OK");
950
951
        $else = $this->getXoopsCodeTplAssign('error', "\${$tableName}Obj->getHtmlErrors()");
952
953
        $ret .= $this->phpcode->getPhpCodeConditions($handlerInsert, '', '', $redirect, $else);*/
954
955
        return $this->phpcode->getPhpCodeCaseSwitch('delete', $ret);
956
    }
957
958
    /*
959
    *  @public function getXoopsCodeUpdate
960
    *  @param $language
961
    *  @param $tableName
962
    *  @param $fieldId
963
    *  @param $fieldName
964
    *  @return string
965
    */
966
    public function getXoopsCodeUpdate($language, $tableName, $fieldId, $fieldName)
967
    {
968
        $isset = $this->phpcode->getPhpCodeIsset($fieldId);
969
        $get = $this->getXoopsCodeHandler($tableName, $fieldId, true);
970
        $content = $this->phpcode->getPhpCodeConditions($isset, '', '', "\${$tableName}Obj =& ".$get);
971
        $content .= $this->getXoopsCodeSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
972
        $handlerInsert = $this->$this->getXoopsCodeHandler($tableName, $tableName, false, true, false, 'Obj');
973
        $redirect = $this->getXoopsCodeRedirectHeader($tableName, '?op=list', 2, "{$language}FORM_UPDATE_OK");
974
        $content .= $this->phpcode->getPhpCodeConditions($handlerInsert, '', '', $redirect);
975
976
        $content .= $this->getXoopsCodeTplAssign('error', "\${$tableName}Obj->getHtmlErrors()");
977
978
        return $this->phpcode->getPhpCodeCaseSwitch('update', $content);
979
    }
980
}
981