Passed
Branch timgno (61fada)
by Gino
05:14
created

TDMCreateXoopsCode::getXoopsCodeCheckRight()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 6
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 28 and the first side effect is on line 24.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
/**
13
 * tdmcreate module.
14
 *
15
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 *
18
 * @since           2.5.0
19
 *
20
 * @author          Txmod Xoops http://www.txmodxoops.org
21
 *
22
 * @version         $Id: 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 $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 === false) {
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 null
270
    *  @return string
271
    */
272
    public function getXoopsCodeXoopsCaptcha()
273
    {
274
        return "\$xoopsCaptcha = XoopsCaptcha::getInstance();\n";
275
    }
276
277
    /*
278
    *  @public function getXoopsCodeGetConfig
279
    *  @param $moduleDirname
280
    *  @param $name
281
    *  @return string
282
    */
283
    public function getXoopsCodeGetConfig($moduleDirname, $name)
284
    {
285
        return "\${$moduleDirname}->getConfig('{$name}')";
286
    }
287
288
    /*
289
    *  @public function getXoopsCodeIdGetVar
290
    *  @param $lpFieldName
291
    *  @return string
292
    */
293
    public function getXoopsCodeIdGetVar($lpFieldName)
294
    {
295
        return "\${$lpFieldName}['id'] = \$i;\n";
296
    }
297
298
    /*
299
    *  @public function getXoopsCodeGetVarAll
300
    *  @param $lpFieldName
301
    *  @param $rpFieldName
302
    *  @param $tableName
303
    *  @param $fieldName
304
    *  @return string
305
    */
306
    public function getXoopsCodeGetVarAll($lpFieldName, $rpFieldName, $tableName, $fieldName)
307
    {
308
        return "\${$lpFieldName}['{$rpFieldName}'] = \${$tableName}All[\$i]->getVar('{$fieldName}');\n";
309
    }
310
311
    /*
312
    *  @public function getXoopsHandlerInstance
313
    *  @param $moduleDirname
314
    *  
315
    *  @return string
316
    */
317
    public function getXoopsHandlerInstance($moduleDirname)
318
    {
319
        $ucfModuleDirname = ucfirst($moduleDirname);
320
        $ret = "// Get instance of module\n";
321
        $ret .= "\${$moduleDirname} = {$ucfModuleDirname}Helper::getInstance();\n";
322
323
        return $ret;
324
    }
325
326
    /*
327
    *  @public function getXoopsHandlerLine
328
    *  @param $moduleDirname
329
    *  @param $tableName
330
    *  @return string
331
    */
332
    public function getXoopsHandlerLine($moduleDirname, $tableName)
333
    {
334
        return "\${$tableName}Handler =& \${$moduleDirname}->getHandler('{$tableName}');\n";
335
    }
336
337
    /*
338
    *  @public function getXoopsSimpleForm
339
    *  @param $left
340
    *  @param $element
341
    *  @param $elementsContent
342
    *  @param $caption
343
    *  @param $var
344
    *  @param $filename
345
    *  @param $type
346
    *  
347
    *  @return string
348
    */
349
    public function getXoopsSimpleForm($left = '', $element = '', $elementsContent = '', $caption = '', $var = '', $filename = '', $type = 'post')
350
    {
351
        $ret = "\${$left} = new XoopsSimpleForm({$caption}, '{$var}', '{$filename}.php', '{$type}');\n";
352
        if (!empty($elementsContent)) {
353
            $ret .= "{$elementsContent}";
354
        }
355
        $ret .= "\${$left}->addElement(\${$element});\n";
356
        $ret .= "\${$left}->display();\n";
357
358
        return $ret;
359
    }
360
361
    /*
362
    *  @public function getXoopsFormSelect
363
    *  @param $varSelect
364
    *  @param $caption
365
    *  @param $var
366
    *  @param $options
367
    *  @param $setExtra
368
    *  
369
    *  @return string
370
    */
371
    public function getXoopsFormSelect($varSelect = '', $caption = '', $var = '', $options = array(), $setExtra = true)
372
    {
373
        $ret = "\${$varSelect} = new XoopsFormSelect({$caption}, '{$var}', \${$var});\n";
374
        if (false !== $setExtra) {
375
            $ret .= "\${$varSelect}->setExtra('{$setExtra}');\n";
376
        }
377
        foreach ($options as $key => $value) {
378
            $ret .= "\${$varSelect}->addOption('{$key}', {$value});\n";
379
        }
380
381
        return $ret;
382
    }
383
384
    /*
385
     *  @public function getXoopsCodeUnameFromId
386
     *  @param $left
387
     *  @param $tableName
388
     *
389
     * @return string
390
     */
391
    public function getXoopsCodeUnameFromId($left, $value)
392
    {
393
        return "\${$left} = XoopsUser::getUnameFromId({$value});\n";
394
    }
395
396
    /*
397
    *  @public function getXoopsCodeFormatTimeStamp
398
    *  @param $lpFieldName
399
    *  @param $rpFieldName
400
    *  @param $tableName
401
    *  @param $fieldName
402
    *  @return string
403
    */
