Passed
Branch master (31be8b)
by Gino
03:04
created

getXoopsCodePermissionsHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
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
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 $ref
97
    *
98
    *  @return string
99
    */
100
    public function getXoopsCodeEqualsOperator($left, $right, $ref = false)
101
    {
102
        if (false === $ref) {
103
            $ret = "{$left}= {$right};\n";
104
        } else {
105
            $ret = "{$left}=& {$right};\n";
106
        }
107
108
        return $ret;
109
    }
110
111
    /*
112
    *  @public function getXoopsCodeCPHeader
113
    *  @param null
114
    *  @return string
115
    */
116
    public function getXoopsCodeCPHeader()
117
    {
118
        return "xoops_cp_header();\n";
119
    }
120
121
    /*
122
    *  @public function getXoopsCodeCPFooter
123
    *  @param null
124
    *  @return string
125
    */
126
    public function getXoopsCodeCPFooter()
127
    {
128
        return "xoops_cp_footer();\n";
129
    }
130
131
    /**
132
     *  @public function getXoopsCodeLoad
133
     *
134
     *  @param $var
135
     *
136
     *  @return string
137
     */
138
    public function getXoopsCodeLoad($var = '')
139
    {
140
        return "xoops_load('{$var}');\n";
141
    }
142
143
    /**
144
     *  @public function getXoopsCodeLoadLanguage
145
     *
146
     *  @param $lang
147
     *
148
     *  @return string
149
     */
150
    public function getXoopsCodeLoadLanguage($lang)
151
    {
152
        return "xoops_loadLanguage('{$lang}');\n";
153
    }
154
155
    /*
156
    *  @public function getXoopsCodeAnchorFunction
157
    *  @param $anchor
158
    *  @param $name
159
    *  @param $vars
160
    *
161
    *  @return string
162
    */
163
    public function getXoopsCodeAnchorFunction($anchor, $name, $vars)
164
    {
165
        return "\${$anchor}->{$name}({$vars})";
166
    }
167
168
    /*
169
    *  @public function getXoopsCodeSetVar
170
    *  @param $tableName
171
    *  @param $fieldName
172
    *  @param $var
173
    *  @return string
174
    */
175
    public function getXoopsCodeSetVar($tableName, $fieldName, $var)
176
    {
177
        return "\${$tableName}Obj->setVar('{$fieldName}', {$var});\n";
178
    }
179
180
    /*
181
    *  @public function getXoopsCodeGetVar
182
    *  @param $varLeft
183
    *  @param $handle
184
    *  @param $var
185
    *  @param $isParam
186
    *
187
    *  @return string
188
    */
189
    public function getXoopsCodeGetVar($varLeft = '', $handle = '', $var = '', $isParam = false)
190
    {
191
        if (!$isParam) {
192
            $ret = "\${$varLeft} = \${$handle}->getVar('{$var}');\n";
193
        } else {
194
            $ret = "\${$handle}->getVar('{$var}')";
195
        }
196
197
        return $ret;
198
    }
199
200
    /*
201
    *  @public function getXoopsCodeGroupPermForm
202
    *  @param $varLeft
203
    *  @param $formTitle
204
    *  @param $moduleId
205
    *  @param $permName
206
    *  @param $permDesc
207
    *  @param $filename
208
    *
209
    *  @return string
210
    */
211
    public function getXoopsCodeGroupPermForm($varLeft = '', $formTitle = '', $moduleId = '', $permName = '', $permDesc = '', $filename = '')
212
    {
213
        return "\${$varLeft} = new XoopsGroupPermForm({$formTitle}, {$moduleId}, {$permName}, {$permDesc}, {$filename});\n";
214
    }
215
216
    /*
217
    *  @public function getXoopsCodeAddItem
218
    *  @param $varLeft
219
    *  @param $paramLeft
220
    *  @param $paramRight
221
    *
222
    *  @return string
223
    */
224
    public function getXoopsCodeAddItem($varLeft = '', $paramLeft = '', $paramRight = '')
225
    {
226
        return "\${$varLeft}->addItem({$paramLeft}, {$paramRight});\n";
227
    }
228
229
    /*
230
    *  @public function getXoopsCodeTextDateSelectSetVar
231
    *  @param $tableName
232
    *  @param $fieldName
233
    *  @return string
234
    */
235
    public function getXoopsCodeTextDateSelectSetVar($tableName, $fieldName)
236
    {
237
        return $this->getXoopsCodeSetVar($tableName, $fieldName, "strtotime(\$_POST['{$fieldName}'])");
238
    }
239
240
    /*
241
    *  @public function getXoopsCodeCheckBoxOrRadioYNSetVar
242
    *  @param $tableName
243
    *  @param $fieldName
244
    *  @return string
245
    */
246
    public function getXoopsCodeCheckBoxOrRadioYNSetVar($tableName, $fieldName)
247
    {
248
        return $this->getXoopsCodeSetVar($tableName, $fieldName, "((1 == \$_REQUEST['{$fieldName}']) ? '1' : '0')");
249
    }
250
251
    /*
252
    *  @public function getXoopsCodeXoopsMediaUploader
253
    *  @param $var
254
    *  @param $dirPath
255
    *  @param $tableName
256
    *  @param $moduleDirname
257
    *  @return string
258
    */
259
    public function getXoopsCodeXoopsMediaUploader($var = '', $dirPath, $tableName, $moduleDirname)
260
    {
261
        $mimetypes = $this->getXoopsCodeGetConfig($moduleDirname, 'mimetypes');
262
        $maxsize = $this->getXoopsCodeGetConfig($moduleDirname, 'maxsize');
263
264
        return "\${$var} = new XoopsMediaUploader({$dirPath} . '/{$tableName}', {$mimetypes}, {$maxsize}, null, null);\n";
265
    }
266
267
    /*
268
    *  @public function getXoopsCodeXoopsCaptcha
269
    *  @param $var
270
    *  @param $instance
271
    *
272
    *  @return string
273
    */
