Completed
Pull Request — master (#126)
by Gino
03:40
created

ClassFiles::getClassGetTableSolenameById()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
506
     *  @param string $fieldId
507
     *  @param string $fieldMain
508
     *
509
     * @return string
510
     */
511
    private function getClassObjectHandler($moduleDirname, $table, $fieldId, $fieldName, $fieldMain, $fieldParent, $fieldParentId, $fieldElement)
512
    {
513
        $pc = TDMCreatePhpCode::getInstance();
514
        $tableName = $table->getVar('table_name');
515
        $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...
516
        $tableSoleName = $table->getVar('table_solename');
517
        $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...
518
        $ucfModuleDirname = ucfirst($moduleDirname);
519
        $ucfTableName = ucfirst($tableName);
520
        $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...
521
        $ucfModuleTable = $ucfModuleDirname.$ucfTableName;
522
        $multiLineCom = array('Class Object Handler' => $ucfModuleDirname.$ucfTableName);
523
        $ret = $pc->getPhpCodeCommentMultiLine($multiLineCom);
524
525
        $cClh = $pc->getPhpCodeCommentMultiLine(array('Constructor' => '', '' => '', '@param' => 'null|XoopsDatabase $db'), "\t");
526
        $constr = "\t\tparent::__construct(\$db, '{$moduleDirname}_{$tableName}', '{$moduleDirname}{$tableName}', '{$fieldId}', '{$fieldMain}');\n";
527
528
        $cClh .= $pc->getPhpCodeFunction('__construct', 'XoopsDatabase $db', $constr, 'public ', false, "\t");
529
        $cClh .= $this->getClassCreate();
530
        $cClh .= $this->getClassGet();
531
        $cClh .= $this->getClassGetInsertId();
532
        $cClh .= $this->getClassCounter($tableName, $fieldId, $fieldMain);
533
        $cClh .= $this->getClassAll($tableName, $fieldId, $fieldMain);
534
        $cClh .= $this->getClassCriteria($tableName);
535
        if (in_array(1, $fieldParentId) && $fieldElement > 15) {
536
            $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...
537
            $cClh .= $this->getClassGetTableSolenameById($moduleDirname, $table, $fieldMain);
538
        }
539
540
        $ret .= $pc->getPhpCodeClass("{$ucfModuleTable}Handler", $cClh, 'XoopsPersistableObjectHandler');
541
542
        return $ret;
543
    }
544
545
    /**
546
     *  @public function getClassCreate
547
     *
548
     *  @return string
549
     */
550
    private function getClassCreate()
551
    {
552
        $pc = TDMCreatePhpCode::getInstance();
553
        $ret = $pc->getPhpCodeCommentMultiLine(array('@param bool' => '$isNew', '' => '', '@return' => 'object'), "\t");
554
        $cClhc = $this->getSimpleString('return parent::create($isNew);', "\t\t");
555
556
        $ret .= $pc->getPhpCodeFunction('create', '$isNew = true', $cClhc, 'public ', false, "\t");
557
558
        return $ret;
559
    }
560
561
    /**
562
     *  @public function getClassGet
563
     *
564
     *  @return string
565
     */
566
    private function getClassGet()
567
    {
568
        $pc = TDMCreatePhpCode::getInstance();
569
        $ret = $pc->getPhpCodeCommentMultiLine(array('retrieve a' => 'field', '' => '', '@param int' => '$i field id', '@return mixed reference to the' => '{@link Get} object'), "\t");
570
        $cClhg = $this->getSimpleString('return parent::get($i, $fields);', "\t\t");
571
572
        $ret .= $pc->getPhpCodeFunction('get', '$i = null, $fields = null', $cClhg, 'public ', false, "\t");
573
574
        return $ret;
575
    }
576
577
    /**
578
     *  @public function getClassGetInsertId
579
     *
580
     *  @return string
581
     */
582
    private function getClassGetInsertId()
583
    {
584
        $pc = TDMCreatePhpCode::getInstance();
585
        $ret = $pc->getPhpCodeCommentMultiLine(array('get inserted' => 'id', '' => '', '@param' => 'null', '@return integer reference to the' => '{@link Get} object'), "\t");
586
        $cClhgid = $this->getSimpleString('return $this->db->getInsertId();', "\t\t");
587
588
        $ret .= $pc->getPhpCodeFunction('getInsertId', '', $cClhgid, 'public ', false, "\t");
589
590
        return $ret;
591
    }
592
593
    /**
594
     *  @public function getClassCounter
595
     *
596
     *  @param $tableName
597
     *  @param $fieldId
598
     *  @param $fieldMain
599
     *
600
     *  @return string
601
     */
602
    private function getClassCounter($tableName, $fieldId, $fieldMain)
603
    {
604
        $pc = TDMCreatePhpCode::getInstance();
605
        $xc = TDMCreateXoopsCode::getInstance();
606
        $cc = ClassXoopsCode::getInstance();
607
        $ucfTableName = ucfirst($tableName);
608
        $ret = $pc->getPhpCodeCommentMultiLine(array('Get Count '.$ucfTableName => 'in the database'), "\t");
609
610
        $critCount = $cc->getClassCriteriaCompo('crCount'.$ucfTableName, "\t\t");
611
        $paramsCrit = "\$this->get{$ucfTableName}Criteria(\$crCount{$ucfTableName}, \$start, \$limit, \$sort, \$order)";
612
        $critCount .= $xc->getXcEqualsOperator('$crCount'.$ucfTableName, $paramsCrit, null, false, "\t\t");
613
        $critCount .= $this->getSimpleString("return parent::getCount(\$crCount{$ucfTableName});", "\t\t");
614
        $params = "\$start = 0, \$limit = 0, \$sort = '{$fieldId} ASC, {$fieldMain}', \$order = 'ASC'";
615
616
        $ret .= $pc->getPhpCodeFunction('getCount'.$ucfTableName, $params, $critCount, 'public ', false, "\t");
617
618
        return $ret;
619
    }
620
621
    /**
622
     *  @public function getClassAll
623
     *
624
     *  @param $tableName
625
     *  @param $fieldId
626
     *  @param $fieldMain
627
     *
628
     *  @return string
629
     */
630
    private function getClassAll($tableName, $fieldId, $fieldMain)
631
    {
632
        $pc = TDMCreatePhpCode::getInstance();
633
        $xc = TDMCreateXoopsCode::getInstance();
634
        $cc = ClassXoopsCode::getInstance();
635
        $ucfTableName = ucfirst($tableName);
636
        $ret = $pc->getPhpCodeCommentMultiLine(array('Get All '.$ucfTableName => 'in the database'), "\t");
637
638
        $critAll = $cc->getClassCriteriaCompo('crAll'.$ucfTableName, "\t\t");
639
        $paramsCrit = "\$this->get{$ucfTableName}Criteria(\$crAll{$ucfTableName}, \$start, \$limit, \$sort, \$order)";
640
        $critAll .= $xc->getXcEqualsOperator('$crAll'.$ucfTableName, $paramsCrit, null, false, "\t\t");
641
        $critAll .= $this->getSimpleString("return parent::getAll(\$crAll{$ucfTableName});", "\t\t");
642
        $params = "\$start = 0, \$limit = 0, \$sort = '{$fieldId} ASC, {$fieldMain}', \$order = 'ASC'";
643
644
        $ret .= $pc->getPhpCodeFunction('getAll'.$ucfTableName, $params, $critAll, 'public ', false, "\t");
645
646
        return $ret;
647
    }
648
649
    /**
650
     *  @public function getClassByCategory
651
     *
652
     *  @param $tableName
653
     *  @param $fieldId
654
     *  @param $fieldMain
655
     *  @param $fieldParent
656
     *
657
     *  @return string
658
     */
659
    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...
660
    {
661
        $tc = TDMCreateHelper::getInstance();
662
        $pc = TDMCreatePhpCode::getInstance();
663
        $xc = TDMCreateXoopsCode::getInstance();
664
        $cc = ClassXoopsCode::getInstance();
665
        $ucfTableName = ucfirst($tableName);
666
        $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...
667
        $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...
668
        $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...
669
        $fieldElementName = $fieldElements->getVar('fieldelement_name');
670
        $fieldNameDesc = ucfirst(substr($fieldElementName, strrpos($fieldElementName, ':'), strlen($fieldElementName)));
671
        $topicTableName = str_replace(': ', '', $fieldNameDesc);
672
        $lcfTopicTableName = lcfirst($topicTableName);
673
674
        $ret = $pc->getPhpCodeCommentMultiLine(array("Get All {$ucfTableName} By" => "{$fieldNameDesc} Id"), "\t");
675
676
        $critAll = $xc->getXcEqualsOperator('$gpermHandler', "xoops_gethandler('groupperm')", null, true, "\t\t");
677
        $param1 = "'{$moduleDirname}_view'";
678
        $param2 = "\$GLOBALS['xoopsUser']->getGroups()";
679
        $param3 = "\$GLOBALS['xoopsModule']->getVar('mid')";
680
        $critAll .= $xc->getXcGetItemIds($lcfTopicTableName, 'gpermHandler', $param1, $param2, $param3, "\t\t");
681
        $critAll .= $cc->getClassCriteriaCompo('crAll'.$ucfTableName, "\t\t");
682
683
        if (strstr($fieldName, 'status')) {
684
            $crit = $cc->getClassCriteria('', "'{$fieldName}'", '0', "'!='", true);
685
            $critAll .= $cc->getClassAdd('crAll'.$ucfTableName, $crit, "\t\t");
686
        }
687
        $paramsCritAll = "\$this->get{$ucfTableName}Criteria(\$crAll{$ucfTableName}, \$start, \$limit, \$sort, \$order)";
688
        $critAll .= $xc->getXcEqualsOperator('$crAll'.$ucfTableName, $paramsCritAll, null, false, "\t\t");
689
690
        $critAll .= $this->getSimpleString("return parent::getAll(\$crAll{$ucfTableName});", "\t\t");
691
        $params = "\${$tableFieldName}Id, \$start = 0, \$limit = 0, \$sort = '{$fieldId} ASC, {$fieldMain}', \$order = 'ASC'";
692
693
        $ret .= $pc->getPhpCodeFunction("getAll{$ucfTableName}By{$fieldNameDesc}Id".$ucfTableName, $params, $critAll, 'public ', false, "\t");
694
695
        return $ret;
696
    }
697
698
    /**
699
     *  @public function getClassCriteria
700
     *
701
     *  @param $tableName
702
     *  @param $fieldId
703
     *  @param $fieldMain
704
     *
705
     *  @return string
706
     */
707
    private function getClassCriteria($tableName)
708
    {
709
        $pc = TDMCreatePhpCode::getInstance();
710
        $cc = ClassXoopsCode::getInstance();
711
        $ucfTableName = ucfirst($tableName);
712
        $ret = $pc->getPhpCodeCommentMultiLine(array('Get' => 'Criteria '.$ucfTableName), "\t");
713
714
        $paramsAllCriteria = "\$cr{$ucfTableName}, \$start, \$limit, \$sort, \$order";
715
716
        $critSets = $cc->getClassSetStart('cr'.$ucfTableName, 'start', "\t\t");
717
        $critSets .= $cc->getClassSetLimit('cr'.$ucfTableName, 'limit', "\t\t");
718
        $critSets .= $cc->getClassSetSort('cr'.$ucfTableName, 'sort', "\t\t");
719
        $critSets .= $cc->getClassSetOrder('cr'.$ucfTableName, 'order', "\t\t");
720
        $critSets .= $this->getSimpleString("return \$cr{$ucfTableName};", "\t\t");
721
722
        $ret .= $pc->getPhpCodeFunction("get{$ucfTableName}Criteria", $paramsAllCriteria, $critSets, 'private ', false, "\t");
723
724
        return $ret;
725
    }
726
727
    /**
728
     *  @public function getClassGetTableSolenameById
729
     *
730
     *  @param $moduleDirname
731
     *  @param $table
732
     *
733
     *  @return string
734
     */
735
    private function getClassGetTableSolenameById($moduleDirname, $table, $fieldMain)
736
    {
737
        $pc = TDMCreatePhpCode::getInstance();
738
        $xc = TDMCreateXoopsCode::getInstance();
739
        $tableName = $table->getVar('table_name');
740
        $tableSoleName = $table->getVar('table_solename');
741
        $ucfTableSoleName = ucfirst($tableSoleName);
742
        $ccTableSoleName = $this->getCamelCase($tableSoleName, true);
743
        $ret = $pc->getPhpCodeCommentMultiLine(array('Returns the' => $ucfTableSoleName.' from id', '' => '', '@return' => 'string'), "\t");
744
        $soleName = $xc->getXcEqualsOperator("\${$tableSoleName}Id", "(int)( \${$tableSoleName}Id )", null, false, "\t\t");
745
        $soleName .= $xc->getXcEqualsOperator("\${$tableSoleName}", "''", null, false, "\t\t");
746
747
        $contentIf = $xc->getXoopsHandlerLine($moduleDirname, $tableName, "\t\t\t");
748
        $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...
749
        $getVar = $xc->getXcGetVar($ccTableSoleName, "{$tableSoleName}Obj", $fieldMain, false, "\t\t\t\t");
750
        $contentIf .= $pc->getPhpCodeConditions("is_object( \${$tableSoleName}Obj )", '', '', $getVar, false, "\t\t\t");
751
752
        $soleName .= $pc->getPhpCodeConditions("\${$tableSoleName}Id", ' > ', '0', $contentIf = null, false, "\t\t");
753
        $soleName .= $this->getSimpleString("return \${$tableSoleName};", "\t\t");
754
755
        $ret .= $pc->getPhpCodeFunction("get{$ucfTableSoleName}FromId", "\${$tableSoleName}Id", $soleName, 'public ', false, "\t");
756
757
        return $ret;
758
    }
759
760
    /*
761
     * @public function render    
762
     * @param null
763
     *
764
     * @return bool|string
765
     */
766
    public function render()
767
    {
768
        $tc = TDMCreateHelper::getInstance();
769
        $module = $this->getModule();
770
        $table = $this->getTable();
771
        $filename = $this->getFileName();
772
        $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...
773
        $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...
774
        $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...
775
        $moduleDirname = $module->getVar('mod_dirname');
776
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
777
        $fieldInForm = array();
778
        $fieldParentId = array();
779
        $fieldElementId = array();
780
        $fieldParent = null;
781
        foreach (array_keys($fields) as $f) {
782
            $fieldName = $fields[$f]->getVar('field_name');
783
            $fieldInForm[] = $fields[$f]->getVar('field_inform');
784
            $fieldParentId[] = $fields[$f]->getVar('field_parent');
785
            if ((0 == $f) && (1 == $table->getVar('table_autoincrement'))) {
786
                $fieldId = $fieldName; // $fieldId = fields parameter index field
787
            }
788
            if (1 == $fields[$f]->getVar('field_main')) {
789
                $fieldMain = $fieldName; // $fieldMain = fields parameter main field
790
            }
791
            if (1 == $fields[$f]->getVar('field_parent')) {
792
                $fieldParent = $fieldName; // $fieldParent = fields parameter parent field
793
            }
794
            $fieldElement = $fields[$f]->getVar('field_element');
795
            //
796
            $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...
797
            $fieldElementId[] = $fieldElements->getVar('fieldelement_id');
798
        }
799
        $content = $this->getHeaderFilesComments($module, $filename);
800
        $content .= $this->getClassObject($module, $table, $fields);
801
        $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...
802
803
        $this->create($moduleDirname, 'class', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
804
805
        return $this->renderFile();
806
    }
807
}
808