404
    public function getXoopsCodeFormatTimeStamp($left, $value, $format = 's')
405
    {
406
        return "\${$left} = formatTimeStamp({$value}, '{$format}');\n";
407
    }
408
409
    /*
410
    *  @public function getXoopsCodeTopicGetVar
411
    *  @param $lpFieldName
412
    *  @param $rpFieldName
413
    *  @param $tableName
414
    *  @param $tableNameTopic
415
    *  @param $fieldNameParent
416
    *  @param $fieldNameTopic
417
    *  @return string
418
    */
419
    public function getXoopsCodeTopicGetVar($lpFieldName, $rpFieldName, $tableName, $tableNameTopic, $fieldNameParent, $fieldNameTopic)
420
    {
421
        $ret = <<<EOT
422
\t\t\t\t// Get Var {$fieldNameParent}
423
\t\t\t\t\${$rpFieldName} =& \${$tableNameTopic}Handler->get(\${$tableName}All[\$i]->getVar('{$fieldNameParent}'));
424
\t\t\t\t\${$lpFieldName}['{$rpFieldName}'] = \${$rpFieldName}->getVar('{$fieldNameTopic}');\n
425
EOT;
426
427
        return $ret;
428
    }
429
430
    /*
431
    *  @public function getXoopsCodeParentTopicGetVar
432
    *  @param $moduleDirname
433
    *  @param $lpFieldName
434
    *  @param $rpFieldName
435
    *  @param $tableName
436
    *  @param $tableSoleNameTopic
437
    *  @param $tableNameTopic
438
    *  @param $fieldNameParent
439
    *  @return string
440
    */
441 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...
442
    {
443
        $ret = <<<EOT
444
\t\t\t\tif(!isset(\${$tableNameTopic}Handler)) {
445
\t\t\t\t\t// Get {$tableNameTopic} Handler
446
\t\t\t\t\t\${$tableNameTopic}Handler =& \${$moduleDirname}->getHandler('{$tableNameTopic}');
447
\t\t\t\t}
448
\t\t\t\t// Get Var {$fieldNameParent}
449
\t\t\t\t\${$lpFieldName}['{$rpFieldName}'] = \${$tableNameTopic}Handler->get{$tableSoleNameTopic}FromId(\${$tableName}All[\$i]->getVar('{$fieldNameParent}'));\n
450
EOT;
451
452
        return $ret;
453
    }
454
455
    /*
456
    *  @public function getXoopsCodeUploadImageGetVar
457
    *  @param $lpFieldName
458
    *  @param $rpFieldName
459
    *  @param $tableName
460
    *  @param $fieldName
461
    *  @return string
462
    */
463
    public function getXoopsCodeUploadImageGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
464
    {
465
        $ret = <<<EOT
466
\t\t\t\t// Get Var {$fieldName}
467
\t\t\t\t\${$fieldName} = \${$tableName}All[\$i]->getVar('{$fieldName}');
468
\t\t\t\t\${$lpFieldName}['{$rpFieldName}'] = \${$fieldName} ? \${$fieldName} : 'blank.gif';\n
469
EOT;
470
471
        return $ret;
472
    }
473
    /*
474
    *  @public function getXoopsCodeUrlFileGetVar
475
    *  @param $lpFieldName
476
    *  @param $rpFieldName
477
    *  @param $tableName
478
    *  @param $fieldName
479
    *  @return string
480
    */
481
    public function getXoopsCodeUrlFileGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
482
    {
483
        return $this->getXoopsCodeGetVarAll($lpFieldName, $rpFieldName, $tableName, $fieldName);
484
    }
485
    /*
486
    *  @public function getXoopsCodeTextAreaGetVar
487
    *  @param $lpFieldName
488
    *  @param $rpFieldName
489
    *  @param $tableName
490
    *  @param $fieldName
491
    *  @return string
492
    */
493
    public function getXoopsCodeTextAreaGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
494
    {
495
        return "\${$lpFieldName}['{$rpFieldName}'] = strip_tags(\${$tableName}All[\$i]->getVar('{$fieldName}'));\n";
496
    }
497
498
    /*
499
    *  @public function getXoopsCodeSelectUserGetVar
500
    *  @param $lpFieldName
501
    *  @param $rpFieldName
502
    *  @param $tableName
503
    *  @param $fieldName
504
    * @return string
505
    */
506
    public function getXoopsCodeSelectUserGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
507
    {
508
        return "\${$lpFieldName}['{$rpFieldName}'] = XoopsUser::getUnameFromId(\${$tableName}All[\$i]->getVar('{$fieldName}'), 's');\n";
509
    }
510
511
    /*
512
    *  @public function getXoopsCodeTextDateSelectGetVar
513
    *  @param $lpFieldName
514
    *  @param $rpFieldName
515
    *  @param $tableName
516
    *  @param $fieldName
517
    *  @return string
518
    */
519
    public function getXoopsCodeTextDateSelectGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
520
    {
521
        return "\${$lpFieldName}['{$rpFieldName}'] = formatTimeStamp(\${$tableName}All[\$i]->getVar('{$fieldName}'), 's');\n";
522
    }