274
    public function getXoopsCodeGetInstance($var = '', $instance = '')
275
    {
276
        return "\${$var} = {$instance}::getInstance();\n";
277
    }
278
279
    /*
280
    *  @public function getXoopsCodeXoopsCaptcha
281
    *  @param null
282
    *  @return string
283
    */
284
    public function getXoopsCodeXoopsCaptcha()
285
    {
286
        return "\$xoopsCaptcha = XoopsCaptcha::getInstance();\n";
287
    }
288
289
    /*
290
    *  @public function getXoopsCodeGetConfig
291
    *  @param $moduleDirname
292
    *  @param $name
293
    *  @return string
294
    */
295
    public function getXoopsCodeGetConfig($moduleDirname, $name)
296
    {
297
        return "\${$moduleDirname}->getConfig('{$name}')";
298
    }
299
300
    /*
301
    *  @public function getXoopsCodeIdGetVar
302
    *  @param $lpFieldName
303
    *  @return string
304
    */
305
    public function getXoopsCodeIdGetVar($lpFieldName)
306
    {
307
        return "\${$lpFieldName}['id'] = \$i;\n";
308
    }
309
310
    /*
311
    *  @public function getXoopsCodeGetVarAll
312
    *  @param $lpFieldName
313
    *  @param $rpFieldName
314
    *  @param $tableName
315
    *  @param $fieldName
316
    *  @return string
317
    */
318
    public function getXoopsCodeGetVarAll($lpFieldName, $rpFieldName, $tableName, $fieldName)
319
    {
320
        return "\${$lpFieldName}['{$rpFieldName}'] = \${$tableName}All[\$i]->getVar('{$fieldName}');\n";
321
    }
322
323
    /*
324
    *  @public function getXoopsHandlerInstance
325
    *  @param $moduleDirname
326
    *  
327
    *  @return string
328
    */
329
    public function getXoopsHandlerInstance($moduleDirname)
330
    {
331
        $ucfModuleDirname = ucfirst($moduleDirname);
332
        $ret = "// Get instance of module\n";
333
        $ret .= "\${$moduleDirname} = {$ucfModuleDirname}Helper::getInstance();\n";
334
335
        return $ret;
336
    }
337
338
    /*
339
    *  @public function getXoopsHandlerLine
340
    *  @param $moduleDirname
341
    *  @param $tableName
342
    *  @return string
343
    */
344
    public function getXoopsHandlerLine($moduleDirname, $tableName)
345
    {
346
        return "\${$tableName}Handler =& \${$moduleDirname}->getHandler('{$tableName}');\n";
347
    }
348
349
    /*
350
    *  @public function getXoopsSimpleForm
351
    *  @param $left
352
    *  @param $element
353
    *  @param $elementsContent
354
    *  @param $caption
355
    *  @param $var
356
    *  @param $filename
357
    *  @param $type
358
    *  
359
    *  @return string
360
    */
361
    public function getXoopsSimpleForm($left = '', $element = '', $elementsContent = '', $caption = '', $var = '', $filename = '', $type = 'post')
362
    {
363
        $ret = "\${$left} = new XoopsSimpleForm({$caption}, '{$var}', '{$filename}.php', '{$type}');\n";
364
        if (!empty($elementsContent)) {
365
            $ret .= "{$elementsContent}";
366
        }
367
        $ret .= "\${$left}->addElement(\${$element});\n";
368
        $ret .= "\${$left}->display();\n";
369
370
        return $ret;
371
    }
372
373
    /*
374
    *  @public function getXoopsFormSelect
375
    *  @param $varSelect
376
    *  @param $caption
377
    *  @param $var
378
    *  @param $options
379
    *  @param $setExtra
380
    *  
381
    *  @return string
382
    */
383
    public function getXoopsFormSelect($varSelect = '', $caption = '', $var = '', $options = array(), $setExtra = true)
384
    {
385
        $ret = "\${$varSelect} = new XoopsFormSelect({$caption}, '{$var}', \${$var});\n";
386
        if (false !== $setExtra) {
387
            $ret .= "\${$varSelect}->setExtra('{$setExtra}');\n";
388
        }
389
        foreach ($options as $key => $value) {
390
            $ret .= "\${$varSelect}->addOption('{$key}', {$value});\n";
391
        }
392
393
        return $ret;
394
    }
395
396
    /*
397
     *  @public function getXoopsCodeUnameFromId
398
     *  @param $left
399
     *  @param $tableName
400
     *
401
     * @return string
402
     */
403
    public function getXoopsCodeUnameFromId($left, $value)
404
    {
405
        return "\${$left} = XoopsUser::getUnameFromId({$value});\n";
406
    }
407
408
    /*
409
    *  @public function getXoopsCodeFormatTimeStamp
410
    *  @param $lpFieldName
411
    *  @param $rpFieldName
412
    *  @param $tableName
413
    *  @param $fieldName
414
    *  @return string
415
    */
416
    public function getXoopsCodeFormatTimeStamp($left, $value, $format = 's')
417
    {
418
        return "\${$left} = formatTimeStamp({$value}, '{$format}');\n";
419
    }
420
421
    /*
422
    *  @public function getXoopsCodeTopicGetVar
423
    *  @param $lpFieldName
424
    *  @param $rpFieldName
425
    *  @param $tableName
426
    *  @param $tableNameTopic
427
    *  @param $fieldNameParent
428
    *  @param $fieldNameTopic
429
    *  @return string
430
    */
431
    public function getXoopsCodeTopicGetVar($lpFieldName, $rpFieldName, $tableName, $tableNameTopic, $fieldNameParent, $fieldNameTopic)
432
    {
433
        $ret = <<<EOT
434
\t\t\t\t// Get Var {$fieldNameParent}
435
\t\t\t\t\${$rpFieldName} =& \${$tableNameTopic}Handler->get(\${$tableName}All[\$i]->getVar('{$fieldNameParent}'));
436
\t\t\t\t\${$lpFieldName}['{$rpFieldName}'] = \${$rpFieldName}->getVar('{$fieldNameTopic}');\n
437
EOT;
438
439
        return $ret;
440
    }
