Completed
Pull Request — master (#51)
by Gino
06:03
created

TDMCreateXoopsCode::getTopicGetVar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
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: 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 $tdmcfile = null;
34
35
    /*
36
    * @var mixed
37
    */
38
    private $phpcode = null;
39
40
    /*
41
    * @var string
42
    */
43
    protected $xoopscode;
44
45
    /*
46
    *  @public function constructor
47
    *  @param null
48
    */
49
    /**
50
     *
51
     */
52
    public function __construct()
53
    {
54
        $this->tdmcfile = TDMCreateFile::getInstance();
55
        $this->phpcode = TDMCreatePhpCode::getInstance();
56
    }
57
58
    /*
59
    *  @static function &getInstance
60
    *  @param null
61
    */
62
    /**
63
     * @return TDMCreateXoopsCode
64
     */
65
    public static function &getInstance()
66
    {
67
        static $instance = false;
68
        if (!$instance) {
69
            $instance = new self();
70
        }
71
72
        return $instance;
73
    }
74
75
    /*
76
     *  @public function getXoopsCodeSwitch
77
     *  @param $op
78
     *  @param $cases
79
     *  @param $defaultAfterCase
80
     *  @param $default
81
     *  @param $tabulation
82
     *
83
     * @return string
84
     */
85
    public function getXoopsCodeSwitch($op = '', $cases = array(), $defaultAfterCase = false, $default = false, $t = '')
86
    {
87
        $contentSwitch = $this->phpcode->getPhpCodeCaseSwitch($cases, $defaultAfterCase, $default, $t);
88
89
        return $this->phpcode->getPhpCodeSwitch($op, $contentSwitch, $t);
90
    }
91
92
    /*
93
    *  @public function getXoopsCodeEqualsOperator
94
    *  @param $left
95
    *  @param $right
96
    *  @param boolean $ref
97
    *  @return string
98
    */
99
    public function getXoopsCodeEqualsOperator($left, $right, $ref = false)
100
    {
101
        if (false === $ref) {
102
            $ret = "{$left}= {$right};\n";
103
        } else {
104
            $ret = "{$left}=& {$right};\n";
105
        }
106
107
        return $ret;
108
    }
109
110
    /*
111
    *  @public function getXoopsCodeCPHeader
112
    *  @param null
113
    *  @return string
114
    */
115
    public function getXoopsCodeCPHeader()
116
    {
117
        return "xoops_cp_header();\n";
118
    }
119
120
    /*
121
    *  @public function getXoopsCodeCPFooter
122
    *  @param null
123
    *  @return string
124
    */
125
    public function getXoopsCodeCPFooter()
126
    {
127
        return "xoops_cp_footer();\n";
128
    }
129
130
    /**
131
     *  @public function getXoopsCodeLoad
132
     *
133
     *  @param $var
134
     *
135
     *  @return string
136
     */
137
    public function getXoopsCodeLoad($var = '')
138
    {
139
        return "xoops_load('{$var}');\n";
140
    }
141
142
    /**
143
     *  @public function getXoopsCodeLoadLanguage
144
     *
145
     *  @param $lang
146
     *
147
     *  @return string
148
     */
149
    public function getXoopsCodeLoadLanguage($lang)
150
    {
151
        return "xoops_loadLanguage('{$lang}');\n";
152
    }
153
154
    /*
155
    *  @public function getXoopsCodeSetVar
156
    *  @param $tableName
157
    *  @param $fieldName
158
    *  @param $var
159
    *  @return string
160
    */
161
    public function getXoopsCodeSetVar($tableName, $fieldName, $var)
162
    {
163
        return "\${$tableName}Obj->setVar('{$fieldName}', {$var});\n";
164
    }
165
166
    /*
167
    *  @public function getXoopsCodeGetVar
168
    *  @param $varLeft
169
    *  @param $handle
170
    *  @param $var
171
    *  @param $isParam
172
    *
173
    *  @return string
174
    */
175
    public function getXoopsCodeGetVar($varLeft = '', $handle = '', $var = '', $isParam = false)
176
    {
177
        if ($isParam === false) {
178
            $ret = "\${$varLeft} = \${$handle}->getVar('{$var}');\n";
179
        } else {
180
            $ret = "\${$handle}->getVar('{$var}')";
181
        }
182
183
        return $ret;
184
    }
185
186
    /*
187
    *  @public function getXoopsCodeGroupPermForm
188
    *  @param $varLeft
189
    *  @param $formTitle
190
    *  @param $moduleId
191
    *  @param $permName
192
    *  @param $permDesc
193
    *  @param $filename
194
    *
195
    *  @return string
196
    */
197
    public function getXoopsCodeGroupPermForm($varLeft = '', $formTitle = '', $moduleId = '', $permName = '', $permDesc = '', $filename = '')
198
    {
199
        return "\${$varLeft} = new XoopsGroupPermForm({$formTitle}, {$moduleId}, {$permName}, {$permDesc}, {$filename});\n";
200
    }
201
202
    /*
203
    *  @public function getXoopsCodeAddItem
204
    *  @param $varLeft
205
    *  @param $paramLeft
206
    *  @param $paramRight
207
    *
208
    *  @return string
209
    */
210
    public function getXoopsCodeAddItem($varLeft = '', $paramLeft = '', $paramRight = '')
211
    {
212
        return "\${$varLeft}->addItem({$paramLeft}, {$paramRight});\n";
213
    }
214
215
    /*
216
    *  @public function getXoopsCodeTextDateSelectSetVar
217
    *  @param $tableName
218
    *  @param $fieldName
219
    *  @return string
220
    */
221
    public function getXoopsCodeTextDateSelectSetVar($tableName, $fieldName)
222
    {
223
        return $this->getXoopsCodeSetVar($tableName, $fieldName, "strtotime(\$_POST['{$fieldName}'])");
224
    }
225
226
    /*
227
    *  @public function getXoopsCodeCheckBoxOrRadioYNSetVar
228
    *  @param $tableName
229
    *  @param $fieldName
230
    *  @return string
231
    */
232
    public function getXoopsCodeCheckBoxOrRadioYNSetVar($tableName, $fieldName)
233
    {
234
        return $this->getXoopsCodeSetVar($tableName, $fieldName, "((1 == \$_REQUEST['{$fieldName}']) ? '1' : '0')");
235
    }
236
237
    /*
238
    *  @public function getXoopsCodeXoopsMediaUploader
239
    *  @param $var
240
    *  @param $dirPath
241
    *  @param $tableName
242
    *  @param $moduleDirname
243
    *  @return string
244
    */
245
    public function getXoopsCodeXoopsMediaUploader($var = '', $dirPath, $tableName, $moduleDirname)
246
    {
247
        $mimetypes = $this->getXoopsCodeGetConfig($moduleDirname, 'mimetypes');
248
        $maxsize = $this->getXoopsCodeGetConfig($moduleDirname, 'maxsize');
249
250
        return "\${$var} = new XoopsMediaUploader({$dirPath} . '/{$tableName}', {$mimetypes}, {$maxsize}, null, null);\n";
251
    }
252
253
    /*
254
    *  @public function getXoopsCodeXoopsCaptcha
255
    *  @param null
256
    *  @return string
257
    */
258
    public function getXoopsCodeXoopsCaptcha()
259
    {
260
        return "\$xoopsCaptcha = XoopsCaptcha::getInstance();\n";
261
    }
262
263
    /*
264
    *  @public function getXoopsCodeGetConfig
265
    *  @param $moduleDirname
266
    *  @param $name
267
    *  @return string
268
    */
269
    public function getXoopsCodeGetConfig($moduleDirname, $name)
270
    {
271
        return "\${$moduleDirname}->getConfig('{$name}')";
272
    }
273
274
    /*
275
    *  @public function getXoopsCodeIdGetVar
276
    *  @param $lpFieldName
277
    *  @return string
278
    */
279
    public function getXoopsCodeIdGetVar($lpFieldName)
280
    {
281
        return "\${$lpFieldName}['id'] = \$i;\n";
282
    }
283
284
    /*
285
    *  @public function getXoopsCodeGetVarAll
286
    *  @param $lpFieldName
287
    *  @param $rpFieldName
288
    *  @param $tableName
289
    *  @param $fieldName
290
    *  @return string
291
    */
292
    public function getXoopsCodeGetVarAll($lpFieldName, $rpFieldName, $tableName, $fieldName)
293
    {
294
        return "\${$lpFieldName}['{$rpFieldName}'] = \${$tableName}All[\$i]->getVar('{$fieldName}');\n";
295
    }
296
297
    /*
298
    *  @public function getXoopsHandlerInstance
299
    *  @param $moduleDirname
300
    *  
301
    *  @return string
302
    */
303
    public function getXoopsHandlerInstance($moduleDirname)
304
    {
305
        $ucfModuleDirname = ucfirst($moduleDirname);
306
        $ret = "// Get instance of module\n";
307
        $ret .= "\${$moduleDirname} = {$ucfModuleDirname}Helper::getInstance();\n";
308
309
        return $ret;
310
    }
311
312
    /*
313
    *  @public function getXoopsHandlerLine
314
    *  @param $moduleDirname
315
    *  @param $tableName
316
    *  @return string
317
    */
318
    public function getXoopsHandlerLine($moduleDirname, $tableName)
319
    {
320
        return "\${$tableName}Handler =& \${$moduleDirname}->getHandler('{$tableName}');\n";
321
    }
322
323
    /*
324
    *  @public function getXoopsSimpleForm
325
    *  @param $left
326
    *  @param $element
327
    *  @param $elementsContent
328
    *  @param $caption
329
    *  @param $var
330
    *  @param $filename
331
    *  @param $type
332
    *  
333
    *  @return string
334
    */
335
    public function getXoopsSimpleForm($left = '', $element = '', $elementsContent = '', $caption = '', $var = '', $filename = '', $type = 'post')
336
    {
337
        $ret = "\${$left} = new XoopsSimpleForm({$caption}, '{$var}', '{$filename}.php', '{$type}');\n";
338
        if (!empty($elementsContent)) {
339
            $ret .= "{$elementsContent}";
340
        }
341
        $ret .= "\${$left}->addElement(\${$element});\n";
342
        $ret .= "\${$left}->display();\n";
343
344
        return $ret;
345
    }
346
347
    /*
348
    *  @public function getXoopsFormSelect
349
    *  @param $varSelect
350
    *  @param $caption
351
    *  @param $var
352
    *  @param $options
353
    *  @param $setExtra
354
    *  
355
    *  @return string
356
    */
357
    public function getXoopsFormSelect($varSelect = '', $caption = '', $var = '', $options = array(), $setExtra = true)
358
    {
359
        $ret = "\${$varSelect} = new XoopsFormSelect({$caption}, '{$var}', \${$var});\n";
360
        if (false !== $setExtra) {
361
            $ret .= "\${$varSelect}->setExtra('{$setExtra}');\n";
362
        }
363
        foreach ($options as $key => $value) {
364
            $ret .= "\${$varSelect}->addOption('{$key}', {$value});\n";
365
        }
366
367
        return $ret;
368
    }
369
370
    /*
371
    *  @public function getXoopsCodeTopicGetVar
372
    *  @param $lpFieldName
373
    *  @param $rpFieldName
374
    *  @param $tableName
375
    *  @param $tableNameTopic
376
    *  @param $fieldNameParent
377
    *  @param $fieldNameTopic
378
    *  @return string
379
    */
380
    public function getXoopsCodeTopicGetVar($lpFieldName, $rpFieldName, $tableName, $tableNameTopic, $fieldNameParent, $fieldNameTopic)
381
    {
382
        $ret = <<<EOT
383
\t\t\t\t// Get Var {$fieldNameParent}
384
\t\t\t\t\${$rpFieldName} =& \${$tableNameTopic}Handler->get(\${$tableName}All[\$i]->getVar('{$fieldNameParent}'));
385
\t\t\t\t\${$lpFieldName}['{$rpFieldName}'] = \${$rpFieldName}->getVar('{$fieldNameTopic}');\n
386
EOT;
387
388
        return $ret;
389
    }
390
391
    /*
392
    *  @public function getXoopsCodeParentTopicGetVar
393
    *  @param $moduleDirname
394
    *  @param $lpFieldName
395
    *  @param $rpFieldName
396
    *  @param $tableName
397
    *  @param $tableSoleNameTopic
398
    *  @param $tableNameTopic
399
    *  @param $fieldNameParent
400
    *  @return string
401
    */
402 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...
403
    {
404
        $ret = <<<EOT
405
\t\t\t\tif(!isset(\${$tableNameTopic}Handler)) {
406
\t\t\t\t\t// Get {$tableNameTopic} Handler
407
\t\t\t\t\t\${$tableNameTopic}Handler =& \${$moduleDirname}->getHandler('{$tableNameTopic}');
408
\t\t\t\t}
409
\t\t\t\t// Get Var {$fieldNameParent}
410
\t\t\t\t\${$lpFieldName}['{$rpFieldName}'] = \${$tableNameTopic}Handler->get{$tableSoleNameTopic}FromId(\${$tableName}All[\$i]->getVar('{$fieldNameParent}'));\n
411
EOT;
412
413
        return $ret;
414
    }
415
416
    /*
417
    *  @public function getXoopsCodeUploadImageGetVar
418
    *  @param $lpFieldName
419
    *  @param $rpFieldName
420
    *  @param $tableName
421
    *  @param $fieldName
422
    *  @return string
423
    */
424
    public function getXoopsCodeUploadImageGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
425
    {
426
        $ret = <<<EOT
427
\t\t\t\t// Get Var {$fieldName}
428
\t\t\t\t\${$fieldName} = \${$tableName}All[\$i]->getVar('{$fieldName}');
429
\t\t\t\t\${$lpFieldName}['{$rpFieldName}'] = \${$fieldName} ? \${$fieldName} : 'blank.gif';\n
430
EOT;
431
432
        return $ret;
433
    }
434
    /*
435
    *  @public function getXoopsCodeUrlFileGetVar
436
    *  @param $lpFieldName
437
    *  @param $rpFieldName
438
    *  @param $tableName
439
    *  @param $fieldName
440
    *  @return string
441
    */
442
    public function getXoopsCodeUrlFileGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
443
    {
444
        return $this->getXoopsCodeGetVarAll($lpFieldName, $rpFieldName, $tableName, $fieldName);
445
    }
446
    /*
447
    *  @public function getXoopsCodeTextAreaGetVar
448
    *  @param $lpFieldName
449
    *  @param $rpFieldName
450
    *  @param $tableName
451
    *  @param $fieldName
452
    *  @return string
453
    */
454
    public function getXoopsCodeTextAreaGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
455
    {
456
        return "\${$lpFieldName}['{$rpFieldName}'] = strip_tags(\${$tableName}All[\$i]->getVar('{$fieldName}'));\n";
457
    }
458
459
    /*
460
    *  @public function getXoopsCodeSelectUserGetVar
461
    *  @param $lpFieldName
462
    *  @param $rpFieldName
463
    *  @param $tableName
464
    *  @param $fieldName
465
    * @return string
466
    */
467
    public function getXoopsCodeSelectUserGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
468
    {
469
        return "\${$lpFieldName}['{$rpFieldName}'] = XoopsUser::getUnameFromId(\${$tableName}All[\$i]->getVar('{$fieldName}'), 's');\n";
470
    }
471
472
    /*
473
    *  @public function getXoopsCodeTextDateSelectGetVar
474
    *  @param $lpFieldName
475
    *  @param $rpFieldName
476
    *  @param $tableName
477
    *  @param $fieldName
478
    *  @return string
479
    */
480
    public function getXoopsCodeTextDateSelectGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
481
    {
482
        return "\${$lpFieldName}['{$rpFieldName}'] = formatTimeStamp(\${$tableName}All[\$i]->getVar('{$fieldName}'), 's');\n";
483
    }
484
485
    /*
486
    *  @public function getXoopsCodeUserHeader
487
    *  @param $moduleDirname
488
    *  @param $tableName
489
    *  @return string
490
    */
491
    public function getXoopsCodeXoopsOptionTemplateMain($moduleDirname, $tableName)
492
    {
493
        return "\$GLOBALS['xoopsOption']['template_main'] = '{$moduleDirname}_{$tableName}.tpl';\n";
494
    }
495
496
    /*
497
    *  @public function getXoopsCodeUserHeader
498
    *  @param $moduleDirname
499
    *  @param $tableName
500
    *  @return string
501
    */
502
    public function getXoopsCodeUserHeader($moduleDirname, $tableName)
503
    {
504
        $ret = $this->phpcode->getPhpCodeIncludeDir('__DIR__', 'header');
505
        $ret .= $this->getXoopsCodeXoopsOptionTemplateMain($moduleDirname, $tableName);
506
        $ret .= $this->phpcode->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'header', true);
507
508
        return $ret;
509
    }
