Completed
Push — master ( 3f0046...b1c777 )
by Gino
06:41 queued 03:19
created

ClassFiles::getClassInsert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 22
rs 9.2
cc 1
eloc 5
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: ClassFiles.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class ClassFiles.
27
 */
28
class ClassFiles extends TDMCreateFile
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 string
32
    */
33
    private $phpcode = null;
34
35
    /*
36
    * @var string
37
    */
38
    private $tdmcfile = null;
39
40
    /*
41
    * @var string
42
    */
43
    private $formelements = null;
44
45
    /*
46
    *  @public function constructor
47
    *  @param null
48
    */
49
    /**
50
     *
51
     */
52
    public function __construct()
53
    {
54
        parent::__construct();
55
        $this->phpcode = TDMCreatePhpCode::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like \TDMCreatePhpCode::getInstance() of type object<TDMCreatePhpCode> is incompatible with the declared type string of property $phpcode.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
        $this->tdmcfile = TDMCreateFile::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like \TDMCreateFile::getInstance() of type object<TDMCreateFile> is incompatible with the declared type string of property $tdmcfile.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
        $this->tdmcreate = TDMCreateHelper::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like \TDMCreateHelper::getInstance() of type object<TDMCreateHelper> is incompatible with the declared type string of property $tdmcreate.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
        $this->formelements = ClassFormElements::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like \ClassFormElements::getInstance() of type object<ClassFormElements> is incompatible with the declared type string of property $formelements.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59
    }
60
61
    /*
62
    *  @static function &getInstance
63
    *  @param null
64
    */
65
    /**
66
     * @return ClassFiles
67
     */
68
    public static function &getInstance()
69
    {
70
        static $instance = false;
71
        if (!$instance) {
72
            $instance = new self();
73
        }
74
75
        return $instance;
76
    }
77
78
    /*
79
    *  @public function write
80
    *  @param string $module
81
    *  @param string $table
82
    *  @param mixed $tables
83
    */
84
    /**
85
     * @param $module
86
     * @param $table
87
     * @param $tables
88
     */
89
    public function write($module, $table, $tables)
90
    {
91
        $this->setModule($module);
92
        $this->setTable($table);
93
        $this->setTables($tables);
94
    }
95
96
    /*
97
    *  @private function getInitVar
98
    *  @param string $fieldName
99
    *  @param string $type
100
    */
101
    /**
102
     * @param        $fieldName
103
     * @param string $type
104
     *
105
     * @return string
106
     */
107
    private function getInitVar($fieldName, $type = 'INT')
108
    {
109
        $ret = <<<EOT
110
        \$this->initVar('{$fieldName}', XOBJ_DTYPE_{$type});\n
111
EOT;
112
113
        return $ret;
114
    }
115
116
    /*
117
    *  @private function getInitVars
118
    *  @param array $fields
119
    */
120
    /**
121
     * @param $fields
122
     *
123
     * @return string
124
     */
125
    private function getInitVars($fields)