441
442
    /*
443
    *  @public function getXoopsCodeParentTopicGetVar
444
    *  @param $moduleDirname
445
    *  @param $lpFieldName
446
    *  @param $rpFieldName
447
    *  @param $tableName
448
    *  @param $tableSoleNameTopic
449
    *  @param $tableNameTopic
450
    *  @param $fieldNameParent
451
    *  @return string
452
    */
453
    public function getXoopsCodeParentTopicGetVar($moduleDirname, $lpFieldName, $rpFieldName, $tableName, $tableSoleNameTopic, $tableNameTopic, $fieldNameParent)
454
    {
455
        $ret = <<<EOT
456
\t\t\t\tif(!isset(\${$tableNameTopic}Handler)) {
457
\t\t\t\t\t// Get {$tableNameTopic} Handler
458
\t\t\t\t\t\${$tableNameTopic}Handler =& \${$moduleDirname}->getHandler('{$tableNameTopic}');
459
\t\t\t\t}
460
\t\t\t\t// Get Var {$fieldNameParent}
461
\t\t\t\t\${$lpFieldName}['{$rpFieldName}'] = \${$tableNameTopic}Handler->get{$tableSoleNameTopic}FromId(\${$tableName}All[\$i]->getVar('{$fieldNameParent}'));\n
462
EOT;
463
464
        return $ret;
465
    }
466
467
    /*
468
    *  @public function getXoopsCodeUploadImageGetVar
469
    *  @param $lpFieldName
470
    *  @param $rpFieldName
471
    *  @param $tableName
472
    *  @param $fieldName
473
    *  @return string
474
    */
475
    public function getXoopsCodeUploadImageGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
476
    {
477
        $ret = <<<EOT
478
\t\t\t\t// Get Var {$fieldName}
479
\t\t\t\t\${$fieldName} = \${$tableName}All[\$i]->getVar('{$fieldName}');
480
\t\t\t\t\${$lpFieldName}['{$rpFieldName}'] = \${$fieldName} ? \${$fieldName} : 'blank.gif';\n
481
EOT;
482
483
        return $ret;
484
    }
485
    /*
486
    *  @public function getXoopsCodeUrlFileGetVar
487
    *  @param $lpFieldName
488
    *  @param $rpFieldName
489
    *  @param $tableName
490
    *  @param $fieldName
491
    *  @return string
492
    */
493
    public function getXoopsCodeUrlFileGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
494
    {
495
        return $this->getXoopsCodeGetVarAll($lpFieldName, $rpFieldName, $tableName, $fieldName);
496
    }
497
    /*
498
    *  @public function getXoopsCodeTextAreaGetVar
499
    *  @param $lpFieldName
500
    *  @param $rpFieldName
501
    *  @param $tableName
502
    *  @param $fieldName
503
    *  @return string
504
    */
505
    public function getXoopsCodeTextAreaGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
506
    {
507
        return "\${$lpFieldName}['{$rpFieldName}'] = strip_tags(\${$tableName}All[\$i]->getVar('{$fieldName}'));\n";
508
    }
509
510
    /*
511
    *  @public function getXoopsCodeSelectUserGetVar
512
    *  @param $lpFieldName
513
    *  @param $rpFieldName
514
    *  @param $tableName
515
    *  @param $fieldName
516
    * @return string
517
    */
518
    public function getXoopsCodeSelectUserGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
519
    {
520
        return "\${$lpFieldName}['{$rpFieldName}'] = XoopsUser::getUnameFromId(\${$tableName}All[\$i]->getVar('{$fieldName}'), 's');\n";
521
    }
522
523
    /*
524
    *  @public function getXoopsCodeTextDateSelectGetVar
525
    *  @param $lpFieldName
526
    *  @param $rpFieldName
527
    *  @param $tableName
528
    *  @param $fieldName
529
    *  @return string
530
    */
531
    public function getXoopsCodeTextDateSelectGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
532
    {
533
        return "\${$lpFieldName}['{$rpFieldName}'] = formatTimeStamp(\${$tableName}All[\$i]->getVar('{$fieldName}'), 's');\n";
534
    }
535
536
    /*
537
    *  @public function getXoopsCodeUserHeader
538
    *  @param $moduleDirname
539
    *  @param $tableName
540
    *  @return string
541
    */
542
    public function getXoopsCodeXoopsOptionTemplateMain($moduleDirname, $tableName)
543
    {
544
        return "\$GLOBALS['xoopsOption']['template_main'] = '{$moduleDirname}_{$tableName}.tpl';\n";
545
    }
546
547
    /*
548
    *  @public function getXoopsCodeUserHeader
549
    *  @param $moduleDirname
550
    *  @param $tableName
551
    *  @return string
552
    */
553
    public function getXoopsCodeUserHeader($moduleDirname, $tableName)
554
    {
555
        $ret = $this->phpcode->getPhpCodeIncludeDir('__DIR__', 'header');
556
        $ret .= $this->getXoopsCodeXoopsOptionTemplateMain($moduleDirname, $tableName);
557
        $ret .= $this->phpcode->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'header', true);
558
559
        return $ret;
560
    }
561
562
    /*
563
    *  @public function getXoopsCodePermissionsHeader
564
    *  @param null
565
    */
566
    /**
567
     * @return string
568
     */
569
    public function getXoopsCodePermissionsHeader()
570
    {
571
        $ret = $this->phpcode->getPhpCodeCommentLine('Permission');
572
        $ret .= $this->phpcode->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/xoopsform/grouppermform', true);
573
        $ret .= $this->getXoopsCodeEqualsOperator('$gperm_handler', "xoops_gethandler('groupperm')", true);
574
        $groups = $this->getXoopsCodeEqualsOperator('$groups', '$xoopsUser->getGroups()');
575
        $elseGroups = $this->getXoopsCodeEqualsOperator('$groups', 'XOOPS_GROUP_ANONYMOUS');
576
        $ret .= $this->phpcode->getPhpCodeConditions('is_object($xoopsUser)', '', $type = '', $groups, $elseGroups);
577
578
        return $ret;
579
    }