523
524
    /*
525
    *  @public function getXoopsCodeUserHeader
526
    *  @param $moduleDirname
527
    *  @param $tableName
528
    *  @return string
529
    */
530
    public function getXoopsCodeXoopsOptionTemplateMain($moduleDirname, $tableName)
531
    {
532
        return "\$GLOBALS['xoopsOption']['template_main'] = '{$moduleDirname}_{$tableName}.tpl';\n";
533
    }
534
535
    /*
536
    *  @public function getXoopsCodeUserHeader
537
    *  @param $moduleDirname
538
    *  @param $tableName
539
    *  @return string
540
    */
541
    public function getXoopsCodeUserHeader($moduleDirname, $tableName)
542
    {
543
        $ret = $this->phpcode->getPhpCodeIncludeDir('__DIR__', 'header');
544
        $ret .= $this->getXoopsCodeXoopsOptionTemplateMain($moduleDirname, $tableName);
545
        $ret .= $this->phpcode->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'header', true);
546
547
        return $ret;
548
    }
549
550
    /*
551
    *  @public function getXoopsCodePermissionsHeader
552
    *  @param null
553
    */
554
    /**
555
     * @return string
556
     */
557
    public function getXoopsCodePermissionsHeader()
558
    {
559
        $ret = $this->phpcode->getPhpCodeCommentLine('Permission');
560
        $ret .= $this->phpcode->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/xoopsform/grouppermform', true);
561
        $ret .= $this->getXoopsCodeEqualsOperator('$gperm_handler', "xoops_gethandler('groupperm')", true);
562
        $groups = $this->getXoopsCodeEqualsOperator('$groups', '$xoopsUser->getGroups()');
563
        $elseGroups = $this->getXoopsCodeEqualsOperator('$groups', 'XOOPS_GROUP_ANONYMOUS');
564
        $ret .= $this->phpcode->getPhpCodeConditions('is_object($xoopsUser)', '', $type = '', $groups, $elseGroups);
565
566
        return $ret;
567
    }
568
569
    /**
570
     *  @public function getXoopsCodeGetFieldId
571
     *
572
     *  @param $fields
573
     *
574
     *  @return string
575
     */
576 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...
577
    {
578
        $fieldId = 'id';
579
        foreach (array_keys($fields) as $f) {
580
            $fieldName = $fields[$f]->getVar('field_name');
581
            if (0 == $f) {
582
                $fieldId = $fieldName;
583
            }
584
        }
585
586
        return $fieldId;
587
    }
588
589
    /**
590
     *  @public function getXoopsCodeGetFieldName
591
     *
592
     *  @param $fields
593
     *
594
     *  @return string
595
     */
596
    public function getXoopsCodeGetFieldName($fields)
597
    {
598
        foreach (array_keys($fields) as $f) {
599
            $fieldName = $fields[$f]->getVar('field_name');
600
        }
601
602
        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...
603
    }
604
605
    /**
606
     *  @public function getXoopsCodeGetFieldParentId
607
     *
608
     *  @param $fields
609
     *
610
     *  @return string
611
     */
612 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...
613
    {
614
        $fieldPid = 'pid';
615
        foreach (array_keys($fields) as $f) {
616
            $fieldName = $fields[$f]->getVar('field_name');
617
            if (1 == $fields[$f]->getVar('field_parent')) {
618
                $fieldPid = $fieldName;
619
            }
620
        }
621
622
        return $fieldPid;
623
    }
624
625
    /**
626
     *  @public function getXoopsCodeUserSaveElements
627
     *
628
     *  @param $moduleDirname
629
     *  @param $tableName
630
     *  @param $fields
631
     *
632
     *  @return string
633
     */
634 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...
635
    {
636
        $ret = '';
637
        foreach (array_keys($fields) as $f) {
638
            $fieldName = $fields[$f]->getVar('field_name');
639
            $fieldElement = $fields[$f]->getVar('field_element');
640
            if (1 == $fields[$f]->getVar('field_main')) {
641
                $fieldMain = $fieldName;
642
            }
643
            if ((5 == $fieldElement) || (6 == $fieldElement)) {
644
                $ret .= $this->getXoopsCodeCheckBoxOrRadioYNSetVar($tableName, $fieldName);
645
            } elseif (13 == $fieldElement) {
646
                $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...
647
            } elseif (14 == $fieldElement) {
648
                $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...
649
            } elseif (15 == $fieldElement) {
650
                $ret .= $this->getXoopsCodeTextDateSelectSetVar($tableName, $fieldName);
651
            } else {
652
                $ret .= $this->getXoopsCodeSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
653
            }
654
        }
655
656
        return $ret;
657
    }
658
659
    /*
660
    *  @public function getXoopsCodeXoopsRequest
661
    *  @param $left
662
    *  @param $var1
663
    *  @param $var2
664
    *  @param $type
665
    *  @param $metod
666
    *  @return string
667
    */
668
    public function getXoopsCodeXoopsRequest($left = '', $var1 = '', $var2 = '', $type = 'String', $metod = false)