126
    {
127
        $ret = '';
128
        // Creation of the initVar functions list
129
        foreach (array_keys($fields) as $f) {
130
            $fieldName = $fields[$f]->getVar('field_name');
131
            $fieldType = $fields[$f]->getVar('field_type');
132
            if ($fieldType > 1) {
133
                $fType = $this->tdmcreate->getHandler('fieldtype')->get($fieldType);
0 ignored issues
show
Bug introduced by
The method getHandler cannot be called on $this->tdmcreate (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
134
                $fieldTypeName = $fType->getVar('fieldtype_name');
0 ignored issues
show
Unused Code introduced by
$fieldTypeName 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...
135
            } else {
136
                $fieldType = null;
137
            }
138
            switch ($fieldType) {
139
                case 2:
140
                case 3:
141
                case 4:
142
                case 5:
143
                    $ret .= $this->getInitVar($fieldName, 'INT');
144
                    break;
145
                case 6:
146
                    $ret .= $this->getInitVar($fieldName, 'FLOAT');
147
                    break;
148
                case 7:
149
                case 8:
150
                    $ret .= $this->getInitVar($fieldName, 'DECIMAL');
151
                    break;
152
                case 10:
153
                    $ret .= $this->getInitVar($fieldName, 'ENUM');
154
                    break;
155
                case 11:
156
                    $ret .= $this->getInitVar($fieldName, 'EMAIL');
157
                    break;
158
                case 12:
159
                    $ret .= $this->getInitVar($fieldName, 'URL');
160
                    break;
161
                case 13:
162
                case 14:
163
                    $ret .= $this->getInitVar($fieldName, 'TXTBOX');
164
                    break;
165
                case 15:
166
                case 16:
167
                case 17:
168
                case 18:
169
                    $ret .= $this->getInitVar($fieldName, 'TXTAREA');
170
                    break;
171
                case 19:
172
                case 20:
173
                case 21:
174
                case 22:
175
                case 23:
176
                    $ret .= $this->getInitVar($fieldName, 'LTIME');
177
                    break;
178
            }
179
        }
180
181
        return $ret;
182
    }
183
184
    /*
185
    *  @private function getHeadClass
186
    *  @param string $moduleDirname
187
    *  @param string $tableName
188
    *  @param array $fields
189
    */
190
    /**
191
     * @param $moduleDirname
192
     * @param $tableName
193
     * @param $fields
194
     *
195
     * @return string
196
     */
197
    private function getHeadClass($moduleDirname, $tableName, $fields)
198
    {
199
        $ucfModuleDirname = ucfirst($moduleDirname);
200
        $ucfTableName = ucfirst($tableName);
201
        $ret = $this->phpcode->getPhpCodeDefined();
0 ignored issues
show
Bug introduced by
The method getPhpCodeDefined cannot be called on $this->phpcode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
202
        $ret .= $this->phpcode->getPhpCodeCommentMultiLine(array('Class Object ' => $ucfModuleDirname.$ucfTableName));
0 ignored issues
show
Bug introduced by
The method getPhpCodeCommentMultiLine cannot be called on $this->phpcode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
203
        $ret .= <<<EOT
204
class {$ucfModuleDirname}{$ucfTableName} extends XoopsObject
205
{
206
    /*
207
    * @var mixed
208
    */
209
    private \${$moduleDirname} = null;\n\n
210
EOT;
211
        $fieldElementId = array();
212
        $optionsFieldName = array();
213
        foreach (array_keys($fields) as $f) {
214
            $fieldName = $fields[$f]->getVar('field_name');
215
            $fieldElement = $fields[$f]->getVar('field_element');
216
            $fieldElements = $this->tdmcreate->getHandler('fieldelements')->get($fieldElement);
0 ignored issues
show
Bug introduced by
The method getHandler cannot be called on $this->tdmcreate (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
217
            $fieldElementId[] = $fieldElements->getVar('fieldelement_id');
218
            $rpFieldName = $this->tdmcfile->getRightString($fieldName);
0 ignored issues
show
Bug introduced by
The method getRightString cannot be called on $this->tdmcfile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
219
            if (in_array(5, $fieldElementId)) {
220
                if (count($rpFieldName) % 5) {
221
                    $optionsFieldName[] = "'".$rpFieldName."'";
222
                } else {
223
                    $optionsFieldName[] = "'".$rpFieldName."'\n";
224
                }
225
            }
226
        }
227
        if (in_array(5, $fieldElementId) > 1) {
228
            $optionsElements = implode(', ', $optionsFieldName);
229
            $ret .= <<<EOT
230
	/**
231
     * Options
232
     */
233
	public \$options = array({$optionsElements});\n\n
234
EOT;
235
        }
236
        unset($fieldElementId, $optionsFieldName);
237
        $ret .= <<<EOT
238
	/*
239
     * Constructor
240
     *
241
     * @param null
242
     */
243
    public function __construct()
244
    {
245
        \$this->{$moduleDirname} = {$ucfModuleDirname}Helper::getInstance();
246
{$this->getInitVars($fields)}\t}\n
247
    /*
248
    *  @static function &getInstance
249
    *  @param null
250
    */
251
    public static function &getInstance()
252
    {
253
        static \$instance = false;
254
        if(!\$instance) {
255
            \$instance = new self();
256
        }
257
        return \$instance;
258
    }\n\n
259
EOT;
260
261
        return $ret;
262
    }
263
264
    /*
265
    *  @private function getHeadInForm
266
    *  @param string $module
267
    *  @param string $table
268
    */
269
    /**
270
     * @param $module
271
     * @param $table
272
     *
273
     * @return string
274
     */
275
    private function getHeadInForm($module, $table)
276
    {
277
        $moduleDirname = $module->getVar('mod_dirname');
278
        $tableName = $table->getVar('table_name');
279
        $tableSoleName = $table->getVar('table_solename');
280
        $tablePermissions = $table->getVar('table_permissions');
281
        $ucfTableName = ucfirst($tableName);
282
        $stuTableSoleName = strtoupper($tableSoleName);
283
        $language = $this->getLanguage($moduleDirname, 'AM');
284
        $this->formelements->initForm($module, $table);
0 ignored issues
show
Bug introduced by
The method initForm cannot be called on $this->formelements (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
285
        $ret = <<<EOT
286
    /*
287
     * Get form
288
     *
289
     * @param mixed \$action
290
     */
291
    public function getForm{$ucfTableName}(\$action = false)
292
    {
293
        if(\$action === false) {
294
            \$action = \$_SERVER['REQUEST_URI'];
295
        }\n
296
EOT;
297
        if (1 == $tablePermissions) {
298
            $ret .= <<<EOT
299
		global \$xoopsUser, \$xoopsModule;
300
		// Permissions for uploader
301
        \$gpermHandler =& xoops_gethandler('groupperm');
302
        \$groups = is_object(\$xoopsUser) ? \$xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
303
        if(\$xoopsUser) {
304
            if( !\$xoopsUser->isAdmin(\$xoopsModule->mid()) ) {
305
                \$permissionUpload =(\$gpermHandler->checkRight('{$moduleDirname}_ac', 32, \$groups, \$xoopsModule->getVar('mid'))) ? true : false ;
306
            }else{
307
                \$permissionUpload = true;
308
            }
309
        }else{
310
            \$permissionUpload = (\$gpermHandler->checkRight('{$moduleDirname}_ac', 32, \$groups, \$xoopsModule->getVar('mid'))) ? true : false ;
311
        }\n
312
EOT;
313
        }
314
        $ret .= <<<EOT
315
		// Title
316
        \$title = \$this->isNew() ? sprintf({$language}{$stuTableSoleName}_ADD) : sprintf({$language}{$stuTableSoleName}_EDIT);
317
        // Get Theme Form
318
        xoops_load('XoopsFormLoader');
319
        \$form = new XoopsThemeForm(\$title, 'form', \$action, 'post', true);
320
        \$form->setExtra('enctype="multipart/form-data"');\n
321
EOT;
322
323
        if (0 == $table->getVar('table_category')) {
324
            $ret .= <<<EOT
325
		// {$ucfTableName} handler
326
		//\${$tableName}Handler =& \$this->{$moduleDirname}->getHandler('{$tableName}');\n
327
EOT;
328
        }
329
330
        $ret .= <<<EOT
331
{$this->formelements->renderElements()}
0 ignored issues
show
Bug introduced by
The method renderElements cannot be called on $this->formelements (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
332
EOT;
333
334
        return $ret;
335
    }
336
337
    /*
338
    *  @private function getPermissionsInForm
339
    *  @param string $moduleDirname
340
    *  @param string $fieldId
341
    */
342
    /**
343
     * @param $moduleDirname
344
     * @param $fieldId
345
     *
346
     * @return string
347
     */
348
    private function getPermissionsInForm($moduleDirname, $fieldId)
349
    {
350
        $permissionApprove = $this->tdmcfile->getLanguage($moduleDirname, 'AM', 'PERMISSIONS_APPROVE');
0 ignored issues
show
Bug introduced by
The method getLanguage cannot be called on $this->tdmcfile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
351
        $permissionSubmit = $this->tdmcfile->getLanguage($moduleDirname, 'AM', 'PERMISSIONS_SUBMIT');
0 ignored issues
show
Bug introduced by
The method getLanguage cannot be called on $this->tdmcfile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
352
        $permissionView = $this->tdmcfile->getLanguage($moduleDirname, 'AM', 'PERMISSIONS_VIEW');
0 ignored issues
show
Bug introduced by
The method getLanguage cannot be called on $this->tdmcfile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
353
        $ret = <<<EOT
354
        // Permissions
355
        \$memberHandler = & xoops_gethandler( 'member' );
356
        \$groupList     = \$memberHandler->getGroupList();
357
        \$gpermHandler  = &xoops_gethandler( 'groupperm' );
358
        \$fullList = array_keys( \$groupList );
359
        global \$xoopsModule;
360
        if( !\$this->isNew() ) {
361
            \$groupsIdsApprove = \$gpermHandler->getGroupIds( '{$moduleDirname}_approve', \$this->getVar( '{$fieldId}' ), \$xoopsModule->getVar( 'mid' ) );
362
            \$groupsIdsSubmit = \$gpermHandler->getGroupIds( '{$moduleDirname}_submit', \$this->getVar( '{$fieldId}' ), \$xoopsModule->getVar( 'mid' ) );
363
            \$groupsIdsView = \$gpermHandler->getGroupIds( '{$moduleDirname}_view', \$this->getVar( '{$fieldId}' ), \$xoopsModule->getVar( 'mid' ) );
364
            \$groupsIdsApprove = array_values( \$groupsIdsApprove );
365
            \$groupsCanApproveCheckbox = new XoopsFormCheckBox( {$permissionApprove}, 'groups_approve[]', \$groupsIdsApprove );
366
            \$groupsIdsSubmit = array_values( \$groupsIdsSubmit );
367
            \$groupsCanSubmitCheckbox = new XoopsFormCheckBox( {$permissionSubmit}, 'groups_submit[]', \$groupsIdsSubmit );
368
            \$groupsIdsView = array_values( \$groupsIdsView );
369
            \$groupsCanViewCheckbox = new XoopsFormCheckBox( {$permissionView}, 'groups_view[]', \$groupsIdsView );
370
        } else {
371
            \$groupsCanApproveCheckbox = new XoopsFormCheckBox( {$permissionApprove}, 'groups_approve[]', \$fullList );
372
            \$groupsCanSubmitCheckbox = new XoopsFormCheckBox( {$permissionSubmit}, 'groups_submit[]', \$fullList );
373
            \$groupsCanViewCheckbox = new XoopsFormCheckBox( {$permissionView}, 'groups_view[]', \$fullList );
374
        }
375
        // For approve
376
        \$groupsCanApproveCheckbox->addOptionArray( \$groupList );
377
        \$form->addElement( \$groupsCanApproveCheckbox );
378
        // For submit
379
        \$groupsCanSubmitCheckbox->addOptionArray( \$groupList );
380
        \$form->addElement( \$groupsCanSubmitCheckbox );
381
        // For view
382
        \$groupsCanViewCheckbox->addOptionArray( \$groupList );
383
        \$form->addElement( \$groupsCanViewCheckbox );\n\n
384
EOT;
385
386
        return $ret;
387
    }
388
389
    /*
390
    *  @private function getFootInForm
391
    *  @param null
392
    */
393
    /**
394
     * @return string
395
     */
396
    private function getFootInForm()
397
    {
398
        $ret = <<<EOT
399
        // Send
400
        \$form->addElement(new XoopsFormHidden('op', 'save'));
401
        \$form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
402
        return \$form;
403
    }\n\n
404
EOT;
405
406
        return $ret;
407
    }
408
409
    /*
410
    *  @private function getToArray
411
    *  @param null
412
    */
413
    /**
414
     * @return string
415
     */
416
    private function getValuesInForm($moduleDirname, $table, $fields)
417
    {
418
        $stuModuleDirname = strtoupper($moduleDirname);
0 ignored issues
show
Unused Code introduced by
$stuModuleDirname 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...
419
        $ucfTableName = ucfirst($table->getVar('table_name'));
420
        $ret = <<<EOT
421
	/**
422
     * Get Values
423
     */
424
	public function getValues{$ucfTableName}(\$keys = null, \$format = null, \$maxDepth = null)
425
    {
426
		\$ret = parent::getValues(\$keys, \$format, \$maxDepth);\n
427
EOT;
428
        foreach (array_keys($fields) as $f) {
429
            $fieldName = $fields[$f]->getVar('field_name');
430
            $fieldElement = $fields[$f]->getVar('field_element');
431
            $rpFieldName = $this->tdmcfile->getRightString($fieldName);
0 ignored issues
show
Bug introduced by
The method getRightString cannot be called on $this->tdmcfile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
432
            switch ($fieldElement) {
433
                case 3:
434
                case 4:
435
                    $ret .= <<<EOT
436
		\$ret['{$rpFieldName}'] = strip_tags(\$this->getVar('{$fieldName}'));\n
437
EOT;
438
                break;
439
                case 8:
440
                    $ret .= <<<EOT
441
		\$ret['{$rpFieldName}'] = XoopsUser::getUnameFromId(\$this->getVar('{$fieldName}'));\n
442
EOT;
443
                break;
444
                /*case 10:
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
445
                    $ret .= <<<EOT
446
        \$ret['{$rpFieldName}'] = XOOPS_ICONS32_URL .'/'. \$this->getVar('{$fieldName}');\n
447
EOT;
448
                break;
449
                case 13:
450
                    $ret .= <<<EOT
451
        \$ret['{$rpFieldName}'] = {$stuModuleDirname}_UPLOAD_IMAGE_URL .'/'. \$this->getVar('{$fieldName}');\n
452
EOT;
453
                break;*/
454
                case 15:
455
                    $ret .= <<<EOT
456
		\$ret['{$rpFieldName}'] = formatTimeStamp(\$this->getVar('{$fieldName}'));\n
457
EOT;
458
                break;
459
                default:
460
                    if ($fieldElement > 15) {
461
                        $fieldElements = $this->tdmcreate->getHandler('fieldelements')->get($fieldElement);
0 ignored issues
show
Bug introduced by
The method getHandler cannot be called on $this->tdmcreate (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
462
                        $fieldElementMid = $fieldElements->getVar('fieldelement_mid');
463
                        $fieldElementTid = $fieldElements->getVar('fieldelement_tid');
464
                        $fieldElementName = $fieldElements->getVar('fieldelement_name');
465
                        $fieldNameDesc = substr($fieldElementName, strrpos($fieldElementName, ':'), strlen($fieldElementName));
466
                        $topicTableName = str_replace(': ', '', strtolower($fieldNameDesc));
467
                        $fieldsTopics = $this->tdmcfile->getTableFields($fieldElementMid, $fieldElementTid);
0 ignored issues
show
Bug introduced by
The method getTableFields cannot be called on $this->tdmcfile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
468
                        foreach (array_keys($fieldsTopics) as $f) {
469
                            $fieldNameTopic = $fieldsTopics[$f]->getVar('field_name');
470
                            if (1 == $fieldsTopics[$f]->getVar('field_main')) {
471
                                $fieldMainTopic = $fieldNameTopic;
472
                            }
473
                        }
474
                        $ret .= <<<EOT
475
		\$ret['{$rpFieldName}'] = \$this->{$moduleDirname}->getHandler('{$topicTableName}')->get(\$this->getVar('{$fieldName}'))->getVar('{$fieldMainTopic}');\n
0 ignored issues
show
Bug introduced by
The variable $fieldMainTopic 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...
476
EOT;
477
                    } else {
478
                        $ret .= <<<EOT
479
		\$ret['{$rpFieldName}'] = \$this->getVar('{$fieldName}');\n
480
EOT;
481
                    }
482
                break;
483
            }
484
        }
485
        $ret .= <<<EOT
486
487
		return \$ret;
488
    }\n\n
489
EOT;
490
491
        return $ret;
492
    }
493
    /*
494
    *  @private function getToArray
495
    *  @param $table
496
    */
497
    /**
498
     * @return string
499
     */
500
    private function getToArrayInForm($table)
501
    {
502
        $tableName = $table->getVar('table_name');
503
        $ucfTableName = ucfirst($tableName);
504
        $ret = <<<EOT
505
    /**
506
     * Returns an array representation of the object
507
     *
508
     * @return array
509
     **/
510
    public function toArray{$ucfTableName}()
511
    {
512
        \$ret = array();
513
        \$vars = \$this->getVars();
514
        foreach( array_keys( \$vars ) as \$var ) {
515
            \$ret[\$var] = \$this->getVar( \$var );
516
        }
517
        return \$ret;
518
    }
519
}\n\n
520
EOT;
521
522
        return $ret;
523
    }
524
525
    /*
526
    *  @private function getOptionsCheck
527
    *  @param $table
528
    */
529
    /**
530
     * @return string
531
     */
532
    private function getOptionsCheck($table)
533
    {
534
        $tableName = $table->getVar('table_name');
535
        $ucfTableName = ucfirst($tableName);
536
        $ret = <<<EOT
537
    /**
538
     * Get Options
539
     */
540
	public function getOptions{$ucfTableName}()
541
    {
542
        \$ret = array();\n
543
EOT;
544
        $fields = $this->tdmcfile->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
0 ignored issues
show
Bug introduced by
The method getTableFields cannot be called on $this->tdmcfile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
545
        foreach (array_keys($fields) as $f) {
546
            $fieldName = $fields[$f]->getVar('field_name');
547
            $fieldElement = $fields[$f]->getVar('field_element');
548
            //
549
            $fieldElements = $this->tdmcreate->getHandler('fieldelements')->get($fieldElement);
0 ignored issues
show
Bug introduced by
The method getHandler cannot be called on $this->tdmcreate (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
550
            $fieldElementId = $fieldElements->getVar('fieldelement_id');
551
            $rpFieldName = $this->tdmcfile->getRightString($fieldName);
0 ignored issues
show
Bug introduced by
The method getRightString cannot be called on $this->tdmcfile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
552
            if (5 == $fieldElementId) {
553
                $ret .= <<<EOT
554
		if(1 == \$this->getVar('{$fieldName}')) {
555
            array_push(\$ret, '{$rpFieldName}');
556
        }\n
557
EOT;
558
            }
559
        }
560
        $ret .= <<<EOT
561
        return \$ret;
562
    }
563
}\n\n
564
EOT;
565
566
        return $ret;
567
    }
568
569
    /*
570
    *  @public function getClassHandler
571
    *  @param string $moduleDirname
572
    *  @param string $tableName
573
    *  @param string $fieldId
574
    *  @param string $fieldMain
575
    */
576
    /**
577
     * @param $moduleDirname
578
     * @param $tableName
579
     * @param $tableCategory
580
     * @param $tableFieldname
581
     * @param $fieldId
582
     * @param $fieldMain
583
     *
584
     * @return string
585
     */
586
    private function getClassObjectHandler($moduleDirname, $table, $fieldId, $fieldMain)
587
    {
588
        $tableName = $table->getVar('table_name');
589
        $tableCategory = $table->getVar('table_category');
0 ignored issues
show
Unused Code introduced by
$tableCategory 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...
590
        $tableSoleName = $table->getVar('table_solename');
591
        $tableFieldname = $table->getVar('table_fieldname');
0 ignored issues
show
Unused Code introduced by
$tableFieldname 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...
592
        $ucfModuleDirname = ucfirst($moduleDirname);
593
        $ucfTableName = ucfirst($tableName);
594
        $ucfTableSoleName = ucfirst($tableSoleName);
0 ignored issues
show
Unused Code introduced by
$ucfTableSoleName 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...
595
        $ucfModuleTable = $ucfModuleDirname.$ucfTableName;
596
        $ret = <<<EOT
597
/*
598
 * Class Object Handler {$ucfModuleDirname}{$ucfTableName}
599
 */
600
class {$ucfModuleTable}Handler extends XoopsPersistableObjectHandler
601
{
602
    /*
603
    * @var mixed
604
    */
605
    private \${$moduleDirname} = null;
606
	/*
607
     * Constructor
608
     *
609
     * @param string \$db
610
     */
611
    public function __construct(&\$db)
612
    {
613
        parent::__construct(\$db, '{$moduleDirname}_{$tableName}', '{$moduleDirname}{$tableName}', '{$fieldId}', '{$fieldMain}');
614
		\$this->{$moduleDirname} = {$ucfModuleDirname}Helper::getInstance();
615
    }\n\n
616
EOT;
617
618
        return $ret;
619
    }
620
621
    /**
622
     *  @public function getClassCreate
623
     *
624
     *  @return string
625
     */
626
    private function getClassCreate()
627
    {
628
        $ret = <<<EOT
629
	/**
630
     * @param bool \$isNew
631
     *
632
     * @return object
633
     */
634
    public function &create(\$isNew = true)
635
    {
636
        return parent::create(\$isNew);
637
    }\n\n
638
EOT;
639
640
        return $ret;
641
    }
642
643
    /**
644
     *  @public function getClassGet
645
     *
646
     *  @return string
647
     */
648
    private function getClassGet()
649
    {
650
        $ret = <<<EOT
651
	/**
652
     * retrieve a field
653
     *
654
     * @param int \$i field id
655
     * @return mixed reference to the {@link TDMCreateFields} object
656
     */
657
    public function &get(\$i = null, \$fields = null)
658
    {
659
        return parent::get(\$i, \$fields);
660
    }\n\n
661
EOT;
662
663
        return $ret;
664
    }
665
666
    /**
667
     *  @public function getClassGetInsertId
668
     *
669
     *  @return string
670
     */
671
    private function getClassGetInsertId()
672
    {
673
        $ret = <<<EOT
674
    /**
675
     * get inserted id
676
     *
677
     * @param null
678
     * @return integer reference to the {@link TDMCreateFields} object
679
     */
680
    public function &getInsertId()
681
    {
682
        return \$this->db->getInsertId();
683
    }\n\n
684
EOT;
685
686
        return $ret;
687
    }
688
689
    /**
690
     *  @public function getClassGetIds
691
     *
692
     *  @return string
693
     */
694
    private function getClassGetIds()
695
    {
696
        $ret = <<<EOT
697
	/**
698
     * get IDs of objects matching a condition
699
     *
700
     * @param object \$criteria {@link CriteriaElement} to match
701
     * @return array of object IDs
702
     */
703
    public function &getIds(\$criteria)
704
    {
705
        return parent::getIds(\$criteria);
706
    }\n\n
707
EOT;
708
709
        return $ret;
710
    }
711
712
    /**
713
     *  @public function getClassInsert
714
     *
715
     *  @return string
716
     */
717
    private function getClassInsert()
718
    {
719
        $ret = <<<EOT
720
	/**
721
     * insert a new field in the database
722
     *
723
     * @param object \$field reference to the {@link TDMCreateFields} object
724
     * @param bool \$force
725
     *
726
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
727
     */
728
    public function &insert(&\$field, \$force = false)
729
    {
730
        if(!parent::insert(\$field, \$force)) {
731
            return false;
732
        }
733
        return true;
734
    }\n\n
735
EOT;
736
737
        return $ret;
738
    }
739
740
    /**
741
     *  @public function getClassCounter
742
     *
743
     *  @param $tableName
744
     *  @param $fieldId
745
     *  @param $fieldMain
746
     *
747
     *  @return string
748
     */
749
    private function getClassCounter($tableName, $fieldId, $fieldMain)
750
    {
751
        $ucfTableName = ucfirst($tableName);
752
        $ret = <<<EOT
753
    /**
754
     * Get Count {$ucfTableName}
755
     */
756
    public function getCount{$ucfTableName}(\$start = 0, \$limit = 0, \$sort = '{$fieldId} ASC, {$fieldMain}', \$order = 'ASC')
757
    {
758
        \$criteriaCount{$ucfTableName} = new CriteriaCompo();
759
        \$criteriaCount{$ucfTableName} = \$this->get{$ucfTableName}Criteria(\$criteriaCount{$ucfTableName}, \$start, \$limit, \$sort, \$order);
760
		return parent::getCount(\$criteriaCount{$ucfTableName});
761
    }\n\n
762
EOT;
763
764
        return $ret;
765
    }
766
767
    /**
768
     *  @public function getClassAll
769
     *
770
     *  @param $tableName
771
     *  @param $fieldId
772
     *  @param $fieldMain
773
     *
774
     *  @return string
775
     */
776
    private function getClassAll($tableName, $fieldId, $fieldMain)
777
    {
778
        $ucfTableName = ucfirst($tableName);
779
        $ret = <<<EOT
780
	/**
781
     * Get All {$ucfTableName}
782
     */
783
	public function getAll{$ucfTableName}(\$start = 0, \$limit = 0, \$sort = '{$fieldId} ASC, {$fieldMain}', \$order = 'ASC')
784
    {
785
        \$criteriaAll{$ucfTableName} = new CriteriaCompo();
786
        \$criteriaAll{$ucfTableName} = \$this->get{$ucfTableName}Criteria(\$criteriaAll{$ucfTableName}, \$start, \$limit, \$sort, \$order);
787
        return parent::getAll(\$criteriaAll{$ucfTableName});
788
    }\n\n
789
EOT;
790
791
        return $ret;
792
    }
793
794
    /**
795
     *  @public function getClassByCategory
796
     *
797
     *  @param $tableName
798
     *  @param $fieldId
799
     *  @param $fieldMain
800
     *  @param $fieldParent
801
     *
802
     *  @return string
803
     */
804
    private function getClassByCategory($moduleDirname, $tableName, $tableFieldName, $fieldId, $fieldName, $fieldMain, $fieldParent, $fieldElement)
805
    {
806
        $ucfTableName = ucfirst($tableName);
807
        $fieldElements = $this->tdmcreate->getHandler('fieldelements')->get($fieldElement);
0 ignored issues
show
Bug introduced by
The method getHandler cannot be called on $this->tdmcreate (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
808
        $fieldElementMid = $fieldElements->getVar('fieldelement_mid');
0 ignored issues
show
Unused Code introduced by
$fieldElementMid 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...
809
        $fieldElementTid = $fieldElements->getVar('fieldelement_tid');
0 ignored issues
show
Unused Code introduced by
$fieldElementTid 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...
810
        $fieldElementName = $fieldElements->getVar('fieldelement_name');
811
        $fieldNameDesc = ucfirst(substr($fieldElementName, strrpos($fieldElementName, ':'), strlen($fieldElementName)));
812
        $topicTableName = str_replace(': ', '', $fieldNameDesc);
813
        $lcfTopicTableName = lcfirst($topicTableName);
814
        $ret = <<<EOT
815
	/**
816
     * Get All {$ucfTableName} By {$fieldNameDesc} Id
817
     */
818
	public function getAll{$ucfTableName}By{$fieldNameDesc}Id(\${$tableFieldName}Id, \$start = 0, \$limit = 0, \$sort = '{$fieldId} ASC, {$fieldMain}', \$order = 'ASC')
819
    {
820
        \$gpermHandler =& xoops_gethandler('groupperm');
821
		\${$lcfTopicTableName} = \$gpermHandler->getItemIds('{$moduleDirname}_view', \$GLOBALS['xoopsUser']->getGroups(), \$GLOBALS['xoopsModule']->getVar('mid') );
822
823
		\$criteriaAll{$ucfTableName} = new CriteriaCompo();\n
824
EOT;
825
        if (strstr($fieldName, 'status')) {
826
            $ret .= <<<EOT
827
		\$criteriaAll{$ucfTableName}->add(new Criteria('{$fieldName}', 0, '!='));\n
828
EOT;
829
        }
830
        $ret .= <<<EOT
831
		\$criteriaAll{$ucfTableName}->add(new Criteria('{$fieldParent}', \${$tableFieldName}Id));
832
		\$criteriaAll{$ucfTableName}->add(new Criteria('{$fieldId}', '(' . implode(',', \${$lcfTopicTableName}) . ')','IN'));
833
        \$criteriaAll{$ucfTableName} = \$this->get{$ucfTableName}Criteria(\$criteriaAll{$ucfTableName}, \$start, \$limit, \$sort, \$order);
834
        return parent::getAll(\$criteriaAll{$ucfTableName});
835
    }\n\n
836
EOT;
837
838
        return $ret;
839
    }
840
841
    /**
842
     *  @public function getClassCriteria
843
     *
844
     *  @param $tableName
845
     *  @param $fieldId
846
     *  @param $fieldMain
847
     *
848
     *  @return string
849
     */
850
    private function getClassCriteria($tableName)
851
    {
852
        $ucfTableName = ucfirst($tableName);
853
        $ret = <<<EOT
854
	/**
855
     * Get {$ucfTableName} Criteria.
856
     */
857
    private function get{$ucfTableName}Criteria(\$criteria{$ucfTableName}, \$start, \$limit, \$sort, \$order)
858
    {
859
        \$criteria{$ucfTableName}->setStart(\$start);
860
        \$criteria{$ucfTableName}->setLimit(\$limit);
861
        \$criteria{$ucfTableName}->setSort(\$sort);
862
        \$criteria{$ucfTableName}->setOrder(\$order);
863
864
        return \$criteria{$ucfTableName};
865
    }\n\n
866
EOT;
867
868
        return $ret;
869
    }
870
871
    /**
872
     *  @public function getClassGetTableSolenameById
873
     *
874
     *  @param $moduleDirname
875
     *  @param $table
876
     *
877
     *  @return string
878
     */
879
    private function getClassGetTableSolenameById($moduleDirname, $table, $fieldMain)
880
    {
881
        $tableName = $table->getVar('table_name');
882
        $tableSoleName = $table->getVar('table_solename');
883
        $ucfTableSoleName = ucfirst($tableSoleName);
884
        $ret = <<<EOT
885
	/**
886
     * Returns the {$ucfTableSoleName} from id
887
     *
888
     * @return string
889
     **/
890
    public function get{$ucfTableSoleName}FromId(\${$tableSoleName}Id)
891
    {
892
        \${$tableSoleName}Id = (int) ( \${$tableSoleName}Id );
893
        \${$tableSoleName} = '';
894
        if(\${$tableSoleName}Id > 0) {
895
            \${$tableName}Handler = \$this->{$moduleDirname}->getHandler( '{$tableName}' );
896
            \${$tableSoleName}Obj = & \${$tableName}Handler->get( \${$tableSoleName}Id );
897
            if(is_object( \${$tableSoleName}Obj )) {
898
                \${$tableSoleName} = \${$tableSoleName}Obj->getVar( '{$fieldMain}' );
899
            }
900
        }
901
        return \${$tableSoleName};
902
    }\n
903
EOT;
904
905
        return $ret;
906
    }
907
908
    /**
909
     *  @public function getClassEnd
910
     */
911
    private function getClassEnd()
912
    {
913
        $ret = <<<EOT
914
}\n
915
EOT;
916
917
        return $ret;
918
    }
919
920
    /*
921
    *  @public function renderFile
922
    *  @param string $filename
923
    */
924
    /**
925
     * @param $filename
926
     *
927
     * @return bool|string
928
     */
929
    public function renderFile($filename)
930
    {
931
        $module = $this->getModule();
932
        $table = $this->getTable();
933
        $tableName = $table->getVar('table_name');
934
        $tableFieldName = $table->getVar('table_fieldname');
935
        $tableCategory = $table->getVar('table_category');
0 ignored issues
show
Unused Code introduced by
$tableCategory 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...
936
        $moduleDirname = $module->getVar('mod_dirname');
937
        $fields = $this->tdmcfile->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
0 ignored issues
show
Bug introduced by
The method getTableFields cannot be called on $this->tdmcfile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
938
        $fieldInForm = array();
939
        $fieldParentId = array();
940
        $fieldElementId = array();
941
        foreach (array_keys($fields) as $f) {
942
            $fieldName = $fields[$f]->getVar('field_name');
943
            $fieldInForm[] = $fields[$f]->getVar('field_inform');
944
            $fieldParentId[] = $fields[$f]->getVar('field_parent');
945
            if ((0 == $f) && (1 == $table->getVar('table_autoincrement'))) {
946
                $fieldId = $fieldName; // $fieldId = fields parameter index field
947
            }
948
            if (1 == $fields[$f]->getVar('field_main')) {
949
                $fieldMain = $fieldName; // $fieldMain = fields parameter main field
950
            }
951
            if (1 == $fields[$f]->getVar('field_parent')) {
952
                $fieldParent = $fieldName; // $fieldParent = fields parameter parent field
953
            }
954
            $fieldElement = $fields[$f]->getVar('field_element');
955
            //
956
            $fieldElements = $this->tdmcreate->getHandler('fieldelements')->get($fieldElement);
0 ignored issues
show
Bug introduced by
The method getHandler cannot be called on $this->tdmcreate (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
957
            $fieldElementId[] = $fieldElements->getVar('fieldelement_id');
958
        }
959
        $content = $this->getHeaderFilesComments($module, $filename);
960
        $content .= $this->getHeadClass($moduleDirname, $tableName, $fields);
961
        if (in_array(1, $fieldInForm)) {
962
            $content .= $this->getHeadInForm($module, $table);
963
            if (1 == $table->getVar('table_permissions')) {
964
                $content .= $this->getPermissionsInForm($moduleDirname, $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...
965
            }
966
            $content .= $this->getFootInForm();
967
        }
968
        $content .= $this->getValuesInForm($moduleDirname, $table, $fields);
969
        $content .= $this->getToArrayInForm($table);
970
        if (in_array(5, $fieldElementId) > 1) {
971
            $content .= $this->getOptionsCheck($table);
972
        }
973
        $content .= $this->getClassObjectHandler($moduleDirname, $table, $fieldId, $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...
974
        $content .= $this->getClassCreate();
975
        $content .= $this->getClassGet();
976
        $content .= $this->getClassGetInsertId();
977
        $content .= $this->getClassGetIds();
978
        $content .= $this->getClassInsert();
979
        $content .= $this->getClassCounter($tableName, $fieldId, $fieldMain);
980
        $content .= $this->getClassAll($tableName, $fieldId, $fieldMain);
981
        $content .= $this->getClassCriteria($tableName);
982
        if (in_array(1, $fieldParentId) && $fieldElement > 15) {
983
            $content .= $this->getClassByCategory($moduleDirname, $tableName, $tableFieldName, $fieldId, $fieldName, $fieldMain, $fieldParent, $fieldElement);
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...
Bug introduced by
The variable $fieldParent 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 variable $fieldElement 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...
984
            $content .= $this->getClassGetTableSolenameById($moduleDirname, $table, $fieldMain);
985
        }
986
        $content .= $this->getClassEnd();
987
        $this->tdmcfile->create($moduleDirname, 'class', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
0 ignored issues
show
Bug introduced by
The method create cannot be called on $this->tdmcfile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
988
989
        return $this->tdmcfile->renderFile();
0 ignored issues
show
Bug introduced by
The method renderFile cannot be called on $this->tdmcfile (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
990
    }
991
}
992