580
581
    /**
582
     *  @public function getXoopsCodeGetFieldId
583
     *
584
     *  @param $fields
585
     *
586
     *  @return string
587
     */
588
    public function getXoopsCodeGetFieldId($fields)
589
    {
590
        $fieldId = 'id';
591
        foreach (array_keys($fields) as $f) {
592
            $fieldName = $fields[$f]->getVar('field_name');
593
            if (0 == $f) {
594
                $fieldId = $fieldName;
595
            }
596
        }
597
598
        return $fieldId;
599
    }
600
601
    /**
602
     *  @public function getXoopsCodeGetFieldName
603
     *
604
     *  @param $fields
605
     *
606
     *  @return string
607
     */
608
    public function getXoopsCodeGetFieldName($fields)
609
    {
610
        foreach (array_keys($fields) as $f) {
611
            $fieldName = $fields[$f]->getVar('field_name');
612
        }
613
614
        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...
615
    }
616
617
    /**
618
     *  @public function getXoopsCodeGetFieldParentId
619
     *
620
     *  @param $fields
621
     *
622
     *  @return string
623
     */
624
    public function getXoopsCodeGetFieldParentId($fields)
625
    {
626
        $fieldPid = 'pid';
627
        foreach (array_keys($fields) as $f) {
628
            $fieldName = $fields[$f]->getVar('field_name');
629
            if (1 == $fields[$f]->getVar('field_parent')) {
630
                $fieldPid = $fieldName;
631
            }
632
        }
633
634
        return $fieldPid;
635
    }
636
637
    /**
638
     *  @public function getXoopsCodeUserSaveElements
639
     *
640
     *  @param $moduleDirname
641
     *  @param $tableName
642
     *  @param $fields
643
     *
644
     *  @return string
645
     */
646
    public function getXoopsCodeUserSaveElements($moduleDirname, $tableName, $fields)
647
    {
648
        $ret = '';
649
        foreach (array_keys($fields) as $f) {
650
            $fieldName = $fields[$f]->getVar('field_name');
651
            $fieldElement = $fields[$f]->getVar('field_element');
652
            if (1 == $fields[$f]->getVar('field_main')) {
653
                $fieldMain = $fieldName;
654
            }
655
            if ((5 == $fieldElement) || (6 == $fieldElement)) {
656
                $ret .= $this->getXoopsCodeCheckBoxOrRadioYNSetVar($tableName, $fieldName);
657
            } elseif (13 == $fieldElement) {
658
                $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...
659
            } elseif (14 == $fieldElement) {
660
                $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...
661
            } elseif (15 == $fieldElement) {
662
                $ret .= $this->getXoopsCodeTextDateSelectSetVar($tableName, $fieldName);
663
            } else {
664
                $ret .= $this->getXoopsCodeSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
665
            }
666
        }
667
668
        return $ret;
669
    }
670
671
    /*
672
    *  @public function getXoopsCodeXoopsRequest
673
    *  @param $left
674
    *  @param $var1
675
    *  @param $var2
676
    *  @param $type
677
    *  @param $metod
678
    *  @return string
679
    */
680
    public function getXoopsCodeXoopsRequest($left = '', $var1 = '', $var2 = '', $type = 'String', $metod = false)
681
    {
682
        $ret = '';
683
        $intVars = ($var2 != '') ? "'{$var1}', {$var2}" : "'{$var1}'";
684
        if ($type == 'String') {
685
            $ret .= "\${$left} = XoopsRequest::getString('{$var1}', '{$var2}');\n";
686
        } elseif ($type == 'Int') {
687
            $ret .= "\${$left} = XoopsRequest::getInt({$intVars});\n";
688
        } elseif ($type == 'Int' && $metod !== false) {
689
            $ret .= "\${$left} = XoopsRequest::getInt({$intVars}, '{$metod}');\n";
690
        }
691
692
        return $ret;
693
    }
694
695
    /**
696
     *  @public function getXoopsCodeTplAssign     
697
     *
698
     *  @param $tplString
699
     *  @param $phpRender
700
     *  @param $leftIsString 
701
     *
702
     *  @return string
703
     */
704
    public function getXoopsCodeTplAssign($tplString, $phpRender, $leftIsString = true)
705
    {
706
        $assign = "\$GLOBALS['xoopsTpl']->assign(";
707
        if ($leftIsString === false) {
708
            $ret = $assign."{$tplString}, {$phpRender});\n";
709
        } else {
710
            $ret = $assign."'{$tplString}', {$phpRender});\n";
711
        }
712
713
        return $ret;
714
    }
715
716
    /**
717
     *  @public function getXoopsCodeXoopsTplAppend
718
     *
719
     *  @param $tplString
720
     *  @param $phpRender
721
     *
722
     *  @return string
723
     */
724
    public function getXoopsCodeXoopsTplAppend($tplString, $phpRender)
725
    {
726
        return "\$GLOBALS['xoopsTpl']->append('{$tplString}', {$phpRender});\n";
727
    }
728
729
    /**
730
     *  @public function getXoopsCodeXoopsTplAppendByRef
731
     *
732
     *  @param $tplString
733
     *  @param $phpRender
734
     *
735
     *  @return string
736
     */
737
    public function getXoopsCodeXoopsTplAppendByRef($tplString, $phpRender)
738
    {
739
        return "\$GLOBALS['xoopsTpl']->appendByRef('{$tplString}', {$phpRender});\n";
740
    }
741
742
    /**
743
     *  @public function getXoopsCodePath
744
     *
745
     *  @param $directory
746
     *  @param $filename
747
     *  @param $isParam
748
     *
749
     *  @return string
750
     */