669
    {
670
        $ret = '';
671
        $intVars = ($var2 != '') ? "'{$var1}', {$var2}" : "'{$var1}'";
672
        if ($type == 'String') {
673
            $ret .= "\${$left} = XoopsRequest::getString('{$var1}', '{$var2}');\n";
674
        } elseif ($type == 'Int') {
675
            $ret .= "\${$left} = XoopsRequest::getInt({$intVars});\n";
676
        } elseif ($type == 'Int' && $metod !== false) {
677
            $ret .= "\${$left} = XoopsRequest::getInt({$intVars}, '{$metod}');\n";
678
        }
679
680
        return $ret;
681
    }
682
683
    /**
684
     *  @public function getXoopsCodeTplAssign     
685
     *
686
     *  @param $tplString
687
     *  @param $phpRender
688
     *  @param $leftIsString 
689
     *
690
     *  @return string
691
     */
692
    public function getXoopsCodeTplAssign($tplString, $phpRender, $leftIsString = true)
693
    {
694
        $assign = "\$GLOBALS['xoopsTpl']->assign(";
695
        if ($leftIsString === false) {
696
            $ret = $assign."{$tplString}, {$phpRender});\n";
697
        } else {
698
            $ret = $assign."'{$tplString}', {$phpRender});\n";
699
        }
700
701
        return $ret;
702
    }
703
704
    /**
705
     *  @public function getXoopsCodeXoopsTplAppend
706
     *
707
     *  @param $tplString
708
     *  @param $phpRender
709
     *
710
     *  @return string
711
     */
712
    public function getXoopsCodeXoopsTplAppend($tplString, $phpRender)
713
    {
714
        return "\$GLOBALS['xoopsTpl']->append('{$tplString}', {$phpRender});\n";
715
    }
716
717
    /**
718
     *  @public function getXoopsCodeXoopsTplAppendByRef
719
     *
720
     *  @param $tplString
721
     *  @param $phpRender
722
     *
723
     *  @return string
724
     */
725
    public function getXoopsCodeXoopsTplAppendByRef($tplString, $phpRender)
726
    {
727
        return "\$GLOBALS['xoopsTpl']->appendByRef('{$tplString}', {$phpRender});\n";
728
    }
729
730
    /**
731
     *  @public function getXoopsCodePath
732
     *
733
     *  @param $directory
734
     *  @param $filename
735
     *  @param $isParam
736
     *
737
     *  @return string
738
     */
739
    public function getXoopsCodePath($directory, $filename, $isParam = false)
740
    {
741
        if ($isParam === false) {
742
            $ret = "\$GLOBALS['xoops']->path({$directory}.'/{$filename}.php');\n";
743
        } else {
744
            $ret = "\$GLOBALS['xoops']->path({$directory}.'/{$filename}.php')";
745
        }
746
747
        return $ret;
748
    }
749
750
    /**
751
     *  @public function getXoopsCodeTplDisplay
752
     *
753
     *  @param $displayTpl
754
     *
755
     *  @return string
756
     */
757
    public function getXoopsCodeTplDisplay($displayTpl = '{$templateMain}')
758
    {
759
        return "\$GLOBALS['xoopsTpl']->display(\"db:{$displayTpl}\");\n";
760
    }
761
762
    /**
763
     *  @public function getXoopsCodeGetInfo
764
     *
765
     *  @param $left
766
     *  @param $string
767
     *  @param $isParam
768
     *
769
     *  @return string
770
     */
771
    public function getXoopsCodeGetInfo($left = '', $string, $isParam = false)
772
    {
773
        if ($isParam === false) {
774
            $ret = "\${$left} = \$GLOBALS['xoopsModule']->getInfo('{$string}');\n";
775
        } else {
776
            $ret = "\$GLOBALS['xoopsModule']->getInfo('{$string}')";
777
        }
778
779
        return $ret;
780
    }
781
782
    /**
783
     *  @public function getXoopsCodeCheckRight
784
     *
785
     *  @param $anchor
786
     *  @param $permString
787
     *  @param $var
788
     *  @param $groups
789
     *  @param $mid
790
     *  @param $isParam
791
     *
792
     *  @return string
793
     */
794
    public function getXoopsCodeCheckRight($anchor, $permString = '', $var = '', $groups = '', $mid = '', $isParam = false)
795
    {
796
        if ($isParam === false) {
797
            $ret = "{$anchor}->checkRight('{$permString}', {$var}, {$groups}, {$mid});\n";
798
        } else {
799
            $ret = "{$anchor}->checkRight('{$permString}', {$var}, {$groups}, {$mid})";
800
        }
801
802
        return $ret;
803
    }
804
805
    /**
806
     *  @public function getXoopsCodeObjHandlerCreate
807
     *
808
     *  @param $tableName
809
     *
810
     *  @return string
811
     */
812
    public function getXoopsCodeObjHandlerCreate($tableName)
813
    {
814
        return "\${$tableName}Obj =& \${$tableName}Handler->create();\n";
815
    }
816
817
    /**
818
     *  @public function getXoopsCodeObjHandlerCount
819
     *
820
     *  @param $tableName
821
     *
822
     *  @return string
823
     */
824
    public function getXoopsCodeObjHandlerCount($tableName)