510
511
    /*
512
    *  @public function getXoopsCodePermissionsHeader
513
    *  @param null
514
    */
515
    /**
516
     * @return string
517
     */
518
    public function getXoopsCodePermissionsHeader()
519
    {
520
        $ret = $this->phpcode->getPhpCodeCommentLine('Permission');
521
        $ret .= $this->phpcode->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/xoopsform/grouppermform', true);
522
        $ret .= $this->getXoopsCodeEqualsOperator('$gperm_handler', "xoops_gethandler('groupperm')", true);
523
        $groups = $this->getXoopsCodeEqualsOperator('$groups', '$xoopsUser->getGroups()');
524
        $elseGroups = $this->getXoopsCodeEqualsOperator('$groups', 'XOOPS_GROUP_ANONYMOUS');
525
        $ret .= $this->phpcode->getPhpCodeConditions('is_object($xoopsUser)', '', $type = '', $groups, $elseGroups);
526
527
        return $ret;
528
    }
529
530
    /**
531
     *  @public function getXoopsCodeGetFieldId
532
     *
533
     *  @param $fields
534
     *
535
     *  @return string
536
     */
537 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...
538
    {
539
        $fieldId = 'id';
540
        foreach (array_keys($fields) as $f) {
541
            $fieldName = $fields[$f]->getVar('field_name');
542
            if (0 == $f) {
543
                $fieldId = $fieldName;
544
            }
545
        }
546
547
        return $fieldId;
548
    }