751
    public function getXoopsCodePath($directory, $filename, $isParam = false)
752
    {
753
        if (!$isParam) {
754
            $ret = "\$GLOBALS['xoops']->path({$directory}.'/{$filename}.php');\n";
755
        } else {
756
            $ret = "\$GLOBALS['xoops']->path({$directory}.'/{$filename}.php')";
757
        }
758
759
        return $ret;
760
    }
761
762
    /**
763
     *  @public function getXoopsCodeTplDisplay
764
     *
765
     *  @param $displayTpl
766
     *
767
     *  @return string
768
     */
769
    public function getXoopsCodeTplDisplay($displayTpl = '{$templateMain}')
770
    {
771
        return "\$GLOBALS['xoopsTpl']->display(\"db:{$displayTpl}\");\n";
772
    }
773
774
    /**
775
     *  @public function getXoopsCodeGetInfo
776
     *
777
     *  @param $left
778
     *  @param $string
779
     *  @param $isParam
780
     *
781
     *  @return string
782
     */
783
    public function getXoopsCodeGetInfo($left = '', $string, $isParam = false)
784
    {
785
        if (!$isParam) {
786
            $ret = "\${$left} = \$GLOBALS['xoopsModule']->getInfo('{$string}');\n";
787
        } else {
788
            $ret = "\$GLOBALS['xoopsModule']->getInfo('{$string}')";
789
        }
790
791
        return $ret;
792
    }
793
794
    /**
795
     *  @public function getXoopsCodeCheckRight
796
     *
797
     *  @param $anchor
798
     *  @param $permString
799
     *  @param $var
800
     *  @param $groups
801
     *  @param $mid
802
     *  @param $isParam
803
     *
804
     *  @return string
805
     */
806
    public function getXoopsCodeCheckRight($anchor, $permString = '', $var = '', $groups = '', $mid = '', $isParam = false)
807
    {
808
        if (!$isParam) {
809
            $ret = "{$anchor}->checkRight('{$permString}', {$var}, {$groups}, {$mid});\n";
810
        } else {
811
            $ret = "{$anchor}->checkRight('{$permString}', {$var}, {$groups}, {$mid})";
812
        }
813
814
        return $ret;
815
    }
816
817
    /**
818
     *  @public function getXoopsCodeObjHandlerCreate
819
     *
820
     *  @param $tableName
821
     *
822
     *  @return string
823
     */
824
    public function getXoopsCodeObjHandlerCreate($tableName)
825
    {
826
        return "\${$tableName}Obj =& \${$tableName}Handler->create();\n";
827
    }
828
829
    /**
830
     *  @public function getXoopsCodeObjHandlerCount
831
     *
832
     *  @param $tableName
833
     *
834
     *  @return string
835
     */
836
    public function getXoopsCodeObjHandlerCount($tableName)
837
    {
838
        $ucfTableName = ucfirst($tableName);
839
        $ret = "\${$tableName}Count = \${$tableName}Handler->getCount{$ucfTableName}();\n";
840
841
        return $ret;
842
    }
843
844
    /**
845
     *  @public function getXoopsCodeObjHandlerAll
846
     *
847
     *  @param $tableName
848
     *  @param $fieldMain
849
     *  @param $start
850
     *  @param $limit
851
     *
852
     *  @return string
853
     */
854
    public function getXoopsCodeObjHandlerAll($tableName, $fieldMain = '', $start = '0', $limit = '0')
855
    {
856
        $ucfTableName = ucfirst($tableName);
857
        $startLimit = ($limit != '0') ? "{$start}, {$limit}" : '0';
858
        $params = ($fieldMain != '') ? "{$startLimit}, '{$fieldMain}'" : $startLimit;
859
        $ret = "\${$tableName}All = \${$tableName}Handler->getAll{$ucfTableName}({$params});\n";
860
861
        return $ret;
862
    }
863
864
    /**
865
     *  @public function getXoopsCodeGetValues
866
     *
867
     *  @param $tableName
868
     *  @param $tableSoleName
869
     *
870
     *  @return string
871
     */
872
    public function getXoopsCodeGetValues($tableName, $tableSoleName, $index = 'i', $noArray = false)
873
    {
874
        $ucfTableName = ucfirst($tableName);
875
        if (!$noArray) {
876
            $ret = "\${$tableSoleName} = \${$tableName}All[\${$index}]->getValues{$ucfTableName}();\n";
877
        } else {
878
            $ret = "\${$tableSoleName} = \${$tableName}->getValues{$ucfTableName}();\n";
879
        }
880
881
        return $ret;
882
    }
883
884
    /**
885
     *  @public function getXoopsCodeObjectTree
886
     *
887
     *  @param $var
888
     *  @param $tableName
889
     *  @param $fieldId
890
     *  @param $fieldParent
891
     *
892
     *  @return string
893
     */
894
    public function getXoopsCodeObjectTree($var = 'mytree', $tableName, $fieldId, $fieldParent)
895
    {
896
        $ret = "\${$var} = new XoopsObjectTree(\${$tableName}All, '{$fieldId}', '{$fieldParent}');\n";
897
898
        return $ret;
899
    }
900
901
    /**
902
     *  @public function getXoopsCodeSetVarsObjects
903
     *
904
     *  @param $moduleDirname
905
     *  @param $tableName
906
     *  @param $fields
907
     *
908
     *  @return string
909
     */
910
    public function getXoopsCodeSetVarsObjects($moduleDirname, $tableName, $fields)
