Passed
Push — master ( 0c230a...202965 )
by Michael
02:53
created

ClassFiles::getInitVars()   D

Complexity

Conditions 24
Paths 45

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 24
nc 45
nop 1
dl 0
loc 59
rs 4.1666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 * tc module.
14
 *
15
 * @copyright       XOOPS Project (https://xoops.org)
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
29
{
30
    /**
31
     *  @public function constructor
32
     *
33
     *  @param null
34
     */
35
    public function __construct()
36
    {
37
        parent::__construct();
38
    }
39
40
    /**
41
     *  @static function getInstance
42
     *
43
     *  @param null
44
     *
45
     * @return ClassFiles
46
     */
47
    public static function getInstance()
48
    {
49
        static $instance = false;
50
        if (!$instance) {
51
            $instance = new self();
52
        }
53
54
        return $instance;
55
    }
56
57
    /**
58
     * @public function write
59
     *
60
     * @param string $module
61
     * @param string $table
62
     * @param mixed  $tables
63
     * @param        $filename
64
     */
65
    public function write($module, $table, $tables, $filename)
66
    {
67
        $this->setModule($module);
68
        $this->setTable($table);
69
        $this->setTables($tables);
70
        $this->setFileName($filename);
71
    }
72
73
    /**
74
     *  @private function getInitVar
75
     *
76
     *  @param string $fieldName
77
     *  @param string $type
78
     *
79
     * @return string
80
     */
81
    private function getInitVar($fieldName, $type = 'INT')
82
    {
83
        $cc = ClassXoopsCode::getInstance();
84
85
        return $cc->getClassInitVar($fieldName, $type);
86
    }
87
88
    /**
89
     *  @private function getInitVars
90
     *
91
     *  @param array $fields
92
     *
93
     * @return string
94
     */
95
    private function getInitVars($fields)
96
    {
97
        $tc = TDMCreateHelper::getInstance();
98
        $ret = '';
99
        // Creation of the initVar functions list
100
        foreach (array_keys($fields) as $f) {
101
            $fieldName = $fields[$f]->getVar('field_name');
102
            $fieldType = $fields[$f]->getVar('field_type');
103
            if ($fieldType > 1) {
104
                $fType = $tc->getHandler('fieldtype')->get($fieldType);
0 ignored issues
show
Bug introduced by
The method get cannot be called on $tc->getHandler('fieldtype') (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...
105
                $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...
106
            } else {
107
                $fieldType = null;
108
            }
109
            switch ($fieldType) {
110
                case 2:
111
                case 3:
112
                case 4:
113
                case 5:
114
                    $ret .= $this->getInitVar($fieldName, 'INT');
115
                    break;
116
                case 6:
117
                    $ret .= $this->getInitVar($fieldName, 'FLOAT');
118
                    break;
119
                case 7:
120
                case 8:
121
                    $ret .= $this->getInitVar($fieldName, 'DECIMAL');
122
                    break;
123
                case 10:
124
                    $ret .= $this->getInitVar($fieldName, 'ENUM');
125
                    break;
126
                case 11:
127
                    $ret .= $this->getInitVar($fieldName, 'EMAIL');
128
                    break;
129
                case 12:
130
                    $ret .= $this->getInitVar($fieldName, 'URL');
131
                    break;
132
                case 13:
133
                case 14:
134
                    $ret .= $this->getInitVar($fieldName, 'TXTBOX');
135
                    break;
136
                case 15:
137
                case 16:
138
                case 17:
139
                case 18:
140
                    $ret .= $this->getInitVar($fieldName, 'TXTAREA');
141
                    break;
142
                case 19:
143
                case 20:
144
                case 21:
145
                case 22:
146
                case 23:
147
                    $ret .= $this->getInitVar($fieldName, 'LTIME');
148
                    break;
149
            }
150
        }
151
152
        return $ret;
153
    }
154
155
    /**
156
     * @private  function getClassObject
157
     * @param $module
158
     * @param $table
159
     * @param $fields
160
     * @return string
161
     */
162
    private function getClassObject($module, $table, $fields)
163
    {
164
        $tc = TDMCreateHelper::getInstance();
165
        $pc = TDMCreatePhpCode::getInstance();
166
        $xc = TDMCreateXoopsCode::getInstance();
167
        $moduleDirname = $module->getVar('mod_dirname');
168
        $tableName = $table->getVar('table_name');
169
        $ucfModuleDirname = ucfirst($moduleDirname);
170
        $ucfTableName = ucfirst($tableName);
171
        $ret = $pc->getPhpCodeDefined();
172
        $ret .= $pc->getPhpCodeCommentMultiLine(['Class Object' => $ucfModuleDirname . $ucfTableName]);
173
        $cCl = '';
174
175
        $fieldInForm = [];
176
        $fieldElementId = [];
177
        $optionsFieldName = [];
178
        foreach (array_keys($fields) as $f) {
179
            $fieldName = $fields[$f]->getVar('field_name');
180
            $fieldElement = $fields[$f]->getVar('field_element');
181
            $fieldInForm[] = $fields[$f]->getVar('field_inform');
182
            $fieldElements = $tc->getHandler('fieldelements')->get($fieldElement);
0 ignored issues
show
Bug introduced by
The method get cannot be called on $tc->getHandler('fieldelements') (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...
183
            $fieldElementId[] = $fieldElements->getVar('fieldelement_id');
184
            $rpFieldName = $this->getRightString($fieldName);
185
            if (in_array(5, $fieldElementId, true)) {
186
                if (count($rpFieldName) % 5) {
187
                    $optionsFieldName[] = "'" . $rpFieldName . "'";
188
                } else {
189
                    $optionsFieldName[] = "'" . $rpFieldName . "'\n";
190
                }
191
            }
192
            if ((0 == $f) && (1 == $table->getVar('table_autoincrement'))) {
193
                $fieldId = $fieldName;
194
            }
195
        }
196
        if (in_array(5, $fieldElementId, true) > 1) {
197
            $optionsElements = implode(', ', $optionsFieldName);
0 ignored issues
show
Unused Code introduced by
$optionsElements 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...
198
            $cCl .= $pc->getPhpCodeCommentMultiLine(['Options' => '']);
199
            $options = $pc->getPhpCodeArray('', $optionsFieldName, true);
200
            $cCl .= $pc->getPhpCodeVariableClass('private', 'options', $options);
201
        }
202
        unset($optionsFieldName);
203
204
        $cCl .= $pc->getPhpCodeCommentMultiLine(['Constructor' => '', '' => '', '@param' => 'null'], "\t");
205
        $constr = $this->getInitVars($fields);
206
        $cCl .= $pc->getPhpCodeFunction('__construct', '', $constr, 'public ', false, "\t");
207
        $arrayGetInstance = ['@static function' => '&getInstance', '' => '', '@param' => 'null'];
208
        $cCl .= $pc->getPhpCodeCommentMultiLine($arrayGetInstance, "\t");
209
        $getInstance = $pc->getPhpCodeVariableClass('static', 'instance', 'false', "\t\t");
210
        $instance = $xc->getXcEqualsOperator('$instance', 'new self()', null, false, "\t\t\t");
211
        $getInstance .= $pc->getPhpCodeConditions('!$instance', '', '', $instance, false, "\t\t");
212
        $cCl .= $pc->getPhpCodeFunction('getInstance', '', $getInstance, 'public static ', false, "\t");
213
214
        $cCl .= $this->getNewInsertId($table);
215
        $cCl .= $this->getFunctionForm($module, $table, $fieldId, $fieldInForm);
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...
216
        $cCl .= $this->getValuesInObject($moduleDirname, $table, $fields);
217
        $cCl .= $this->getToArrayInObject($table);
218
219
        if (in_array(5, $fieldElementId, true) > 1) {
220
            $cCl .= $this->getOptionsCheck($table);
221
        }
222
        unset($fieldElementId);
223
224
        $ret .= $pc->getPhpCodeClass($ucfModuleDirname . $ucfTableName, $cCl, 'XoopsObject');
225
226
        return $ret;
227
    }
228
229
    /**
230
     *  @private function getNewInsertId
231
     *
232
     *  @param $table
233
     *
234
     * @return string
235
     */
236
    private function getNewInsertId($table)
237
    {
238
        $pc = TDMCreatePhpCode::getInstance();
239
        $xc = TDMCreateXoopsCode::getInstance();
240
        $tableName = $table->getVar('table_name');
241
        $ucfTableName = ucfirst($tableName);
242
        $ret = $pc->getPhpCodeCommentMultiLine(['The new inserted' => '$Id', '@return' => 'inserted id'], "\t");
243
        $getInsertedId = $xc->getXcEqualsOperator('$newInsertedId', "\$GLOBALS['xoopsDB']->getInsertId()", null, false, "\t\t");
244
        $getInsertedId .= $this->getSimpleString('return $newInsertedId;', "\t\t");
245
246
        $ret .= $pc->getPhpCodeFunction('getNewInsertedId' . $ucfTableName, '', $getInsertedId, 'public ', false, "\t");
247
248
        return $ret;
249
    }
250
251
    /**
252
     * @private function getFunctionForm
253
     *
254
     * @param string $module
255
     * @param string $table
256
     *
257
     * @param        $fieldId
258
     * @param        $fieldInForm
259
     * @return string
260
     */
261
    private function getFunctionForm($module, $table, $fieldId, $fieldInForm)
262
    {
263
        $pc = TDMCreatePhpCode::getInstance();
264
        $xc = TDMCreateXoopsCode::getInstance();
265
        $cc = ClassXoopsCode::getInstance();
266
        $fe = ClassFormElements::getInstance();
267
        $moduleDirname = $module->getVar('mod_dirname');
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $module (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...
268
        $tableName = $table->getVar('table_name');
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $table (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...
269
        $tableSoleName = $table->getVar('table_solename');
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $table (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...
270
        $tableCategory = $table->getVar('table_category');
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $table (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...
271
        $tablePermissions = $table->getVar('table_permissions');
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $table (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...
Unused Code introduced by
$tablePermissions 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...
272
        $ucfTableName = ucfirst($tableName);
273
        $stuTableSoleName = mb_strtoupper($tableSoleName);
274
        $language = $this->getLanguage($moduleDirname, 'AM');
275
        $fe->initForm($module, $table);
276
        $ret = $pc->getPhpCodeCommentMultiLine(['@public function' => 'getForm', '@param bool' => '$action', '@return' => 'XoopsThemeForm'], "\t");
277
        $action = $xc->getXcEqualsOperator('$action', "\$_SERVER['REQUEST_URI']", null, false, "\t\t\t");
278
        $ucfModuleDirname = ucfirst($moduleDirname);
279
        $getForm = $xc->getXcGetInstance("{$moduleDirname}", "{$ucfModuleDirname}Helper", "\t\t");
280
        //$getForm .= $pc->getPhpCodeConditions('$action', ' === ', 'false', $action, false, "\t\t");
281
        $getForm .= $pc->getPhpCodeConditions('false', ' === ', '$action', $action, false, "\t\t");
282
        $xUser = $pc->getPhpCodeGlobals('xoopsUser');
283
        $xModule = $pc->getPhpCodeGlobals('xoopsModule');
284
        if (1 != $tableCategory/* && (1 == $tablePermissions)*/) {
285
            $getForm .= $pc->getPhpCodeCommentLine('Permissions for', 'uploader', "\t\t");
286
            $getForm .= $xc->getXcEqualsOperator('$gpermHandler', "xoops_getHandler('groupperm')", null, true, "\t\t");
287
            $getForm .= $pc->getPhpCodeTernaryOperator('groups', 'is_object(' . $xUser . ')', $xUser . '->getGroups()', 'XOOPS_GROUP_ANONYMOUS', "\t\t");
288
            $checkRight = $xc->getXcCheckRight('$gpermHandler', $permString = '', 32, '$groups', $xModule . '->getVar(\'mid\')', true);
289
            $ternaryOperator = $pc->getPhpCodeTernaryOperator('permissionUpload', $checkRight, 'true', 'false', "\t\t\t\t");
290
            $permissionUpload = $xc->getXcEqualsOperator('$permissionUpload', 'true', null, false, "\t\t\t\t");
291
            $ternOperator = $pc->getPhpCodeRemoveCarriageReturn($ternaryOperator, '', "\r");
292
            $if = $pc->getPhpCodeConditions('!' . $xUser . '->isAdmin(' . $xModule . '->mid())', '', '', $ternaryOperator, $permissionUpload, "\t\t\t");
293
            $getForm .= $pc->getPhpCodeConditions($xUser, '', '', $if, $ternOperator, "\t\t");
294
        }
295
        $getForm .= $pc->getPhpCodeCommentLine('Title', '', "\t\t");
296
        $getForm .= $pc->getPhpCodeTernaryOperator('title', '$this->isNew()', "sprintf({$language}{$stuTableSoleName}_ADD)", "sprintf({$language}{$stuTableSoleName}_EDIT)", "\t\t");
297
        $getForm .= $pc->getPhpCodeCommentLine('Get Theme', 'Form', "\t\t");
298
        $getForm .= $xc->getXcLoad('XoopsFormLoader', "\t\t");
299
        $getForm .= $cc->getClassXoopsThemeForm('form', 'title', 'form', 'action', 'post');
300
        $getForm .= $cc->getClassSetExtra('form', "'enctype=\"multipart/form-data\"'");
301
        $getForm .= $fe->renderElements();
302
303
        if (in_array(1, $fieldInForm, true)) {
304
            if (1 == $table->getVar('table_permissions')) {
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $table (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...
305
                $getForm .= $this->getPermissionsInForm($moduleDirname, $fieldId);
306
            }
307
        }
308
        $getForm .= $pc->getPhpCodeCommentLine('To Save', '', "\t\t");
309
        //$hiddenSave = $cc->getClassXoopsFormHidden('', "'op'", "'save'", true, false);
310
        $getForm .= $cc->getClassAddElement('form', "new XoopsFormHidden('op', 'save')");
311
        //$buttonSend = $cc->getClassXoopsFormButton('', '', 'submit', '_SUBMIT', 'submit', true);
312
        $getForm .= $cc->getClassAddElement('form', "new XoopsFormButtonTray('', _SUBMIT, 'submit', '', false)");
313
        $getForm .= $this->getSimpleString('return $form;', "\t\t");
314
315
        $ret .= $pc->getPhpCodeFunction('getForm' . $ucfTableName, '$action = false', $getForm, 'public ', false, "\t");
316
317
        return $ret;
318
    }
319
320
    /**
321
     *  @private function getPermissionsInForm
322
     *
323
     *  @param string $moduleDirname
324
     *  @param string $fieldId
325
     *
326
     * @return string
327
     */
328
    private function getPermissionsInForm($moduleDirname, $fieldId)
329
    {
330
        $pc = TDMCreatePhpCode::getInstance();
331
        $xc = TDMCreateXoopsCode::getInstance();
332
        $cc = ClassXoopsCode::getInstance();
333
        $permissionApprove = $this->getLanguage($moduleDirname, 'AM', 'PERMISSIONS_APPROVE');
334
        $permissionSubmit = $this->getLanguage($moduleDirname, 'AM', 'PERMISSIONS_SUBMIT');
335
        $permissionView = $this->getLanguage($moduleDirname, 'AM', 'PERMISSIONS_VIEW');
336
        $ret = $pc->getPhpCodeCommentLine('Permissions', '', "\t\t");
337
        $ret .= $xc->getXcEqualsOperator('$memberHandler', "xoops_getHandler('member')", null, false, "\t\t");
338
        $ret .= $xc->getXcEqualsOperator('$groupList', '$memberHandler->getGroupList()', null, false, "\t\t");
339
        $ret .= $xc->getXcEqualsOperator('$gpermHandler', "xoops_getHandler('groupperm')", null, false, "\t\t");
340
        $ret .= $pc->getPhpCodeArrayType('fullList', 'keys', 'groupList', null, false, "\t\t");
341
        $fId = $xc->getXcGetVar('', 'this', $fieldId, true);
342
        $mId = $xc->getXcGetVar('', "GLOBALS['xoopsModule']", 'mid', true);
343
        $ifGroups = $xc->getXcGetGroupIds('groupsIdsApprove', 'gpermHandler', "'{$moduleDirname}_approve'", $fId, $mId, "\t\t\t");
344
        $ifGroups .= $pc->getPhpCodeArrayType('groupsIdsApprove', 'values', 'groupsIdsApprove', null, false, "\t\t\t");
345
        $ifGroups .= $cc->getClassXoopsFormCheckBox('groupsCanApproveCheckbox', "{$permissionApprove}", 'groups_approve[]', '$groupsIdsApprove', false, "\t\t\t");
346
        $ifGroups .= $xc->getXcGetGroupIds('groupsIdsSubmit', 'gpermHandler', "'{$moduleDirname}_submit'", $fId, $mId, "\t\t\t");
347
        $ifGroups .= $pc->getPhpCodeArrayType('groupsIdsSubmit', 'values', 'groupsIdsSubmit', null, false, "\t\t\t");
348
        $ifGroups .= $cc->getClassXoopsFormCheckBox('groupsCanSubmitCheckbox', "{$permissionSubmit}", 'groups_submit[]', '$groupsIdsSubmit', false, "\t\t\t");
349
        $ifGroups .= $xc->getXcGetGroupIds('groupsIdsView', 'gpermHandler', "'{$moduleDirname}_view'", $fId, $mId, "\t\t\t");
350
        $ifGroups .= $pc->getPhpCodeArrayType('groupsIdsView', 'values', 'groupsIdsView', null, false, "\t\t\t");
351
        $ifGroups .= $cc->getClassXoopsFormCheckBox('groupsCanViewCheckbox', "{$permissionView}", 'groups_view[]', '$groupsIdsView', false, "\t\t\t");
352
353
        $else = $cc->getClassXoopsFormCheckBox('groupsCanApproveCheckbox', "{$permissionApprove}", 'groups_approve[]', '$fullList', false, "\t\t\t");
354
        $else .= $cc->getClassXoopsFormCheckBox('groupsCanSubmitCheckbox', "{$permissionSubmit}", 'groups_submit[]', '$fullList', false, "\t\t\t");
355
        $else .= $cc->getClassXoopsFormCheckBox('groupsCanViewCheckbox', "{$permissionView}", 'groups_view[]', '$fullList', false, "\t\t\t");
356
357
        $ret .= $pc->getPhpCodeConditions('!$this->isNew()', null, null, $ifGroups, $else, "\t\t");
358
        $ret .= $pc->getPhpCodeCommentLine('To Approve', '', "\t\t");
359
        $ret .= $cc->getClassAddOptionArray('groupsCanApproveCheckbox', '$groupList');
360
        $ret .= $cc->getClassAddElement('form', '$groupsCanApproveCheckbox');
361
        $ret .= $pc->getPhpCodeCommentLine('To Submit', '', "\t\t");
362
        $ret .= $cc->getClassAddOptionArray('groupsCanSubmitCheckbox', '$groupList');
363
        $ret .= $cc->getClassAddElement('form', '$groupsCanSubmitCheckbox');
364
        $ret .= $pc->getPhpCodeCommentLine('To View', '', "\t\t");
365
        $ret .= $cc->getClassAddOptionArray('groupsCanViewCheckbox', '$groupList');
366
        $ret .= $cc->getClassAddElement('form', '$groupsCanViewCheckbox');
367
368
        return $ret;
369
    }
370
371
    /**
372
     * @private  function getValuesInObject
373
     *
374
     * @param $moduleDirname
375
     * @param $table
376
     * @param $fields
377
     * @return string
378
     * @internal param $null
379
     */
380
    private function getValuesInObject($moduleDirname, $table, $fields)
381
    {
382
        $tc = TDMCreateHelper::getInstance();
383
        $pc = TDMCreatePhpCode::getInstance();
384
        $xc = TDMCreateXoopsCode::getInstance();
385
        $stuModuleDirname = mb_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...
386
        $ucfTableName = ucfirst($table->getVar('table_name'));
387
        $ret = $pc->getPhpCodeCommentMultiLine(['Get' => 'Values', '@param null $keys' => '', '@param null $format' => '', '@param null$maxDepth' => '', '@return' => 'array'], "\t");
388
        $ucfModuleDirname = ucfirst($moduleDirname);
389
        $getValues = $xc->getXcGetInstance("{$moduleDirname}", "{$ucfModuleDirname}Helper", "\t\t");
390
        $getValues .= $xc->getXcEqualsOperator('$ret', '$this->getValues($keys, $format, $maxDepth)', null, false, "\t\t");
391
392
        foreach (array_keys($fields) as $f) {
393
            $fieldName = $fields[$f]->getVar('field_name');
394
            $fieldElement = $fields[$f]->getVar('field_element');
395
            $rpFieldName = $this->getRightString($fieldName);
396
            switch ($fieldElement) {
397
                case 3:
398
                case 4:
399
                    $getValues .= $pc->getPhpCodeStripTags("ret['{$rpFieldName}']", "\$this->getVar('{$fieldName}')", false, "\t\t");
400
                break;
401
                case 8:
402
                    $getValues .= $xc->getXcUnameFromId("ret['{$rpFieldName}']", "\$this->getVar('{$fieldName}')", "\t\t");
403
                break;
404
                case 15:
405
                    $getValues .= $xc->getXcFormatTimeStamp("ret['{$rpFieldName}']", "\$this->getVar('{$fieldName}')", 's', "\t\t");
406
                break;
407
                default:
408
                    if ($fieldElement > 15) {
409
                        $fieldElements = $tc->getHandler('fieldelements')->get($fieldElement);
0 ignored issues
show
Bug introduced by
The method get cannot be called on $tc->getHandler('fieldelements') (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...
410
                        $fieldElementMid = $fieldElements->getVar('fieldelement_mid');
411
                        $fieldElementTid = $fieldElements->getVar('fieldelement_tid');
412
                        $fieldElementName = $fieldElements->getVar('fieldelement_name');
413
                        $fieldNameDesc = mb_substr($fieldElementName, mb_strrpos($fieldElementName, ':'), mb_strlen($fieldElementName));
414
                        $topicTableName = str_replace(': ', '', mb_strtolower($fieldNameDesc));
415
                        $fieldsTopics = $this->getTableFields($fieldElementMid, $fieldElementTid);
416
                        foreach (array_keys($fieldsTopics) as $g) {
417
                            $fieldNameTopic = $fieldsTopics[$g]->getVar('field_name');
418
                            if (1 == $fieldsTopics[$g]->getVar('field_main')) {
419
                                $fieldMainTopic = $fieldNameTopic;
420
                            }
421
                        }
422
                        $getHandlerVar = "\${$moduleDirname}->getHandler('{$topicTableName}')";
423
                        $getValues .= $xc->getXcEqualsOperator("\${$topicTableName}", $getHandlerVar, null, false, "\t\t");
424
                        $getTopicTable = "\${$topicTableName}->get(\$this->getVar('{$fieldName}'))";
425
                        $getValues .= $xc->getXcEqualsOperator("\${$topicTableName}Obj", $getTopicTable, null, false, "\t\t");
426
                        $fMainTopic = "\${$topicTableName}Obj->getVar('{$fieldMainTopic}')";
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...
427
                        $getValues .= $xc->getXcEqualsOperator("\$ret['{$rpFieldName}']", $fMainTopic, null, false, "\t\t");
428
                    } else {
429
                        $getValues .= $xc->getXcGetVar("ret['{$rpFieldName}']", 'this', $fieldName, false, "\t\t");
430
                    }
431
                break;
432
            }
433
        }
434
        $getValues .= $this->getSimpleString('return $ret;', "\t\t");
435
436
        $ret .= $pc->getPhpCodeFunction('getValues' . $ucfTableName, '$keys = null, $format = null, $maxDepth = null', $getValues, 'public ', false, "\t");
437
438
        return $ret;
439
    }
440
441
    /**
442
     *  @private function getToArrayInObject
443
     *
444
     *  @param $table
445
     *
446
     * @return string
447
     */
448
    private function getToArrayInObject($table)
449
    {
450
        $pc = TDMCreatePhpCode::getInstance();
451
        $xc = TDMCreateXoopsCode::getInstance();
452
        $tableName = $table->getVar('table_name');
453
        $ucfTableName = ucfirst($tableName);
454
        $multiLineCom = ['Returns an array representation' => 'of the object', '' => '', '@return' => 'array'];
455
        $ret = $pc->getPhpCodeCommentMultiLine($multiLineCom, "\t");
456
457
        $getToArray = $pc->getPhpCodeArray('ret', [], false, "\t\t");
458
        $getToArray .= $xc->getXcEqualsOperator('$vars', '$this->getVars()', null, false, "\t\t");
459
        $foreach = $xc->getXcGetVar('ret[$var]', 'this', '"{$var}"', false, "\t");
460
        $getToArray .= $pc->getPhpCodeForeach('vars', true, false, 'var', $foreach, "\t\t");
461
        $getToArray .= $this->getSimpleString('return $ret;', "\t\t");
462
463
        $ret .= $pc->getPhpCodeFunction('toArray' . $ucfTableName, '', $getToArray, 'public ', false, "\t");
464
465
        return $ret;
466
    }
467
468
    /**
469
     *  @private function getOptionsCheck
470
     *
471
     *  @param $table
472
     *
473
     * @return string
474
     */
475
    private function getOptionsCheck($table)
476
    {
477
        $tc = TDMCreateHelper::getInstance();
478
        $pc = TDMCreatePhpCode::getInstance();
479
        $tableName = $table->getVar('table_name');
480
        $ucfTableName = ucfirst($tableName);
481
        $ret = $pc->getPhpCodeCommentMultiLine(['Get' => 'Options'], "\t");
482
        $getOptions = $pc->getPhpCodeArray('ret', [], false, "\t");
483
484
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
485
        foreach (array_keys($fields) as $f) {
486
            $fieldName = $fields[$f]->getVar('field_name');
487
            $fieldElement = $fields[$f]->getVar('field_element');
488
489
            $fieldElements = $tc->getHandler('fieldelements')->get($fieldElement);
0 ignored issues
show
Bug introduced by
The method get cannot be called on $tc->getHandler('fieldelements') (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...
490
            $fieldElementId = $fieldElements->getVar('fieldelement_id');
491
            $rpFieldName = $this->getRightString($fieldName);
492
            if (5 == $fieldElementId) {
493
                $arrayPush = $pc->getPhpCodeArrayType('ret', 'push', "'{$rpFieldName}'", null, false, "\t\t\t");
494
                $getOptions .= $pc->getPhpCodeConditions(1, ' == ', "\$this->getVar('{$fieldName}')", $arrayPush, false, "\t\t");
495
            }
496
        }
497
498
        $getOptions .= $this->getSimpleString('return $ret;', "\t\t");
499
500
        $ret .= $pc->getPhpCodeFunction('getOptions' . $ucfTableName, '', $getOptions, 'public ', false, "\t");
501
502
        return $ret;
503
    }
504
505
    /**
506
     * @public function getClassHandler
507
     *
508
     * @param string $moduleDirname
509
     * @param string $table
510
     * @param string $fieldId
511
     * @param        $fieldName
512
     * @param string $fieldMain
513
     *
514
     * @param        $fieldParent
515
     * @param        $fieldParentId
516
     * @param        $fieldElement
517
     * @return string
518
     */
519
    private function getClassObjectHandler($moduleDirname, $table, $fieldId, $fieldName, $fieldMain, $fieldParent, $fieldParentId, $fieldElement)
520
    {
521
        $pc = TDMCreatePhpCode::getInstance();
522
        $tableName = $table->getVar('table_name');
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $table (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...
523
        $tableCategory = $table->getVar('table_category');
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $table (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...
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...
524
        $tableSoleName = $table->getVar('table_solename');
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $table (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...
525
        $tableFieldname = $table->getVar('table_fieldname');
0 ignored issues
show
Bug introduced by
The method getVar cannot be called on $table (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...
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...
526
        $ucfModuleDirname = ucfirst($moduleDirname);
527
        $ucfTableName = ucfirst($tableName);
528
        $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...
529
        $ucfModuleTable = $ucfModuleDirname . $ucfTableName;
530
        $multiLineCom = ['Class Object Handler' => $ucfModuleDirname . $ucfTableName];
531
        $ret = $pc->getPhpCodeCommentMultiLine($multiLineCom);
532
533
        $cClh = $pc->getPhpCodeCommentMultiLine(['Constructor' => '', '' => '', '@param' => 'null|XoopsDatabase $db'], "\t");
534
        $constr = "\t\tparent::__construct(\$db, '{$moduleDirname}_{$tableName}', '{$moduleDirname}{$tableName}', '{$fieldId}', '{$fieldMain}');\n";
535
536
        $cClh .= $pc->getPhpCodeFunction('__construct', 'XoopsDatabase $db', $constr, 'public ', false, "\t");
537
        $cClh .= $this->getClassCreate();
538
        $cClh .= $this->getClassGet();
539
        $cClh .= $this->getClassGetInsertId();
540
        $cClh .= $this->getClassCounter($tableName, $fieldId, $fieldMain);
541
        $cClh .= $this->getClassAll($tableName, $fieldId, $fieldMain);
542
        $cClh .= $this->getClassCriteria($tableName);
543
        if ($fieldElement > 15 && in_array(1, $fieldParentId, true)) {
544
            $cClh .= $this->getClassByCategory($moduleDirname, $tableName, $tableFieldName, $fieldId, $fieldName, $fieldMain, $fieldParent, $fieldElement);
0 ignored issues
show
Bug introduced by
The variable $tableFieldName does not exist. Did you mean $fieldName?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
545
            $cClh .= $this->getClassGetTableSolenameById($moduleDirname, $table, $fieldMain);
546
        }
547
548
        $ret .= $pc->getPhpCodeClass("{$ucfModuleTable}Handler", $cClh, 'XoopsPersistableObjectHandler');
549
550
        return $ret;
551
    }
552
553
    /**
554
     *  @public function getClassCreate
555
     *
556
     *  @return string
557
     */
558
    private function getClassCreate()
559
    {
560
        $pc = TDMCreatePhpCode::getInstance();
561
        $ret = $pc->getPhpCodeCommentMultiLine(['@param bool' => '$isNew', '' => '', '@return' => 'object'], "\t");
562
        $cClhc = $this->getSimpleString('return parent::create($isNew);', "\t\t");
563
564
        $ret .= $pc->getPhpCodeFunction('create', '$isNew = true', $cClhc, 'public ', false, "\t");
565
566
        return $ret;
567
    }
568
569
    /**
570
     *  @public function getClassGet
571
     *
572
     *  @return string
573
     */
574
    private function getClassGet()
575
    {
576
        $pc = TDMCreatePhpCode::getInstance();
577
        $ret = $pc->getPhpCodeCommentMultiLine(['retrieve a' => 'field', '' => '', '@param int' => '$i field id', '@param null' => 'fields', '@return mixed reference to the' => '{@link Get} object'], "\t");
578
        $cClhg = $this->getSimpleString('return parent::get($i, $fields);', "\t\t");
579
580
        $ret .= $pc->getPhpCodeFunction('get', '$i = null, $fields = null', $cClhg, 'public ', false, "\t");
581
582
        return $ret;
583
    }
584
585
    /**
586
     *  @public function getClassGetInsertId
587
     *
588
     *  @return string
589
     */
590
    private function getClassGetInsertId()
591
    {
592
        $pc = TDMCreatePhpCode::getInstance();
593
        $ret = $pc->getPhpCodeCommentMultiLine(['get inserted' => 'id', '' => '', '@param' => 'null', '@return integer reference to the' => '{@link Get} object'], "\t");
594
        $cClhgid = $this->getSimpleString('return $this->db->getInsertId();', "\t\t");
595
596
        $ret .= $pc->getPhpCodeFunction('getInsertId', '', $cClhgid, 'public ', false, "\t");
597
598
        return $ret;
599
    }
600
601
    /**
602
     *  @public function getClassCounter
603
     *
604
     *  @param $tableName
605
     *  @param $fieldId
606
     *  @param $fieldMain
607
     *
608
     *  @return string
609
     */
610
    private function getClassCounter($tableName, $fieldId, $fieldMain)
611
    {
612
        $pc = TDMCreatePhpCode::getInstance();
613
        $xc = TDMCreateXoopsCode::getInstance();
614
        $cc = ClassXoopsCode::getInstance();
615
        $ucfTableName = ucfirst($tableName);
616
        $ret = $pc->getPhpCodeCommentMultiLine(['Get Count ' . $ucfTableName => 'in the database', '@param int    $start' => '', '@param int    $limit' => '', '@param string $sort' => '', '@param string $order' => '', '@return' => 'int'], "\t");
617
618
        $critCount = $cc->getClassCriteriaCompo('crCount' . $ucfTableName, "\t\t");
619
        $paramsCrit = "\$this->get{$ucfTableName}Criteria(\$crCount{$ucfTableName}, \$start, \$limit, \$sort, \$order)";
620
        $critCount .= $xc->getXcEqualsOperator('$crCount' . $ucfTableName, $paramsCrit, null, false, "\t\t");
621
        $critCount .= $this->getSimpleString("return parent::getCount(\$crCount{$ucfTableName});", "\t\t");
622
        $params = "\$start = 0, \$limit = 0, \$sort = '{$fieldId} ASC, {$fieldMain}', \$order = 'ASC'";
623
624
        $ret .= $pc->getPhpCodeFunction('getCount' . $ucfTableName, $params, $critCount, 'public ', false, "\t");
625
626
        return $ret;
627
    }
628
629
    /**
630
     *  @public function getClassAll
631
     *
632
     *  @param $tableName
633
     *  @param $fieldId
634
     *  @param $fieldMain
635
     *
636
     *  @return string
637
     */
638
    private function getClassAll($tableName, $fieldId, $fieldMain)
639
    {
640
        $pc = TDMCreatePhpCode::getInstance();
641
        $xc = TDMCreateXoopsCode::getInstance();
642
        $cc = ClassXoopsCode::getInstance();
643
        $ucfTableName = ucfirst($tableName);
644
        $ret = $pc->getPhpCodeCommentMultiLine(['Get All ' . $ucfTableName => 'in the database', '@param int    $start' => '', '@param int    $limit' => '', '@param string $sort' => '', '@param string $order' => '', '@return' => 'array'], "\t");
645
646
        $critAll = $cc->getClassCriteriaCompo('crAll' . $ucfTableName, "\t\t");
647
        $paramsCrit = "\$this->get{$ucfTableName}Criteria(\$crAll{$ucfTableName}, \$start, \$limit, \$sort, \$order)";
648
        $critAll .= $xc->getXcEqualsOperator('$crAll' . $ucfTableName, $paramsCrit, null, false, "\t\t");
649
        $critAll .= $this->getSimpleString("return parent::getAll(\$crAll{$ucfTableName});", "\t\t");
650
        $params = "\$start = 0, \$limit = 0, \$sort = '{$fieldId} ASC, {$fieldMain}', \$order = 'ASC'";
651
652
        $ret .= $pc->getPhpCodeFunction('getAll' . $ucfTableName, $params, $critAll, 'public ', false, "\t");
653
654
        return $ret;
655
    }
656
657
    /**
658
     * @public function getClassByCategory
659
     *
660
     * @param $moduleDirname
661
     * @param $tableName
662
     * @param $tableFieldName
663
     * @param $fieldId
664
     * @param $fieldName
665
     * @param $fieldMain
666
     * @param $fieldParent
667
     *
668
     * @param $fieldElement
669
     * @return string
670
     */
671
    private function getClassByCategory($moduleDirname, $tableName, $tableFieldName, $fieldId, $fieldName, $fieldMain, $fieldParent, $fieldElement)
0 ignored issues
show
Unused Code introduced by
The parameter $fieldParent is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
672
    {
673
        $tc = TDMCreateHelper::getInstance();
674
        $pc = TDMCreatePhpCode::getInstance();
675
        $xc = TDMCreateXoopsCode::getInstance();
676
        $cc = ClassXoopsCode::getInstance();
677
        $ucfTableName = ucfirst($tableName);
678
        $fieldElements = $tc->getHandler('fieldelements')->get($fieldElement);
0 ignored issues
show
Bug introduced by
The method get cannot be called on $tc->getHandler('fieldelements') (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...
679
        $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...
680
        $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...
681
        $fieldElementName = $fieldElements->getVar('fieldelement_name');
682
        $fieldNameDesc = ucfirst(mb_substr($fieldElementName, mb_strrpos($fieldElementName, ':'), mb_strlen($fieldElementName)));
683
        $topicTableName = str_replace(': ', '', $fieldNameDesc);
684
        $lcfTopicTableName = lcfirst($topicTableName);
685
686
        $ret = $pc->getPhpCodeCommentMultiLine(["Get All {$ucfTableName} By" => "{$fieldNameDesc} Id", '@param int    $start' => '', '@param int    $limit' => '', '@param string $sort' => '', '@param string $order' => '', '@return' => 'array'], "\t");
687
688
        $critAll = $xc->getXcEqualsOperator('$gpermHandler', "xoops_getHandler('groupperm')", null, true, "\t\t");
689
        $param1 = "'{$moduleDirname}_view'";
690
        $param2 = "\$GLOBALS['xoopsUser']->getGroups()";
691
        $param3 = "\$GLOBALS['xoopsModule']->getVar('mid')";
692
        $critAll .= $xc->getXcGetItemIds($lcfTopicTableName, 'gpermHandler', $param1, $param2, $param3, "\t\t");
693
        $critAll .= $cc->getClassCriteriaCompo('crAll' . $ucfTableName, "\t\t");
694
695
        if (false !== mb_strpos($fieldName, 'status')) {
696
            $crit = $cc->getClassCriteria('', "'{$fieldName}'", '0', "'!='", true);
697
            $critAll .= $cc->getClassAdd('crAll' . $ucfTableName, $crit, "\t\t");
698
        }
699
        $paramsCritAll = "\$this->get{$ucfTableName}Criteria(\$crAll{$ucfTableName}, \$start, \$limit, \$sort, \$order)";
700
        $critAll .= $xc->getXcEqualsOperator('$crAll' . $ucfTableName, $paramsCritAll, null, false, "\t\t");
701
702
        $critAll .= $this->getSimpleString("return parent::getAll(\$crAll{$ucfTableName});", "\t\t");
703
        $params = "\${$tableFieldName}Id, \$start = 0, \$limit = 0, \$sort = '{$fieldId} ASC, {$fieldMain}', \$order = 'ASC'";
704
705
        $ret .= $pc->getPhpCodeFunction("getAll{$ucfTableName}By{$fieldNameDesc}Id" . $ucfTableName, $params, $critAll, 'public ', false, "\t");
706
707
        return $ret;
708
    }
709
710
    /**
711
     *  @public function getClassCriteria
712
     *
713
     *  @param $tableName
714
     *  @return string
715
     */
716
    private function getClassCriteria($tableName)
717
    {
718
        $pc = TDMCreatePhpCode::getInstance();
719
        $cc = ClassXoopsCode::getInstance();
720
        $ucfTableName = ucfirst($tableName);
721
        $ret = $pc->getPhpCodeCommentMultiLine(['Get' => 'Criteria ' . $ucfTableName, '@param       ' => "\$cr{$ucfTableName}", '@param int    $start' => '', '@param int    $limit' => '', '@param string $sort' => '', '@param string $order' => '', '@return' => 'int'], "\t");
722
723
        $paramsAllCriteria = "\$cr{$ucfTableName}, \$start, \$limit, \$sort, \$order";
724
725
        $critSets = $cc->getClassSetStart('cr' . $ucfTableName, 'start', "\t\t");
726
        $critSets .= $cc->getClassSetLimit('cr' . $ucfTableName, 'limit', "\t\t");
727
        $critSets .= $cc->getClassSetSort('cr' . $ucfTableName, 'sort', "\t\t");
728
        $critSets .= $cc->getClassSetOrder('cr' . $ucfTableName, 'order', "\t\t");
729
        $critSets .= $this->getSimpleString("return \$cr{$ucfTableName};", "\t\t");
730
731
        $ret .= $pc->getPhpCodeFunction("get{$ucfTableName}Criteria", $paramsAllCriteria, $critSets, 'private ', false, "\t");
732
733
        return $ret;
734
    }
735
736
    /**
737
     * @public function getClassGetTableSolenameById
738
     *
739
     * @param $moduleDirname
740
     * @param $table
741
     *
742
     * @param $fieldMain
743
     * @return string
744
     */
745
    private function getClassGetTableSolenameById($moduleDirname, $table, $fieldMain)
746
    {
747
        $pc = TDMCreatePhpCode::getInstance();
748
        $xc = TDMCreateXoopsCode::getInstance();
749
        $tableName = $table->getVar('table_name');
750
        $tableSoleName = $table->getVar('table_solename');
751
        $ucfTableSoleName = ucfirst($tableSoleName);
752
        $ccTableSoleName = $this->getCamelCase($tableSoleName, true);
753
        $ret = $pc->getPhpCodeCommentMultiLine(['Returns the' => $ucfTableSoleName . ' from id', '' => '', '@return' => 'string'], "\t");
754
        $soleName = $xc->getXcEqualsOperator("\${$tableSoleName}Id", "(int)( \${$tableSoleName}Id )", null, false, "\t\t");
755
        $soleName .= $xc->getXcEqualsOperator("\${$tableSoleName}", "''", null, false, "\t\t");
756
757
        $contentIf = $xc->getXoopsHandlerLine($moduleDirname, $tableName, "\t\t\t");
758
        $contentIf .= $xc->getXcGet($tableName, "\${$tableSoleName}Id", 'Obj', true, false, "\t\t\t");
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
759
        $getVar = $xc->getXcGetVar($ccTableSoleName, "{$tableSoleName}Obj", $fieldMain, false, "\t\t\t\t");
760
        $contentIf .= $pc->getPhpCodeConditions("is_object( \${$tableSoleName}Obj )", '', '', $getVar, false, "\t\t\t");
761
762
        $soleName .= $pc->getPhpCodeConditions("\${$tableSoleName}Id", ' > ', '0', $contentIf = null, false, "\t\t");
763
        $soleName .= $this->getSimpleString("return \${$tableSoleName};", "\t\t");
764
765
        $ret .= $pc->getPhpCodeFunction("get{$ucfTableSoleName}FromId", "\${$tableSoleName}Id", $soleName, 'public ', false, "\t");
766
767
        return $ret;
768
    }
769
770
    /**
771
     * @public function render
772
     * @param null
773
     *
774
     * @return bool|string
775
     */
776
    public function render()
777
    {
778
        $tc = TDMCreateHelper::getInstance();
779
        $module = $this->getModule();
780
        $table = $this->getTable();
781
        $filename = $this->getFileName();
782
        $tableName = $table->getVar('table_name');
0 ignored issues
show
Unused Code introduced by
$tableName 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...
783
        $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...
784
        $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...
785
        $moduleDirname = $module->getVar('mod_dirname');
786
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
787
        $fieldInForm = [];
788
        $fieldParentId = [];
789
        $fieldElementId = [];
790
        $fieldParent = null;
791
        foreach (array_keys($fields) as $f) {
792
            $fieldName = $fields[$f]->getVar('field_name');
793
            $fieldInForm[] = $fields[$f]->getVar('field_inform');
794
            $fieldParentId[] = $fields[$f]->getVar('field_parent');
795
            if ((0 == $f) && (1 == $table->getVar('table_autoincrement'))) {
796
                $fieldId = $fieldName; // $fieldId = fields parameter index field
797
            }
798
            if (1 == $fields[$f]->getVar('field_main')) {
799
                $fieldMain = $fieldName; // $fieldMain = fields parameter main field
800
            }
801
            if (1 == $fields[$f]->getVar('field_parent')) {
802
                $fieldParent = $fieldName; // $fieldParent = fields parameter parent field
803
            }
804
            $fieldElement = $fields[$f]->getVar('field_element');
805
806
            $fieldElements = $tc->getHandler('fieldelements')->get($fieldElement);
0 ignored issues
show
Bug introduced by
The method get cannot be called on $tc->getHandler('fieldelements') (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...
807
            $fieldElementId[] = $fieldElements->getVar('fieldelement_id');
808
        }
809
        $content = $this->getHeaderFilesComments($module, $filename);
810
        $content .= $this->getClassObject($module, $table, $fields);
811
        $content .= $this->getClassObjectHandler($moduleDirname, $table, $fieldId, $fieldName, $fieldMain, $fieldParent, $fieldParentId, $fieldElement);
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...
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 $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 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...
812
813
        $this->create($moduleDirname, 'class', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
814
815
        return $this->renderFile();
816
    }
817
}
818