549
550
    /**
551
     *  @public function getXoopsCodeGetFieldName
552
     *
553
     *  @param $fields
554
     *
555
     *  @return string
556
     */
557
    public function getXoopsCodeGetFieldName($fields)
558
    {
559
        foreach (array_keys($fields) as $f) {
560
            $fieldName = $fields[$f]->getVar('field_name');
561
        }
562
563
        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...
564
    }
565
566
    /**
567
     *  @public function getXoopsCodeGetFieldParentId
568
     *
569
     *  @param $fields
570
     *
571
     *  @return string
572
     */
573 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...
574
    {
575
        $fieldPid = 'pid';
576
        foreach (array_keys($fields) as $f) {
577
            $fieldName = $fields[$f]->getVar('field_name');
578
            if (1 == $fields[$f]->getVar('field_parent')) {
579
                $fieldPid = $fieldName;
580
            }
581
        }
582
583
        return $fieldPid;
584
    }
585
586
    /**
587
     *  @public function getXoopsCodeUserSaveElements
588
     *
589
     *  @param $moduleDirname
590
     *  @param $tableName
591
     *  @param $fields
592
     *
593
     *  @return string
594
     */
595 View Code Duplication
    public function getXoopsCodeUserSaveElements($moduleDirname, $tableName, $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...
596
    {
597
        $ret = '';
598
        foreach (array_keys($fields) as $f) {
599
            $fieldName = $fields[$f]->getVar('field_name');
600
            $fieldElement = $fields[$f]->getVar('field_element');
601
            if (1 == $fields[$f]->getVar('field_main')) {
602
                $fieldMain = $fieldName;
603
            }
604
            if ((5 == $fieldElement) || (6 == $fieldElement)) {
605
                $ret .= $this->getXoopsCodeCheckBoxOrRadioYNSetVar($tableName, $fieldName);
606
            } elseif (13 == $fieldElement) {
607
                $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...
608
            } elseif (14 == $fieldElement) {
609
                $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...
610
            } elseif (15 == $fieldElement) {
611
                $ret .= $this->getXoopsCodeTextDateSelectSetVar($tableName, $fieldName);
612
            } else {
613
                $ret .= $this->getXoopsCodeSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
614
            }
615
        }
616
617
        return $ret;
618
    }
619
620
    /*
621
    *  @public function getXoopsCodeXoopsRequest
622
    *  @param $left
623
    *  @param $var1
624
    *  @param $var2
625
    *  @param $type
626
    *  @param $metod
627
    *  @return string
628
    */
629
    public function getXoopsCodeXoopsRequest($left = '', $var1 = '', $var2 = '', $type = 'String', $metod = false)
630
    {
631
        $ret = '';
632
        $intVars = ($var2 != '') ? "'{$var1}', {$var2}" : "'{$var1}'";
633
        if ($type == 'String') {
634
            $ret .= "\${$left} = XoopsRequest::getString('{$var1}', '{$var2}');\n";
635
        } elseif ($type == 'Int') {
636
            $ret .= "\${$left} = XoopsRequest::getInt({$intVars});\n";
637
        } elseif ($type == 'Int' && $metod !== false) {
638
            $ret .= "\${$left} = XoopsRequest::getInt({$intVars}, '{$metod}');\n";
639
        }
640
641
        return $ret;
642
    }
643
644
    /**
645
     *  @public function getXoopsCodeTplAssign
646
     *
647
     *  @param $tplString
648
     *  @param $phpRender
649
     *
650
     *  @return string
651
     */
652
    public function getXoopsCodeTplAssign($tplString, $phpRender)
653
    {
654
        return "\$GLOBALS['xoopsTpl']->assign('{$tplString}', {$phpRender});\n";
655
    }
656
657
    /**
658
     *  @public function getXoopsCodeXoopsTplAppend
659
     *
660
     *  @param $tplString
661
     *  @param $phpRender
662
     *
663
     *  @return string
664
     */
665
    public function getXoopsCodeXoopsTplAppend($tplString, $phpRender)
666
    {
667
        return "\$GLOBALS['xoopsTpl']->append('{$tplString}', {$phpRender});\n";
668
    }
669
670
    /**
671
     *  @public function getXoopsCodeXoopsTplAppendByRef
672
     *
673
     *  @param $tplString
674
     *  @param $phpRender
675
     *
676
     *  @return string
677
     */
678
    public function getXoopsCodeXoopsTplAppendByRef($tplString, $phpRender)
679
    {
680
        return "\$GLOBALS['xoopsTpl']->appendByRef('{$tplString}', {$phpRender});\n";
681
    }
682
683
    /**
684
     *  @public function getXoopsCodePath
685
     *
686
     *  @param $directory
687
     *  @param $filename
688
     *  @param $isParam
689
     *
690
     *  @return string
691
     */
692
    public function getXoopsCodePath($directory, $filename, $isParam = false)
693
    {
694
        if ($isParam === false) {
695
            $ret = "\$GLOBALS['xoops']->path({$directory}.'/{$filename}.php');\n";
696
        } else {
697
            $ret = "\$GLOBALS['xoops']->path({$directory}.'/{$filename}.php')";
698
        }
699
700
        return $ret;
701
    }
702
703
    /**
704
     *  @public function getXoopsCodeTplDisplay
705
     *
706
     *  @param null
707
     *
708
     *  @return string
709
     */
710
    public function getXoopsCodeTplDisplay()
711
    {
712
        return "\$GLOBALS['xoopsTpl']->display(\"db:{\$templateMain}\");\n";
713
    }
714
715
    /**
716
     *  @public function getXoopsCodeGetInfo
717
     *
718
     *  @param $left
719
     *  @param $string
720
     *  @param $isParam
721
     *
722
     *  @return string
723
     */
724
    public function getXoopsCodeGetInfo($left = '', $string, $isParam = false)
725
    {
726
        if ($isParam === false) {
727
            $ret = "\${$left} = \$GLOBALS['xoopsModule']->getInfo('{$string}');\n";
728
        } else {
729
            $ret = "\$GLOBALS['xoopsModule']->getInfo('{$string}')";
730
        }
731
732
        return $ret;
733
    }
734
735
    /**
736
     *  @public function getXoopsCodeObjHandlerCreate
737
     *
738
     *  @param $tableName
739
     *
740
     *  @return string
741
     */
742
    public function getXoopsCodeObjHandlerCreate($tableName)
743
    {
744
        return "\${$tableName}Obj =& \${$tableName}Handler->create();\n";
745
    }
746
747
    /**
748
     *  @public function getXoopsCodeObjHandlerCount
749
     *
750
     *  @param $tableName
751
     *
752
     *  @return string
753
     */
754
    public function getXoopsCodeObjHandlerCount($tableName)
755
    {
756
        $ucfTableName = ucfirst($tableName);
757
        $ret = "\${$tableName}Count = \${$tableName}Handler->getCount{$ucfTableName}();\n";
758
759
        return $ret;
760
    }
761
762
    /**
763
     *  @public function getXoopsCodeObjHandlerAll
764
     *
765
     *  @param $tableName
766
     *  @param $fieldMain
767
     *  @param $start
768
     *  @param $limit
769
     *
770
     *  @return string
771
     */
772
    public function getXoopsCodeObjHandlerAll($tableName, $fieldMain, $start = '0', $limit = '0')
773
    {
774
        $ucfTableName = ucfirst($tableName);
775
        $param = ($fieldMain != '') ? "{$start}, {$limit}, '{$fieldMain}'" : "{$start}, {$limit}";
776
        $ret = "\${$tableName}All = \${$tableName}Handler->getAll{$ucfTableName}({$param});\n";
777
778
        return $ret;
779
    }
780
781
    /**
782
     *  @public function getXoopsCodeGetValues
783
     *
784
     *  @param $tableName
785
     *  @param $tableSoleName
786
     *
787
     *  @return string
788
     */
789
    public function getXoopsCodeGetValues($tableName, $tableSoleName, $index = 'i')
790
    {
791
        $ucfTableName = ucfirst($tableName);
792
        $ret = "\${$tableSoleName} = \${$tableName}All[\${$index}]->getValues{$ucfTableName}();\n";
793
794
        return $ret;
795
    }
796
797
    /**
798
     *  @public function getXoopsCodeObjectTree
799
     *
800
     *  @param $tableName
801
     *  @param $fieldId
802
     *  @param $fieldParent
803
     *
804
     *  @return string
805
     */
806
    public function getXoopsCodeObjectTree($tableName, $fieldId, $fieldParent)
807
    {
808
        $ret = "\$mytree = new XoopsObjectTree(\${$tableName}All, '{$fieldId}', '{$fieldParent}');\n";
809
810
        return $ret;
811
    }
812
813
    /**
814
     *  @public function getXoopsCodeSetVarsObjects
815
     *
816
     *  @param $moduleDirname
817
     *  @param $tableName
818
     *  @param $fields
819
     *
820
     *  @return string
821
     */
822
    public function getXoopsCodeSetVarsObjects($moduleDirname, $tableName, $fields)
823
    {
824
        $ret = '';
825
        foreach (array_keys($fields) as $f) {
826
            $fieldName = $fields[$f]->getVar('field_name');
827
            $fieldElement = $fields[$f]->getVar('field_element');
828
            if (1 == $fields[$f]->getVar('field_main')) {
829
                $fieldMain = $fieldName;
830
            }
831
            if ($f > 0) { // If we want to hide field id
832
                switch ($fieldElement) {
833
                    case 5:
834
                    case 6:
835
                        $ret .= $this->getXoopsCodeCheckBoxOrRadioYNSetVar($tableName, $fieldName);
836
                        break;
837
                    case 11:
838
                        $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...
839
                        break;
840
                    case 12:
841
                        $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...
842
                        break;
843
                    case 13:
844
                        $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...
845
                        break;
846
                    case 14:
847
                        $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...
848
                        break;
849
                    case 15:
850
                        $ret .= $this->getXoopsCodeTextDateSelectSetVar($tableName, $fieldName);
851
                        break;
852
                    default:
853
                        $ret .= $this->getXoopsCodeSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
854
                        break;
855
                }
856
            }
857
        }
858
859
        return $ret;
860
    }
861
862
    /**
863
     *  @public function getXoopsCodeSecurity
864
     *
865
     *  @param $tableName
866
     *
867
     *  @return string
868
     */
869
    public function getXoopsCodeSecurity($tableName)
870
    {
871
        $securityError = $this->getXoopsCodeSecurityGetError();
0 ignored issues
show
Bug introduced by
The method getXoopsCodeSecurityGetError() does not exist on TDMCreateXoopsCode. Did you maybe mean getXoopsCodeSecurity()?

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...
872
        $implode = $this->phpcode->getPhpCodeImplode(',', $securityError);
873
        $content = $this->getXoopsCodeRedirectHeader($tableName, '', 3, $implode);
874
        $securityCheck = $this->getXoopsCodeSecurityCheck();
875
876
        return $this->phpcode->getPhpCodeConditions('!'.$securityCheck, '', '', $content);
877
    }
878
879
    /*
880
    *  @public function getXoopsCodeInsertData
881
    *  @param $tableName
882
    *  @param $language
883
    *  @return string
884
    */
885
    public function getXoopsCodeInsertData($tableName, $language)
886
    {
887
        $content = $this->getXoopsCodeRedirectHeader($tableName, '?op=list', 2, "{$language}FORM_OK");
888
        $handlerInsert = $this->getXoopsCodeHandler($tableName, $tableName, false, true, false, 'Obj');
889
890
        return $this->phpcode->getPhpCodeConditions($handlerInsert, '', '', $content);
891
    }
892
893
    /*
894
    *  @public function getXoopsCodeRedirectHeader
895
    *  @param $tableName
896
    *  @param $options
897
    *  @param $numb
898
    *  @param $var
899
    *  @return string
900
    */
901
    public function getXoopsCodeRedirectHeader($tableName, $options = '', $numb = '2', $var)
902
    {
903
        return "redirect_header('{$tableName}.php{$options}', {$numb}, {$var});\n";
904
    }
905
906
    /*
907
    *  @public function getXoopsCodeXoopsConfirm
908
    *  @param $tableName
909
    *  @param $language
910
    *  @param $fieldId
911
    *  @param $fieldMain    
912
    *  @param $options
913
    *
914
    *  @return string
915
    */
916
    public function getXoopsCodeXoopsConfirm($tableName, $language, $fieldId, $fieldMain, $options = 'delete')
917
    {
918
        $stuOptions = strtoupper($options);
919
        $ccFieldId = $this->tdmcfile->getCamelCase($fieldId, false, true);
920
        $ret = "xoops_confirm(array('ok' => 1, '{$fieldId}' => \${$ccFieldId}, 'op' => {$options}), \$_SERVER['REQUEST_URI'], sprintf({$language}FORM_SURE_{$stuOptions}, \${$tableName}Obj->getVar('{$fieldMain}')));\n";
921
922
        return $ret;
923
    }
924
925
    /*
926
    *  @public function getXoopsCodeAddStylesheet
927
    *  @param $style
928
    *  
929
    *  @return string
930
    */
931
    public function getXoopsCodeAddStylesheet($style = 'style')
932
    {
933
        return "\$GLOBALS['xoTheme']->addStylesheet( \${$style}, null );\n";
934
    }
935
936
    /*
937
    *  @public function getXoopsCodeSecurityCheck
938
    *  @param null
939
    *  @return boolean
940
    */
941
    public function getXoopsCodeSecurityCheck()
942
    {
943
        return "\$GLOBALS['xoopsSecurity']->check()";
944
    }
945
946
    /*
947
    *  @public function getXoopsCodeSecurityErrors
948
    *  @param null
949
    *  @return string
950
    */
951
    public function getXoopsCodeSecurityErrors()
952
    {
953
        return "\$GLOBALS['xoopsSecurity']->getErrors()";
954
    }
955
956
    /**
957
     *  @public function getXoopsCodeHtmlErrors
958
     *
959
     *  @param $tableName
960
     *  @param $isParam
961
     *  @param $obj
962
     *
963
     *  @return string
964
     */
965
    public function getXoopsCodeHtmlErrors($tableName, $isParam = false, $obj = 'Obj')
966
    {
967
        $ret = '';
0 ignored issues
show
Unused Code introduced by
$ret is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
968
        if ($isParam) {
969
            $ret = "\${$tableName}{$obj}->getHtmlErrors()";
970
        } else {
971
            $ret = "\${$tableName}{$obj} =& \${$tableName}->getHtmlErrors();";
972
        }
973
974
        return $ret;
975
    }
976
977
    /**
978
     *  @public function getXoopsCodeObjHandlerCount
979
     *
980
     *  @param $left
981
     *  @param $tableName
982
     *  @param $obj
983
     *
984
     *  @return string
985
     */
986
    public function getXoopsCodeGetForm($left, $tableName, $obj = '')
987
    {
988
        $ucfTableName = ucfirst($tableName);
989
990
        return "\${$left} =& \${$tableName}{$obj}->getForm{$ucfTableName}();\n";
991
    }
992
993
    /**
994
     *  @public function getXoopsCodeGet
995
     *
996
     *  @param $tableName
997
     *  @param $var
998
     *  @param $obj
999
     *  @param $isHandler
1000
     *  @param $isParam
1001
     *
1002
     *  @return string
1003
     */
1004 View Code Duplication
    public function getXoopsCodeGet($tableName, $var, $obj = '', $isHandler = false, $isParam = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1005
    {
1006
        $handler = $isHandler === false ? '' : 'Handler';
1007
        if ($isParam) {
1008
            $ret = "\${$tableName}{$handler}->get(\${$var})";
1009
        } else {
1010
            $ret = "\${$tableName}{$obj} =& \${$tableName}{$handler}->get(\${$var});\n";
1011
        }
1012
1013
        return $ret;
1014
    }
1015
1016
    /**
1017
     *  @public function getXoopsCodeHandler
1018
     *
1019
     *  @param $tableName
1020
     *  @param $var
1021
     *  @param $obj
1022
     *  @param $isHandler
1023
     *
1024
     *  @return string
1025
     */
1026 View Code Duplication
    public function getXoopsCodeInsert($tableName, $var, $obj = '', $isHandler = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1027
    {
1028
        $handler = $isHandler === false ? '' : 'Handler';
1029
        if ($obj != '') {
1030
            $ret = "\${$tableName}{$handler}->insert(\${$var}{$obj})";
1031
        } else {
1032
            $ret = "\${$tableName}{$handler}->insert(\${$var})";
1033
        }
1034
1035
        return $ret;
1036
    }
1037
1038
    /**
1039
     *  @public function getXoopsCodeDelete
1040
     *
1041
     *  @param $tableName
1042
     *  @param $var
1043
     *  @param $obj
1044
     *  @param $isHandler
1045
     *
1046
     *  @return string
1047
     */
1048 View Code Duplication
    public function getXoopsCodeDelete($tableName, $var, $obj = '', $isHandler = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1049
    {
1050
        $handler = $isHandler === false ? '' : 'Handler';
1051
        if ($obj != '') {
1052
            $ret = "\${$tableName}{$handler}->delete(\${$var}{$obj})";
1053
        } else {
1054
            $ret = "\${$tableName}{$handler}->delete(\${$var})";
1055
        }
1056
1057
        return $ret;
1058
    }
1059
1060
    /**
1061
     *  @public function getXoopsCodeHandler
1062
     *
1063
     *  @param $tableName
1064
     *  @param $var
1065
     *
1066
     *  @return string
1067
     */
1068
    public function getXoopsCodeHandler($tableName, $var, $get = false, $insert = false, $delete = false, $obj = '')
1069
    {
1070
        if ($get) {
1071
            $ret = "\${$tableName}Handler->get(\${$var});";
1072
        } elseif ($insert && ($obj != '')) {
1073
            $ret = "\${$tableName}Handler->insert(\${$var}{$obj});";
1074
        } elseif ($delete && ($obj != '')) {
1075
            $ret = "\${$tableName}Handler->delete(\${$var}{$obj});";
1076
        }
1077
1078
        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...
1079
    }
1080
1081
    /*
1082
    *  @public function getXoopsCodeCaseDelete
1083
    *  @param $tableName
1084
    *  @param $language
1085
    *  @param $fieldId
1086
    *  @param $fieldMain
1087
    *  @return string
1088
    */
1089
    public function getXoopsCodeCaseDelete($language, $tableName, $fieldId, $fieldMain)
1090
    {
1091
        $ccFieldId = $this->tdmcfile->getCamelCase($fieldId, false, true);
1092
        $ret = $this->getXoopsCodeGet($tableName, $ccFieldId, 'Obj', true);
1093
1094
        $reqOk = "\$_REQUEST['ok']";
1095
        $isset = $this->phpcode->getPhpCodeIsset($reqOk);
1096
        $xoopsSecurityCheck = $this->getXoopsCodeSecurityCheck();
1097
        $xoopsSecurityErrors = $this->getXoopsCodeSecurityErrors();
1098
        $implode = $this->phpcode->getPhpCodeImplode(', ', $xoopsSecurityErrors);
1099
        $redirectHeaderErrors = $this->getXoopsCodeRedirectHeader($tableName, '', '3', $implode);
1100
1101
        $delete = $this->getXoopsCodeDelete($tableName, $tableName, 'Obj', true);
1102
        $condition = $this->phpcode->getPhpCodeConditions('!'.$xoopsSecurityCheck, '', '', $redirectHeaderErrors);
1103
1104
        $redirectHeaderLanguage = $this->getXoopsCodeRedirectHeader($tableName, '', '3', "{$language}FORM_DELETE_OK");
1105
        $htmlErrors = $this->getXoopsCodeHtmlErrors($tableName, true);
1106
        $internalElse = $this->getXoopsCodeTplAssign('error', $htmlErrors);
1107
        $condition .= $this->phpcode->getPhpCodeConditions($delete, '', '', $redirectHeaderLanguage, $internalElse);
1108
1109
        $mainElse = $this->getXoopsCodeXoopsConfirm($tableName, $language, $fieldId, $fieldMain);
1110
        $ret .= $this->phpcode->getPhpCodeConditions($isset, ' && ', "1 == {$reqOk}", $condition, $mainElse);
1111
1112
        return $ret;
1113
    }
1114
1115
    /*
1116
    *  @public function getTopicGetVar
1117
    *  @param $lpFieldName
1118
    *  @param $rpFieldName
1119
    *  @param $tableName
1120
    *  @param $tableNameTopic
1121
    *  @param $fieldNameParent
1122
    *  @param $fieldNameTopic
1123
    *  @return string
1124
    */
1125
    public function getTopicGetVar($lpFieldName, $rpFieldName, $tableName, $tableNameTopic, $fieldNameParent, $fieldNameTopic)
1126
    {
1127
        $ret = <<<EOT
1128
\t\t// Get Var {$fieldNameParent}
1129
\t\t\${$rpFieldName} =& \${$tableNameTopic}Handler->get(\${$tableName}All[\$i]->getVar('{$fieldNameParent}'));
1130
\t\t\${$lpFieldName}['{$rpFieldName}'] = \${$rpFieldName}->getVar('{$fieldNameTopic}');\n
1131
EOT;
1132
1133
        return $ret;
1134
    }
1135
1136
    /*
1137
    *  @public function getUploadImageGetVar
1138
    *  @param $lpFieldName
1139
    *  @param $rpFieldName
1140
    *  @param $tableName
1141
    *  @param $fieldName
1142
    *  @return string
1143
    */
1144
    public function getUploadImageGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
1145
    {
1146
        $ret = <<<EOT
1147
\t\t// Get Var {$fieldName}
1148
\t\t\${$fieldName} = \${$tableName}All[\$i]->getVar('{$fieldName}');
1149
\t\t\$upload_image = \${$fieldName} ? \${$fieldName} : 'blank.gif';
1150
\t\t\${$lpFieldName}['{$rpFieldName}'] = \$upload_image;\n
1151
EOT;
1152
1153
        return $ret;
1154
    }
1155
1156
    /*
1157
    *  @public function getXoopsCodeUpdate
1158
    *  @param $language
1159
    *  @param $tableName
1160
    *  @param $fieldId
1161
    *  @param $fieldName
1162
    *  @return string
1163
    */
1164
    public function getXoopsCodeUpdate($language, $tableName, $fieldId, $fieldName)
1165
    {
1166
        $ccFieldId = $this->tdmcfile->getCamelCase($fieldId, false, true);
1167
        $get = $this->getXoopsCodeGet($tableName, $ccFieldId, 'Obj', true);
0 ignored issues
show
Unused Code introduced by
$get is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1168
        $isset = $this->phpcode->getPhpCodeIsset($ccFieldId);
1169
        $get = $this->getXoopsCodeHandler($tableName, $fieldId, true);
1170
        $ret = $this->phpcode->getPhpCodeConditions($isset, '', '', $get);
1171
        $ret .= $this->getXoopsCodeSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
1172
        $handlerInsert = $this->$this->getXoopsCodeHandler($tableName, $tableName, false, true, false, 'Obj');
1173
        $redirect = $this->getXoopsCodeRedirectHeader($tableName, '?op=list', 2, "{$language}FORM_UPDATE_OK");
1174
        $ret .= $this->phpcode->getPhpCodeConditions($handlerInsert, '', '', $redirect);
1175
1176
        $ret .= $this->getXoopsCodeTplAssign('error', "\${$tableName}Obj->getHtmlErrors()");
1177
1178
        return $this->phpcode->getPhpCodeCaseSwitch('update', $ret);
1179
    }
1180
1181
    /**
1182
     *  @public function getXoopsCodeSaveFieldId
1183
     *
1184
     *  @param $fields
1185
     *
1186
     *  @return string
1187
     */
1188 View Code Duplication
    public function getXoopsCodeSaveFieldId($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...
1189
    {
1190
        foreach (array_keys($fields) as $f) {
1191
            if (0 == $f) {
1192
                $fieldId = $fields[$f]->getVar('field_name');
1193
            }
1194
        }
1195
1196
        return $fieldId;
0 ignored issues
show
Bug introduced by
The variable $fieldId 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...
1197
    }
1198
1199
    /**
1200
     *  @public function getXoopsCodeSaveFieldMain
1201
     *
1202
     *  @param $fields
1203
     *
1204
     *  @return string
1205
     */
1206 View Code Duplication
    public function getXoopsCodeSaveFieldMain($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...
1207
    {
1208
        foreach (array_keys($fields) as $f) {
1209
            if (1 == $fields[$f]->getVar('field_main')) {
1210
                $fieldMain = $fields[$f]->getVar('field_name');
1211
            }
1212
        }
1213
1214
        return $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...
1215
    }
1216
1217
    /**
1218
     *  @public function getXoopsCodeSaveElements
1219
     *
1220
     *  @param $moduleDirname
1221
     *  @param $tableName
1222
     *  @param $fields
1223
     *
1224
     *  @return string
1225
     */
1226 View Code Duplication
    public function getXoopsCodeSaveElements($moduleDirname, $tableName, $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...
1227
    {
1228
        $ret = '';
1229
        foreach (array_keys($fields) as $f) {
1230
            $fieldName = $fields[$f]->getVar('field_name');
1231
            $fieldElement = $fields[$f]->getVar('field_element');
1232
            if (1 == $fields[$f]->getVar('field_main')) {
1233
                $fieldMain = $fieldName;
1234
            }
1235
            if ((5 == $fieldElement) || (6 == $fieldElement)) {
1236
                $ret .= $this->getXoopsCodeCheckBoxOrRadioYNSetVar($tableName, $fieldName);
1237
            } elseif (13 == $fieldElement) {
1238
                $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...
1239
            } elseif (14 == $fieldElement) {
1240
                $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...
1241
            } elseif (15 == $fieldElement) {
1242
                $ret .= $this->getXoopsCodeTextDateSelectSetVar($tableName, $fieldName);
1243
            } else {
1244
                $ret .= $this->getXoopsCodeSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
1245
            }
1246
        }
1247
1248
        return $ret;
1249
    }
1250
1251
    /*
1252
    *  @public function getXoopsCodePageNav
1253
    *  @param $tableName
1254
    *
1255
    *  @return string
1256
    */
1257
    public function getXoopsCodePageNav($tableName)
1258
    {
1259
        $condition = $this->phpcode->getPhpCodeCommentLine('Display Navigation');
1260
        $condition .= $this->phpcode->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/pagenav', true);
1261
        $condition .= "\$pagenav = new XoopsPageNav(\${$tableName}Count, \$limit, \$start, 'start', 'op=list&limit=' . \$limit);\n";
1262
        $condition .= $this->getXoopsCodeTplAssign('pagenav', '$pagenav->renderNav(4)');
1263
        $ret = $this->phpcode->getPhpCodeConditions("\${$tableName}Count", ' > ', '$limit', $condition, false, "\t\t");
1264
1265
        return $ret;
1266
    }
1267
}
1268