911
    {
912
        $ret = '';
913
        foreach (array_keys($fields) as $f) {
914
            $fieldName = $fields[$f]->getVar('field_name');
915
            $fieldElement = $fields[$f]->getVar('field_element');
916
            if (1 == $fields[$f]->getVar('field_main')) {
917
                $fieldMain = $fieldName;
918
            }
919
            if ($f > 0) { // If we want to hide field id
920
                switch ($fieldElement) {
921
                    case 5:
922
                    case 6:
923
                        $ret .= $this->getXoopsCodeCheckBoxOrRadioYNSetVar($tableName, $fieldName);
924
                        break;
925
                    case 11:
926
                        $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...
927
                        break;
928
                    case 12:
929
                        $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...
930
                        break;
931
                    case 13:
932
                        $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...
933
                        break;
934
                    case 14:
935
                        $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...
936
                        break;
937
                    case 15:
938
                        $ret .= $this->getXoopsCodeTextDateSelectSetVar($tableName, $fieldName);
939
                        break;
940
                    default:
941
                        $ret .= $this->getXoopsCodeSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
942
                        break;
943
                }
944
            }
945
        }
946
947
        return $ret;
948
    }
949
950
    /**
951
     *  @public function getXoopsCodeSecurity
952
     *
953
     *  @param $tableName
954
     *
955
     *  @return string
956
     */
957
    public function getXoopsCodeSecurity($tableName)
958
    {
959
        $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...
960
        $implode = $this->phpcode->getPhpCodeImplode(',', $securityError);
961
        $content = $this->getXoopsCodeRedirectHeader($tableName.'.php', '', 3, $implode);
962
        $securityCheck = $this->getXoopsCodeSecurityCheck();
963
964
        return $this->phpcode->getPhpCodeConditions('!'.$securityCheck, '', '', $content);
965
    }
966
967
    /*
968
    *  @public function getXoopsCodeInsertData
969
    *  @param $tableName
970
    *  @param $language
971
    *  @return string
972
    */
973
    public function getXoopsCodeInsertData($tableName, $language)
974
    {
975
        $content = $this->getXoopsCodeRedirectHeader($tableName.'.php', '?op=list', 2, "{$language}FORM_OK");
976
        $handlerInsert = $this->getXoopsCodeHandler($tableName, $tableName, false, true, false, 'Obj');
977
978
        return $this->phpcode->getPhpCodeConditions($handlerInsert, '', '', $content);
979
    }
980
981
    /*
982
    *  @public function getXoopsCodeRedirectHeader
983
    *  @param $tableName
984
    *  @param $options
985
    *  @param $numb
986
    *  @param $var
987
    *  @param $isString
988
    *
989
    *  @return string
990
    */
991
    public function getXoopsCodeRedirectHeader($tableName, $options = '', $numb = '2', $var, $isString = true)
992
    {
993
        if (!$isString) {
994
            $ret = "redirect_header({$tableName}, {$numb}, {$var});\n";
995
        } else {
996
            $ret = "redirect_header('{$tableName}{$options}', {$numb}, {$var});\n";
997
        }
998
999
        return $ret;
1000
    }
1001
1002
    /*
1003
    *  @public function getXoopsCodeXoopsConfirm
1004
    *  @param $tableName
1005
    *  @param $language
1006
    *  @param $fieldId
1007
    *  @param $fieldMain    
1008
    *  @param $options
1009
    *
1010
    *  @return string
1011
    */
1012
    public function getXoopsCodeXoopsConfirm($tableName, $language, $fieldId, $fieldMain, $options = 'delete')
1013
    {
1014
        $stuOptions = strtoupper($options);
1015
        $ccFieldId = $this->tdmcfile->getCamelCase($fieldId, false, true);
1016
        $ret = "xoops_confirm(array('ok' => 1, '{$fieldId}' => \${$ccFieldId}, 'op' => {$options}), \$_SERVER['REQUEST_URI'], sprintf({$language}FORM_SURE_{$stuOptions}, \${$tableName}Obj->getVar('{$fieldMain}')));\n";
1017
1018
        return $ret;
1019
    }
1020
1021
    /*
1022
    *  @public function getXoopsCodeAddStylesheet
1023
    *  @param $style
1024
    *  
1025
    *  @return string
1026
    */
1027
    public function getXoopsCodeAddStylesheet($style = 'style')
1028
    {
1029
        return "\$GLOBALS['xoTheme']->addStylesheet( \${$style}, null );\n";
1030
    }
1031
1032
    /*
1033
    *  @public function getXoopsCodeSecurityCheck
1034
    *  @param $denial
1035
    *  @return boolean
1036
    */
1037
    public function getXoopsCodeSecurityCheck($denial = '')
1038
    {
1039
        return "{$denial}\$GLOBALS['xoopsSecurity']->check()";
1040
    }
1041
1042
    /*
1043
    *  @public function getXoopsCodeSecurityErrors
1044
    *  @param null
1045
    *  @return string
1046
    */
1047
    public function getXoopsCodeSecurityErrors()
1048
    {
1049
        return "\$GLOBALS['xoopsSecurity']->getErrors()";
1050
    }
1051
1052
    /**
1053
     *  @public function getXoopsCodeHtmlErrors
1054
     *
1055
     *  @param $tableName
1056
     *  @param $isParam
1057
     *  @param $obj
1058
     *
1059
     *  @return string
1060
     */
1061
    public function getXoopsCodeHtmlErrors($tableName, $isParam = false, $obj = 'Obj')
1062
    {
1063
        $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...
1064
        if ($isParam) {
1065
            $ret = "\${$tableName}{$obj}->getHtmlErrors()";
1066
        } else {
1067
            $ret = "\${$tableName}{$obj} =& \${$tableName}->getHtmlErrors();";
1068
        }
1069
1070
        return $ret;
1071
    }
1072
1073
    /**
1074
     *  @public function getXoopsCodeObjHandlerCount
1075
     *
1076
     *  @param $left
1077
     *  @param $tableName
1078
     *  @param $obj
1079
     *
1080
     *  @return string
1081
     */
1082
    public function getXoopsCodeGetForm($left, $tableName, $obj = '')
1083
    {
1084
        $ucfTableName = ucfirst($tableName);
1085
1086
        return "\${$left} =& \${$tableName}{$obj}->getForm{$ucfTableName}();\n";
1087
    }