825
    {
826
        $ucfTableName = ucfirst($tableName);
827
        $ret = "\${$tableName}Count = \${$tableName}Handler->getCount{$ucfTableName}();\n";
828
829
        return $ret;
830
    }
831
832
    /**
833
     *  @public function getXoopsCodeObjHandlerAll
834
     *
835
     *  @param $tableName
836
     *  @param $fieldMain
837
     *  @param $start
838
     *  @param $limit
839
     *
840
     *  @return string
841
     */
842
    public function getXoopsCodeObjHandlerAll($tableName, $fieldMain, $start = '0', $limit = '0')
843
    {
844
        $ucfTableName = ucfirst($tableName);
845
        $param = ($fieldMain != '') ? "{$start}, {$limit}, '{$fieldMain}'" : "{$start}, {$limit}";
846
        $ret = "\${$tableName}All = \${$tableName}Handler->getAll{$ucfTableName}({$param});\n";
847
848
        return $ret;
849
    }
850
851
    /**
852
     *  @public function getXoopsCodeGetValues
853
     *
854
     *  @param $tableName
855
     *  @param $tableSoleName
856
     *
857
     *  @return string
858
     */
859
    public function getXoopsCodeGetValues($tableName, $tableSoleName, $index = 'i', $noArray = false)
860
    {
861
        $ucfTableName = ucfirst($tableName);
862
        if ($noArray == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
863
            $ret = "\${$tableSoleName} = \${$tableName}All[\${$index}]->getValues{$ucfTableName}();\n";
864
        } else {
865
            $ret = "\${$tableSoleName} = \${$tableName}->getValues{$ucfTableName}();\n";
866
        }
867
868
        return $ret;
869
    }
870
871
    /**
872
     *  @public function getXoopsCodeObjectTree
873
     *
874
     *  @param $tableName
875
     *  @param $fieldId
876
     *  @param $fieldParent
877
     *
878
     *  @return string
879
     */
880
    public function getXoopsCodeObjectTree($tableName, $fieldId, $fieldParent)
881
    {
882
        $ret = "\$mytree = new XoopsObjectTree(\${$tableName}All, '{$fieldId}', '{$fieldParent}');\n";
883
884
        return $ret;
885
    }
886
887
    /**
888
     *  @public function getXoopsCodeSetVarsObjects
889
     *
890
     *  @param $moduleDirname
891
     *  @param $tableName
892
     *  @param $fields
893
     *
894
     *  @return string
895
     */
896
    public function getXoopsCodeSetVarsObjects($moduleDirname, $tableName, $fields)
897
    {
898
        $ret = '';
899
        foreach (array_keys($fields) as $f) {
900
            $fieldName = $fields[$f]->getVar('field_name');
901
            $fieldElement = $fields[$f]->getVar('field_element');
902
            if (1 == $fields[$f]->getVar('field_main')) {
903
                $fieldMain = $fieldName;
904
            }
905
            if ($f > 0) { // If we want to hide field id
906
                switch ($fieldElement) {
907
                    case 5:
908
                    case 6:
909
                        $ret .= $this->getXoopsCodeCheckBoxOrRadioYNSetVar($tableName, $fieldName);
910
                        break;
911
                    case 11:
912
                        $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...
913
                        break;
914
                    case 12:
915
                        $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...
916
                        break;
917
                    case 13:
918
                        $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...
919
                        break;
920
                    case 14:
921
                        $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...
922
                        break;
923
                    case 15:
924
                        $ret .= $this->getXoopsCodeTextDateSelectSetVar($tableName, $fieldName);
925
                        break;
926
                    default:
927
                        $ret .= $this->getXoopsCodeSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
928
                        break;
929
                }
930
            }
931
        }
932
933
        return $ret;
934
    }
935
936
    /**
937
     *  @public function getXoopsCodeSecurity
938
     *
939
     *  @param $tableName
940
     *
941
     *  @return string
942
     */
943
    public function getXoopsCodeSecurity($tableName)
944
    {
945
        $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...
946
        $implode = $this->phpcode->getPhpCodeImplode(',', $securityError);
947
        $content = $this->getXoopsCodeRedirectHeader($tableName.'.php', '', 3, $implode);
948
        $securityCheck = $this->getXoopsCodeSecurityCheck();
949
950
        return $this->phpcode->getPhpCodeConditions('!'.$securityCheck, '', '', $content);
951
    }
952
953
    /*
954
    *  @public function getXoopsCodeInsertData
955
    *  @param $tableName
956
    *  @param $language
957
    *  @return string
958
    */
959
    public function getXoopsCodeInsertData($tableName, $language)
960
    {
961
        $content = $this->getXoopsCodeRedirectHeader($tableName.'.php', '?op=list', 2, "{$language}FORM_OK");
962
        $handlerInsert = $this->getXoopsCodeHandler($tableName, $tableName, false, true, false, 'Obj');
963
964
        return $this->phpcode->getPhpCodeConditions($handlerInsert, '', '', $content);
965
    }
966
967
    /*
968
    *  @public function getXoopsCodeRedirectHeader
969
    *  @param $tableName
970
    *  @param $options
971
    *  @param $numb
972
    *  @param $var
973
    *  @param $isString
974
    *
975
    *  @return string
976
    */
