Completed
Pull Request — master (#45)
by Gino
05:23
created

AdminXoopsCode::getAdminXoopsCodeSetVarsObjects()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 41
Code Lines 34

Duplication

Lines 41
Ratio 100 %
Metric Value
dl 41
loc 41
rs 5.2653
cc 11
eloc 34
nc 11
nop 3

How to fix   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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 28 and the first side effect is on line 24.

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

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

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

Loading history...
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
/**
13
 * tdmcreate module.
14
 *
15
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 *
18
 * @since           2.5.0
19
 *
20
 * @author          Txmod Xoops http://www.txmodxoops.org
21
 *
22
 * @version         $Id: AdminXoopsCode.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
defined('XOOPS_ROOT_PATH') || die('Restricted access');
25
/**
26
 * Class AdminXoopsCode.
27
 */
28
class AdminXoopsCode
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
{    
0 ignored issues
show
Coding Style introduced by
The opening class brace should be on a newline by itself.
Loading history...
30
    /*
31
    *  @public function constructor
32
    *  @param null
33
    */
34
    /**
35
     *
36
     */
37
    public function __construct()
38
    {
39
        $this->phpcode = TDMCreatePhpCode::getInstance();
0 ignored issues
show
Bug introduced by
The property phpcode does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40
		$this->adminobjects = TDMCreateAdminObjects::getInstance();
0 ignored issues
show
Bug introduced by
The property adminobjects does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
    }
42
43
    /*
44
    *  @static function &getInstance
45
    *  @param null
46
    */
47
    /**
48
     * @return AdminXoopsCode
49
     */
50
    public static function &getInstance()
51
    {
52
        static $instance = false;
53
        if (!$instance) {
54
            $instance = new self();
55
        }
56
57
        return $instance;
58
    }     
59
60
    /*
61
    *  @public function getAdminTemplateMain
62
    *  @param $moduleDirname
63
    *  @param $tableName
64
    *  @return string
65
    */
66
    public function getAdminTemplateMain($moduleDirname, $tableName)
67
    {
68
        $ret = <<<EOT
69
        \$templateMain = '{$moduleDirname}_admin_{$tableName}.tpl';\n
70
EOT;
71
72
        return $ret;
73
    }
74
75
    /**
76
     *  @public function getAdminXoopsTplAssign
77
     *
78
     *  @param string $tplString
79
     *  @param string $phpRender
80
     *
81
     *  @return string
82
     */
83
    public function getAdminXoopsTplAssign($tplString, $phpRender)
84
    {
85
        $ret = <<<EOT
86
        \$GLOBALS['xoopsTpl']->assign('{$tplString}', \${$phpRender});\n
87
EOT;
88
89
        return $ret;
90
    }
91
92
    /**
93
     *  @public function getAdminXoopsTplAppend
94
     *
95
     *  @param string $tplString
96
     *  @param string $phpRender
97
     *
98
     *  @return string
99
     */
100
    public function getAdminXoopsTplAppend($tplString, $phpRender)
101
    {
102
        $ret = <<<EOT
103
        \$GLOBALS['xoopsTpl']->append('{$tplString}', \${$phpRender});\n
104
EOT;
105
106
        return $ret;
107
    }
108
    
109
    /*
110
    *  @public function getAdminTemplateMain
111
    *  @param $moduleDirname
112
    *  @param $tableName
113
    *  @param $admin
114
    *  @return string
115
    */
116
    public function getAdminItemButton($moduleDirname, $tableName, $admin = false)
0 ignored issues
show
Unused Code introduced by
The parameter $moduleDirname 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...
Unused Code introduced by
The parameter $admin 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...
117
    {
118
        $ret = <<<EOT
119
        \$adminMenu->addItemButton({$language}ADD_{$stuTableSoleName}, '{$tableName}.php?op=new', 'add');
0 ignored issues
show
Bug introduced by
The variable $language does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $stuTableSoleName does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
120
        \$adminMenu->addItemButton({$language}{$stuTableName}_LIST, '{$tableName}.php', 'list');
0 ignored issues
show
Bug introduced by
The variable $stuTableName does not exist. Did you mean $tableName?

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...
121
        \$GLOBALS['xoopsTpl']->assign('navigation', \$adminMenu->addNavigation('{$tableName}.php'));
122
        \$GLOBALS['xoopsTpl']->assign('buttons', \$adminMenu->renderButton());\n
123
EOT;
124
125
        return $ret;
126
    }
127
128
    /**
129
     *  @public function getAdminAddNavigation
130
     *
131
     *  @param $tableName
132
     *
133
     *  @return string
134
     */
135
    public function getAdminAddNavigation($tableName)
136
    {
137
        $ret = <<<EOT
138
        \$GLOBALS['xoopsTpl']->assign('navigation', \$adminMenu->addNavigation('{$tableName}.php'));\n
139
EOT;
140
141
        return $ret;
142
    }
143
144
    /**
145
     *  @public function getAdminObjHandlerCreate
146
     *
147
     *  @param string $tableName
148
     *
149
     *  @return string
150
     */
151
    public function getAdminObjHandlerCreate($tableName)
152
    {
153
        $ret = <<<EOT
154
        \${$tableName}Obj =& \${$tableName}Handler->create();\n
155
EOT;
156
157
        return $ret;
158
    }
159
160
    /**
161
     *  @public function getAdminXoopsCodeSetVarsObjects
162
     *
163
     *  @param $moduleDirname
164
     *  @param $tableName
165
     *  @param $fields
166
     *
167
     *  @return string
168
     */
169 View Code Duplication
    public function getAdminXoopsCodeSetVarsObjects($moduleDirname, $tableName, $fields)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
    {
171
        $ret = <<<EOT
172
        // Set Vars\n
173
EOT;
174
        foreach (array_keys($fields) as $f) {
175
            $fieldName = $fields[$f]->getVar('field_name');
176
            $fieldElement = $fields[$f]->getVar('field_element');
177
            if ($f > 0) { // If we want to hide field id
178
                switch ($fieldElement) {
179
                    case 5:
180
                    case 6:
181
                        $ret .= $this->adminobjects->getCheckBoxOrRadioYNSetVar($tableName, $fieldName);
182
                        break;
183
                    case 11:
184
                        $ret .= $this->adminobjects->getImageListSetVar($moduleDirname, $tableName, $fieldName);
185
                        break;
186
                    case 12:
187
                        $ret .= $this->adminobjects->getUrlFileSetVar($moduleDirname, $tableName, $fieldName);
188
                        break;
189
                    case 13:
190
                        if (1 == $fields[$f]->getVar('field_main')) {
191
                            $fieldMain = $fieldName;
192
                        }
193
                        $ret .= $this->adminobjects->getUploadImageSetVar($moduleDirname, $tableName, $fieldName, $fieldMain);
0 ignored issues
show
Bug introduced by
The variable $fieldMain does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
194
                        break;
195
                    case 14:
196
                        $ret .= $this->adminobjects->getUploadFileSetVar($moduleDirname, $tableName, $fieldName);
197
                        break;
198
                    case 15:
199
                        $ret .= $this->adminobjects->getTextDateSelectSetVar($tableName, $fieldName);
200
                        break;
201
                    default:
202
                        $ret .= $this->adminobjects->getSimpleSetVar($tableName, $fieldName);
203
                        break;
204
                }
205
            }
206
        }
207
208
        return $ret;
209
    }
210
211
    /**
212
     *  @public function getAdminXoopsCodeXoopsSecurity
213
     *
214
     *  @param $tableName
215
     *
216
     *  @return string
217
     */
218
    public function getAdminXoopsCodeXoopsSecurity($tableName)
219
    {
220
        $ret = <<<EOT
221
        if ( !\$GLOBALS['xoopsSecurity']->check() ) {
222
			redirect_header('{$tableName}.php', 3, implode(',', \$GLOBALS['xoopsSecurity']->getErrors()));
223
        }\n
224
EOT;
225
226
        return $ret;
227
    }
228
229
    /*
230
    *  @public function getAdminXoopsCodeInsertData
231
    *  @param $tableName
232
    *  @param $language
233
    *  @return string
234
    */
235
    public function getAdminXoopsCodeInsertData($tableName, $language)
236
    {
237
        $ret = <<<EOT
238
        // Insert Data
239
        if (\${$tableName}Handler->insert(\${$tableName}Obj)) {
240
			redirect_header('{$tableName}.php?op=list', 2, {$language}FORM_OK);
241
        }\n
242
EOT;
243
244
        return $ret;
245
    }
246
247
    /**
248
     *  @public function getAdminXoopsCodeGetFormError
249
     *
250
     *  @param $tableName
251
     *
252
     *  @return string
253
     */
254
    public function getAdminXoopsCodeGetFormError($tableName)
255
    {
256
        $ret = <<<EOT
257
        // Get Form
258
        \$GLOBALS['xoopsTpl']->assign('error', \${$tableName}Obj->getHtmlErrors());
259
        \$form =& \${$tableName}Obj->getForm();
260
        \$GLOBALS['xoopsTpl']->assign('form', \$form->render());\n
261
EOT;
262
263
        return $ret;
264
    }
265
266
    /**
267
     *  @public function getAdminXoopsCodeGetFormId
268
     *
269
     *  @param string $tableName
270
     *  @param string $fieldId
271
     *
272
     *  @return string
273
     */
274
    public function getAdminXoopsCodeGetFormId($tableName, $fieldId)
275
    {
276
        $ret = <<<EOT
277
        // Get Form
278
        \${$tableName}Obj = \${$tableName}Handler->get(\${$fieldId});
279
        \$form = \${$tableName}Obj->getForm();
280
        \$GLOBALS['xoopsTpl']->assign('form', \$form->render());\n
281
EOT;
282
283
        return $ret;
284
    }
285
286
    /**
287
     *  @public function getAdminXoopsCodeGetObjHandlerId
288
     *
289
     *  @param string $tableName
290
     *  @param string $fieldId
291
     *
292
     *  @return string
293
     */
294
    public function getAdminXoopsCodeGetObjHandlerId($tableName, $fieldId)
295
    {
296
        $ret = <<<EOT
297
        \${$tableName}Obj =& \${$tableName}Handler->get(\${$fieldId});\n
298
EOT;
299
300
        return $ret;
301
    }
302
303
    /*
304
    *  @public function getAdminXoopsCodeDelete
305
    *  @param string $tableName
306
    *  @param string $language
307
    *  @param string $fieldId
308
    *  @param string $fieldMain
309
    *  @return string
310
    */
311 View Code Duplication
    public function getAdminXoopsCodeDelete($tableName, $language, $fieldId, $fieldMain)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
312
    {
313
        $ret = <<<EOT
314
    case 'delete':
315
        \${$tableName}Obj =& \${$tableName}Handler->get(\${$fieldId});
316
        if (isset(\$_REQUEST['ok']) && 1 == \$_REQUEST['ok']) {
317
            if ( !\$GLOBALS['xoopsSecurity']->check() ) {
318
                redirect_header('{$tableName}.php', 3, implode(', ', \$GLOBALS['xoopsSecurity']->getErrors()));
319
            }
320
            if (\${$tableName}Handler->delete(\${$tableName}Obj)) {
321
                redirect_header('{$tableName}.php', 3, {$language}FORMDELOK);
322
            } else {
323
                echo \${$tableName}Obj->getHtmlErrors();
324
            }
325
        } else {
326
            xoops_confirm(array('ok' => 1, '{$fieldId}' => \${$fieldId}, 'op' => 'delete'), \$_SERVER['REQUEST_URI'], sprintf({$language}FORMSUREDEL, \${$tableName}Obj->getVar('{$fieldMain}')));
327
        }
328
    break;\n
329
EOT;
330
331
        return $ret;
332
    }
333
334
    /*
335
    *  @public function getAdminXoopsCodeUpdate
336
    *  @param string $moduleDirname
337
    *  @param string $tableName
338
    *  @param string $language
339
    *  @param string $fieldId
340
    *  @param string $fieldMain
341
    *  @return string
342
    */
343 View Code Duplication
    public function getAdminXoopsCodeUpdate($moduleDirname, $tableName, $language, $fieldId, $fieldMain)
0 ignored issues
show
Unused Code introduced by
The parameter $language 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...
Unused Code introduced by
The parameter $fieldMain 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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
344
    {
345
        $upModuleName = strtoupper($moduleDirname);
346
        $ret = <<<EOT
347
    case 'update':
348
        if (isset(\${$fieldId})) {
349
            \${$tableName}Obj =& \${$tableName}Handler->get(\${$fieldId});
350
        }
351
        \${$tableName}Obj->setVar("\${$tableName}_display", \$_POST["\${$tableName}_display"]);
352
353
        if (\${$tableName}Handler->insert(\${$tableName}Obj)) {
354
            redirect_header("\${$tableName}.php", 3, _AM_{$upModuleName}_FORMOK);
355
        }
356
        echo \${$tableName}Obj->getHtmlErrors();
357
    break;\n
358
EOT;
359
360
        return $ret;
361
    }    
362
}
363