1088
1089
    /**
1090
     *  @public function getXoopsCodeGet
1091
     *
1092
     *  @param $tableName
1093
     *  @param $var
1094
     *  @param $obj
1095
     *  @param $isHandler
1096
     *  @param $isParam
1097
     *
1098
     *  @return string
1099
     */
1100
    public function getXoopsCodeGet($tableName, $var, $obj = '', $isHandler = false, $isParam = false)
1101
    {
1102
        $handler = $isHandler === false ? '' : 'Handler';
1103
        if ($isParam) {
1104
            $ret = "\${$tableName}{$handler}->get(\${$var})";
1105
        } else {
1106
            $ret = "\${$tableName}{$obj} =& \${$tableName}{$handler}->get(\${$var});\n";
1107
        }
1108
1109
        return $ret;
1110
    }
1111
1112
    /**
1113
     *  @public function getXoopsCodeHandler
1114
     *
1115
     *  @param $tableName
1116
     *  @param $var
1117
     *  @param $obj
1118
     *  @param $isHandler
1119
     *
1120
     *  @return string
1121
     */
1122
    public function getXoopsCodeInsert($tableName, $var, $obj = '', $isHandler = false)
1123
    {
1124
        $handler = ($isHandler === false) ? '' : 'Handler';
1125
        if ($obj != '') {
1126
            $ret = "\${$tableName}{$handler}->insert(\${$var}{$obj})";
1127
        } else {
1128
            $ret = "\${$tableName}{$handler}->insert(\${$var})";
1129
        }
1130
1131
        return $ret;
1132
    }
1133
1134
    /**
1135
     *  @public function getXoopsCodeDelete
1136
     *
1137
     *  @param $tableName
1138
     *  @param $var
1139
     *  @param $obj
1140
     *  @param $isHandler
1141
     *
1142
     *  @return string
1143
     */
1144
    public function getXoopsCodeDelete($tableName, $var, $obj = '', $isHandler = false)
1145
    {
1146
        $handler = $isHandler === false ? '' : 'Handler';
1147
        if ($obj != '') {
1148
            $ret = "\${$tableName}{$handler}->delete(\${$var}{$obj})";
1149
        } else {
1150
            $ret = "\${$tableName}{$handler}->delete(\${$var})";
1151
        }
1152
1153
        return $ret;
1154
    }
1155
1156
    /**
1157
     *  @public function getXoopsCodeHandler
1158
     *
1159
     *  @param $tableName
1160
     *  @param $var
1161
     *
1162
     *  @return string
1163
     */
1164
    public function getXoopsCodeHandler($tableName, $var, $get = false, $insert = false, $delete = false, $obj = '')
1165
    {
1166
        if ($get) {
1167
            $ret = "\${$tableName}Handler->get(\${$var});";
1168
        } elseif ($insert && ($obj != '')) {
1169
            $ret = "\${$tableName}Handler->insert(\${$var}{$obj});";
1170
        } elseif ($delete && ($obj != '')) {
1171
            $ret = "\${$tableName}Handler->delete(\${$var}{$obj});";
1172
        }
1173
1174
        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...
1175
    }
1176
1177
    /*
1178
    *  @public function getXoopsCodeCaseDelete
1179
    *  @param $tableName
1180
    *  @param $language
1181
    *  @param $fieldId
1182
    *  @param $fieldMain
1183
    *  @return string
1184
    */
1185
    public function getXoopsCodeCaseDelete($language, $tableName, $fieldId, $fieldMain)
1186
    {
1187
        $ccFieldId = $this->tdmcfile->getCamelCase($fieldId, false, true);
1188
        $ret = $this->getXoopsCodeGet($tableName, $ccFieldId, 'Obj', true);
1189
1190
        $reqOk = "\$_REQUEST['ok']";
1191
        $isset = $this->phpcode->getPhpCodeIsset($reqOk);
1192
        $xoopsSecurityCheck = $this->getXoopsCodeSecurityCheck();
1193
        $xoopsSecurityErrors = $this->getXoopsCodeSecurityErrors();
1194
        $implode = $this->phpcode->getPhpCodeImplode(', ', $xoopsSecurityErrors);
1195
        $redirectHeaderErrors = $this->getXoopsCodeRedirectHeader($tableName, '', '3', $implode);
1196
1197
        $delete = $this->getXoopsCodeDelete($tableName, $tableName, 'Obj', true);
1198
        $condition = $this->phpcode->getPhpCodeConditions('!'.$xoopsSecurityCheck, '', '', $redirectHeaderErrors);
1199
1200
        $redirectHeaderLanguage = $this->getXoopsCodeRedirectHeader($tableName, '', '3', "{$language}FORM_DELETE_OK");
1201
        $htmlErrors = $this->getXoopsCodeHtmlErrors($tableName, true);
1202
        $internalElse = $this->getXoopsCodeTplAssign('error', $htmlErrors);
1203
        $condition .= $this->phpcode->getPhpCodeConditions($delete, '', '', $redirectHeaderLanguage, $internalElse);
1204
1205
        $mainElse = $this->getXoopsCodeXoopsConfirm($tableName, $language, $fieldId, $fieldMain);
1206
        $ret .= $this->phpcode->getPhpCodeConditions($isset, ' && ', "1 == {$reqOk}", $condition, $mainElse);
1207
1208
        return $ret;
1209
    }
1210
1211
    /*
1212
    *  @public function getTopicGetVar
1213
    *  @param $lpFieldName
1214
    *  @param $rpFieldName
1215
    *  @param $tableName
1216
    *  @param $tableNameTopic
1217
    *  @param $fieldNameParent
1218
    *  @param $fieldNameTopic
1219
    *  @return string
1220
    */
1221
    public function getTopicGetVar($lpFieldName, $rpFieldName, $tableName, $tableNameTopic, $fieldNameParent, $fieldNameTopic)