977
    public function getXoopsCodeRedirectHeader($tableName, $options = '', $numb = '2', $var, $isString = true)
978
    {
979
        if ($isString === false) {
980
            $ret = "redirect_header({$tableName}, {$numb}, {$var});\n";
981
        } else {
982
            $ret = "redirect_header('{$tableName}{$options}', {$numb}, {$var});\n";
983
        }
984
985
        return $ret;
986
    }
987
988
    /*
989
    *  @public function getXoopsCodeXoopsConfirm
990
    *  @param $tableName
991
    *  @param $language
992
    *  @param $fieldId
993
    *  @param $fieldMain    
994
    *  @param $options
995
    *
996
    *  @return string
997
    */
998
    public function getXoopsCodeXoopsConfirm($tableName, $language, $fieldId, $fieldMain, $options = 'delete')
999
    {
1000
        $stuOptions = strtoupper($options);
1001
        $ccFieldId = $this->tdmcfile->getCamelCase($fieldId, false, true);
1002
        $ret = "xoops_confirm(array('ok' => 1, '{$fieldId}' => \${$ccFieldId}, 'op' => {$options}), \$_SERVER['REQUEST_URI'], sprintf({$language}FORM_SURE_{$stuOptions}, \${$tableName}Obj->getVar('{$fieldMain}')));\n";
1003
1004
        return $ret;
1005
    }
1006
1007
    /*
1008
    *  @public function getXoopsCodeAddStylesheet
1009
    *  @param $style
1010
    *  
1011
    *  @return string
1012
    */
1013
    public function getXoopsCodeAddStylesheet($style = 'style')
1014
    {
1015
        return "\$GLOBALS['xoTheme']->addStylesheet( \${$style}, null );\n";
1016
    }
1017
1018
    /*
1019
    *  @public function getXoopsCodeSecurityCheck
1020
    *  @param null
1021
    *  @return boolean
1022
    */
1023
    public function getXoopsCodeSecurityCheck()
1024
    {
1025
        return "\$GLOBALS['xoopsSecurity']->check()";
1026
    }
1027
1028
    /*
1029
    *  @public function getXoopsCodeSecurityErrors
1030
    *  @param null
1031
    *  @return string
1032
    */
1033
    public function getXoopsCodeSecurityErrors()
1034
    {
1035
        return "\$GLOBALS['xoopsSecurity']->getErrors()";
1036
    }
1037
1038
    /**
1039
     *  @public function getXoopsCodeHtmlErrors
1040
     *
1041
     *  @param $tableName
1042
     *  @param $isParam
1043
     *  @param $obj
1044
     *
1045
     *  @return string
1046
     */
1047
    public function getXoopsCodeHtmlErrors($tableName, $isParam = false, $obj = 'Obj')
1048
    {
1049
        $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...
1050
        if ($isParam) {
1051
            $ret = "\${$tableName}{$obj}->getHtmlErrors()";
1052
        } else {
1053
            $ret = "\${$tableName}{$obj} =& \${$tableName}->getHtmlErrors();";
1054
        }
1055
1056
        return $ret;
1057
    }
1058
1059
    /**
1060
     *  @public function getXoopsCodeObjHandlerCount
1061
     *
1062
     *  @param $left
1063
     *  @param $tableName
1064
     *  @param $obj
1065
     *
1066
     *  @return string
1067
     */
1068
    public function getXoopsCodeGetForm($left, $tableName, $obj = '')
1069
    {
1070
        $ucfTableName = ucfirst($tableName);
1071
1072
        return "\${$left} =& \${$tableName}{$obj}->getForm{$ucfTableName}();\n";
1073
    }
1074
1075
    /**
1076
     *  @public function getXoopsCodeGet
1077
     *
1078
     *  @param $tableName
1079
     *  @param $var
1080
     *  @param $obj
1081
     *  @param $isHandler
1082
     *  @param $isParam
1083
     *
1084
     *  @return string
1085
     */
1086 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...
1087
    {
1088
        $handler = $isHandler === false ? '' : 'Handler';
1089
        if ($isParam) {
1090
            $ret = "\${$tableName}{$handler}->get(\${$var})";
1091
        } else {
1092
            $ret = "\${$tableName}{$obj} =& \${$tableName}{$handler}->get(\${$var});\n";
1093
        }
1094
1095
        return $ret;
1096
    }
1097
1098
    /**
1099
     *  @public function getXoopsCodeHandler
1100
     *
1101
     *  @param $tableName
1102
     *  @param $var
1103
     *  @param $obj
1104
     *  @param $isHandler
1105
     *
1106
     *  @return string
1107
     */
1108 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...
1109
    {
1110
        $handler = ($isHandler === false) ? '' : 'Handler';
1111
        if ($obj != '') {
1112
            $ret = "\${$tableName}{$handler}->insert(\${$var}{$obj})";
1113
        } else {
1114
            $ret = "\${$tableName}{$handler}->insert(\${$var})";
1115
        }
1116
1117
        return $ret;
1118
    }
1119
1120
    /**
1121
     *  @public function getXoopsCodeDelete
1122
     *
1123
     *  @param $tableName
1124
     *  @param $var
1125
     *  @param $obj
1126
     *  @param $isHandler
1127
     *
1128
     *  @return string
1129
     */
1130 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...
1131
    {
1132
        $handler = $isHandler === false ? '' : 'Handler';
1133
        if ($obj != '') {
1134
            $ret = "\${$tableName}{$handler}->delete(\${$var}{$obj})";
1135
        } else {
1136
            $ret = "\${$tableName}{$handler}->delete(\${$var})";
1137
        }
1138
1139
        return $ret;
1140
    }
1141
1142
    /**
1143
     *  @public function getXoopsCodeHandler
1144
     *
1145
     *  @param $tableName
1146
     *  @param $var
1147
     *
1148
     *  @return string
1149
     */
1150
    public function getXoopsCodeHandler($tableName, $var, $get = false, $insert = false, $delete = false, $obj = '')
1151
    {
1152
        if ($get) {
1153
            $ret = "\${$tableName}Handler->get(\${$var});";
1154
        } elseif ($insert && ($obj != '')) {
1155
            $ret = "\${$tableName}Handler->insert(\${$var}{$obj});";
1156
        } elseif ($delete && ($obj != '')) {
1157
            $ret = "\${$tableName}Handler->delete(\${$var}{$obj});";
1158
        }
1159
1160
        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...
1161
    }
1162
1163
    /*
1164
    *  @public function getXoopsCodeCaseDelete
1165
    *  @param $tableName
1166
    *  @param $language
1167
    *  @param $fieldId
1168
    *  @param $fieldMain
1169
    *  @return string
1170
    */
1171
    public function getXoopsCodeCaseDelete($language, $tableName, $fieldId, $fieldMain)
1172
    {
1173
        $ccFieldId = $this->tdmcfile->getCamelCase($fieldId, false, true);
1174
        $ret = $this->getXoopsCodeGet($tableName, $ccFieldId, 'Obj', true);
1175
1176
        $reqOk = "\$_REQUEST['ok']";
1177
        $isset = $this->phpcode->getPhpCodeIsset($reqOk);
1178
        $xoopsSecurityCheck = $this->getXoopsCodeSecurityCheck();
1179
        $xoopsSecurityErrors = $this->getXoopsCodeSecurityErrors();
1180
        $implode = $this->phpcode->getPhpCodeImplode(', ', $xoopsSecurityErrors);
1181
        $redirectHeaderErrors = $this->getXoopsCodeRedirectHeader($tableName, '', '3', $implode);
1182
1183
        $delete = $this->getXoopsCodeDelete($tableName, $tableName, 'Obj', true);
1184
        $condition = $this->phpcode->getPhpCodeConditions('!'.$xoopsSecurityCheck, '', '', $redirectHeaderErrors);
1185
1186
        $redirectHeaderLanguage = $this->getXoopsCodeRedirectHeader($tableName, '', '3', "{$language}FORM_DELETE_OK");
1187
        $htmlErrors = $this->getXoopsCodeHtmlErrors($tableName, true);
1188
        $internalElse = $this->getXoopsCodeTplAssign('error', $htmlErrors);
1189
        $condition .= $this->phpcode->getPhpCodeConditions($delete, '', '', $redirectHeaderLanguage, $internalElse);
1190
1191
        $mainElse = $this->getXoopsCodeXoopsConfirm($tableName, $language, $fieldId, $fieldMain);
1192
        $ret .= $this->phpcode->getPhpCodeConditions($isset, ' && ', "1 == {$reqOk}", $condition, $mainElse);
1193
1194
        return $ret;
1195
    }
1196
1197
    /*
1198
    *  @public function getTopicGetVar
1199
    *  @param $lpFieldName
1200
    *  @param $rpFieldName
1201
    *  @param $tableName
1202
    *  @param $tableNameTopic
1203
    *  @param $fieldNameParent
1204
    *  @param $fieldNameTopic
1205
    *  @return string
1206
    */
1207
    public function getTopicGetVar($lpFieldName, $rpFieldName, $tableName, $tableNameTopic, $fieldNameParent, $fieldNameTopic)
1208
    {
1209
        $ret = <<<EOT
1210
\t\t// Get Var {$fieldNameParent}
1211
\t\t\${$rpFieldName} =& \${$tableNameTopic}Handler->get(\${$tableName}All[\$i]->getVar('{$fieldNameParent}'));
1212
\t\t\${$lpFieldName}['{$rpFieldName}'] = \${$rpFieldName}->getVar('{$fieldNameTopic}');\n
1213
EOT;
1214
1215
        return $ret;
1216
    }
1217
1218
    /*
1219
    *  @public function getUploadImageGetVar
1220
    *  @param $lpFieldName
1221
    *  @param $rpFieldName
1222
    *  @param $tableName
1223
    *  @param $fieldName
1224
    *  @return string
1225
    */
1226
    public function getUploadImageGetVar($lpFieldName, $rpFieldName, $tableName, $fieldName)