1222
    {
1223
        $ret = <<<EOT
1224
\t\t// Get Var {$fieldNameParent}
1225
\t\t\${$rpFieldName} =& \${$tableNameTopic}Handler->get(\${$tableName}All[\$i]->getVar('{$fieldNameParent}'));
1226
\t\t\${$lpFieldName}['{$rpFieldName}'] = \${$rpFieldName}->getVar('{$fieldNameTopic}');\n
1227
EOT;
1228
1229
        return $ret;
1230
    }
1231
1232
    /*
1233
    *  @public function getUploadImageGetVar
1234
    *  @param $lpFieldName
1235
    *  @param $rpFieldName
1236
    *  @param $tableName
1237
    *  @param $fieldName
1238
    *  @return string
1239
    */
1240
    public function getUploadImageGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
1241
    {
1242
        $ret = <<<EOT
1243
\t\t// Get Var {$fieldName}
1244
\t\t\${$fieldName} = \${$tableName}All[\$i]->getVar('{$fieldName}');
1245
\t\t\$upload_image = \${$fieldName} ? \${$fieldName} : 'blank.gif';
1246
\t\t\${$lpFieldName}['{$rpFieldName}'] = \$upload_image;\n
1247
EOT;
1248
1249
        return $ret;
1250
    }
1251
1252
    /*
1253
    *  @public function getXoopsCodeUpdate
1254
    *  @param $language
1255
    *  @param $tableName
1256
    *  @param $fieldId
1257
    *  @param $fieldName
1258
    *  @return string
1259
    */
1260
    public function getXoopsCodeUpdate($language, $tableName, $fieldId, $fieldName)
1261
    {
1262
        $ccFieldId = $this->tdmcfile->getCamelCase($fieldId, false, true);
1263
        $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...
1264
        $isset = $this->phpcode->getPhpCodeIsset($ccFieldId);
1265
        $get = $this->getXoopsCodeHandler($tableName, $fieldId, true);
1266
        $ret = $this->phpcode->getPhpCodeConditions($isset, '', '', $get);
1267
        $ret .= $this->getXoopsCodeSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
1268
        $handlerInsert = $this->$this->getXoopsCodeHandler($tableName, $tableName, false, true, false, 'Obj');
1269
        $redirect = $this->getXoopsCodeRedirectHeader($tableName, '?op=list', 2, "{$language}FORM_UPDATE_OK");
1270
        $ret .= $this->phpcode->getPhpCodeConditions($handlerInsert, '', '', $redirect);
1271
1272
        $ret .= $this->getXoopsCodeTplAssign('error', "\${$tableName}Obj->getHtmlErrors()");
1273
1274
        return $this->phpcode->getPhpCodeCaseSwitch('update', $ret);
1275
    }
1276
1277
    /**
1278
     *  @public function getXoopsCodeSaveFieldId
1279
     *
1280
     *  @param $fields
1281
     *
1282
     *  @return string
1283
     */
1284
    public function getXoopsCodeSaveFieldId($fields)
1285
    {
1286
        foreach (array_keys($fields) as $f) {
1287
            if (0 == $f) {
1288
                $fieldId = $fields[$f]->getVar('field_name');
1289
            }
1290
        }
1291
1292
        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...
1293
    }
1294
1295
    /**
1296
     *  @public function getXoopsCodeSaveFieldMain
1297
     *
1298
     *  @param $fields
1299
     *
1300
     *  @return string
1301
     */
1302
    public function getXoopsCodeSaveFieldMain($fields)
1303
    {
1304
        foreach (array_keys($fields) as $f) {
1305
            if (1 == $fields[$f]->getVar('field_main')) {
1306
                $fieldMain = $fields[$f]->getVar('field_name');
1307
            }
1308
        }
1309
1310
        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...
1311
    }
1312
1313
    /**
1314
     *  @public function getXoopsCodeSaveElements
1315
     *
1316
     *  @param $moduleDirname
1317
     *  @param $tableName
1318
     *  @param $fields
1319
     *
1320
     *  @return string
1321
     */
1322
    public function getXoopsCodeSaveElements($moduleDirname, $tableName, $fields)
1323
    {
1324
        $ret = '';
1325
        foreach (array_keys($fields) as $f) {
1326
            $fieldName = $fields[$f]->getVar('field_name');
1327
            $fieldElement = $fields[$f]->getVar('field_element');
1328
            if (1 == $fields[$f]->getVar('field_main')) {
1329
                $fieldMain = $fieldName;
1330
            }
1331
            if ((5 == $fieldElement) || (6 == $fieldElement)) {
1332
                $ret .= $this->getXoopsCodeCheckBoxOrRadioYNSetVar($tableName, $fieldName);
1333
            } elseif (13 == $fieldElement) {
1334
                $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...
1335
            } elseif (14 == $fieldElement) {
1336
                $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...
1337
            } elseif (15 == $fieldElement) {
1338
                $ret .= $this->getXoopsCodeTextDateSelectSetVar($tableName, $fieldName);
1339
            } else {
1340
                $ret .= $this->getXoopsCodeSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
1341
            }
1342
        }
1343
1344
        return $ret;
1345
    }
1346
1347
    /*
1348
    *  @public function getXoopsCodePageNav
1349
    *  @param $tableName
1350
    *
1351
    *  @return string
1352
    */
1353
    public function getXoopsCodePageNav($tableName)
1354
    {
1355
        $condition = $this->phpcode->getPhpCodeCommentLine('Display Navigation');
1356
        $condition .= $this->phpcode->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/pagenav', true);
1357
        $condition .= "\$pagenav = new XoopsPageNav(\${$tableName}Count, \$limit, \$start, 'start', 'op=list&limit=' . \$limit);\n";
1358
        $condition .= $this->getXoopsCodeTplAssign('pagenav', '$pagenav->renderNav(4)');
1359
        $ret = $this->phpcode->getPhpCodeConditions("\${$tableName}Count", ' > ', '$limit', $condition, false, "\t\t");
1360
1361
        return $ret;
1362
    }
1363
}
1364