1227
    {
1228
        $ret = <<<EOT
1229
\t\t// Get Var {$fieldName}
1230
\t\t\${$fieldName} = \${$tableName}All[\$i]->getVar('{$fieldName}');
1231
\t\t\$upload_image = \${$fieldName} ? \${$fieldName} : 'blank.gif';
1232
\t\t\${$lpFieldName}['{$rpFieldName}'] = \$upload_image;\n
1233
EOT;
1234
1235
        return $ret;
1236
    }
1237
1238
    /*
1239
    *  @public function getXoopsCodeUpdate
1240
    *  @param $language
1241
    *  @param $tableName
1242
    *  @param $fieldId
1243
    *  @param $fieldName
1244
    *  @return string
1245
    */
1246
    public function getXoopsCodeUpdate($language, $tableName, $fieldId, $fieldName)
1247
    {
1248
        $ccFieldId = $this->tdmcfile->getCamelCase($fieldId, false, true);
1249
        $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...
1250
        $isset = $this->phpcode->getPhpCodeIsset($ccFieldId);
1251
        $get = $this->getXoopsCodeHandler($tableName, $fieldId, true);
1252
        $ret = $this->phpcode->getPhpCodeConditions($isset, '', '', $get);
1253
        $ret .= $this->getXoopsCodeSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
1254
        $handlerInsert = $this->$this->getXoopsCodeHandler($tableName, $tableName, false, true, false, 'Obj');
1255
        $redirect = $this->getXoopsCodeRedirectHeader($tableName, '?op=list', 2, "{$language}FORM_UPDATE_OK");
1256
        $ret .= $this->phpcode->getPhpCodeConditions($handlerInsert, '', '', $redirect);
1257
1258
        $ret .= $this->getXoopsCodeTplAssign('error', "\${$tableName}Obj->getHtmlErrors()");
1259
1260
        return $this->phpcode->getPhpCodeCaseSwitch('update', $ret);
1261
    }
1262
1263
    /**
1264
     *  @public function getXoopsCodeSaveFieldId
1265
     *
1266
     *  @param $fields
1267
     *
1268
     *  @return string
1269
     */
1270 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...
1271
    {
1272
        foreach (array_keys($fields) as $f) {
1273
            if (0 == $f) {
1274
                $fieldId = $fields[$f]->getVar('field_name');
1275
            }
1276
        }
1277
1278
        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...
1279
    }
1280
1281
    /**
1282
     *  @public function getXoopsCodeSaveFieldMain
1283
     *
1284
     *  @param $fields
1285
     *
1286
     *  @return string
1287
     */
1288 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...
1289
    {
1290
        foreach (array_keys($fields) as $f) {
1291
            if (1 == $fields[$f]->getVar('field_main')) {
1292
                $fieldMain = $fields[$f]->getVar('field_name');
1293
            }
1294
        }
1295
1296
        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...
1297
    }
1298
1299
    /**
1300
     *  @public function getXoopsCodeSaveElements
1301
     *
1302
     *  @param $moduleDirname
1303
     *  @param $tableName
1304
     *  @param $fields
1305
     *
1306
     *  @return string
1307
     */
1308 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...
1309
    {
1310
        $ret = '';
1311
        foreach (array_keys($fields) as $f) {
1312
            $fieldName = $fields[$f]->getVar('field_name');
1313
            $fieldElement = $fields[$f]->getVar('field_element');
1314
            if (1 == $fields[$f]->getVar('field_main')) {
1315
                $fieldMain = $fieldName;
1316
            }
1317
            if ((5 == $fieldElement) || (6 == $fieldElement)) {
1318
                $ret .= $this->getXoopsCodeCheckBoxOrRadioYNSetVar($tableName, $fieldName);
1319
            } elseif (13 == $fieldElement) {
1320
                $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...
1321
            } elseif (14 == $fieldElement) {
1322
                $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...
1323
            } elseif (15 == $fieldElement) {
1324
                $ret .= $this->getXoopsCodeTextDateSelectSetVar($tableName, $fieldName);
1325
            } else {
1326
                $ret .= $this->getXoopsCodeSetVar($tableName, $fieldName, "\$_POST['{$fieldName}']");
1327
            }
1328
        }
1329
1330
        return $ret;
1331
    }
1332
1333
    /*
1334
    *  @public function getXoopsCodePageNav
1335
    *  @param $tableName
1336
    *
1337
    *  @return string
1338
    */
1339
    public function getXoopsCodePageNav($tableName)
1340
    {
1341
        $condition = $this->phpcode->getPhpCodeCommentLine('Display Navigation');
1342
        $condition .= $this->phpcode->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'class/pagenav', true);
1343
        $condition .= "\$pagenav = new XoopsPageNav(\${$tableName}Count, \$limit, \$start, 'start', 'op=list&limit=' . \$limit);\n";
1344
        $condition .= $this->getXoopsCodeTplAssign('pagenav', '$pagenav->renderNav(4)');
1345
        $ret = $this->phpcode->getPhpCodeConditions("\${$tableName}Count", ' > ', '$limit', $condition, false, "\t\t");
1346
1347
        return $ret;
1348
    }
1349
}
1350