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

UserXoopsVersion::getModVersionArrayElements()   D

Complexity

Conditions 11
Paths 512

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 21
rs 4.7532
cc 11
eloc 14
nc 512
nop 6

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 29 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: xoopsversion_file.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
defined('XOOPS_ROOT_PATH') || die('Restricted access');
25
26
/**
27
 * Class UserXoopsVersion.
28
 */
29
class UserXoopsVersion 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...
30
{
31
    /*
32
    * @var array
33
    */
34
    private $keywords = array();
35
36
    /*
37
    *  @public function constructor
38
    *  @param null
39
    */
40
    /**
41
     *
42
     */
43
    public function __construct()
44
    {
45
        parent::__construct();
46
        $this->tdmcfile = TDMCreateFile::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like \TDMCreateFile::getInstance() of type object<TDMCreateFile> is incompatible with the declared type string of property $tdmcfile.

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

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

Loading history...
47
    }
48
49
    /*
50
    *  @static function &getInstance
51
    *  @param null
52
    */
53
    /**
54
     * @return UserXoopsVersion
55
     */
56
    public static function &getInstance()
57
    {
58
        static $instance = false;
59
        if (!$instance) {
60
            $instance = new self();
61
        }
62
63
        return $instance;
64
    }
65
66
    /*
67
    *  @public function write
68
    *  @param $module
69
    *  @param mixed $table
70
    *  @param mixed $tables
71
    *  @param $filename
72
    */
73
    /**
74
     * @param $module
75
     * @param $table
76
     * @param $tables
77
     * @param $filename
78
     */
79
    public function write($module, $table, $tables, $filename)
80
    {
81
        $this->setModule($module);
82
        $this->setTable($table);
83
        $this->setTables($tables);
84
        $this->setFileName($filename);
85
        foreach (array_keys($tables) as $t) {
86
            $tableName = $tables[$t]->getVar('table_name');
87
            $this->setKeywords($tableName);
88
        }
89
    }
90
91
    /*
92
    *  @public function setKeywords
93
    *  @param mixed $keywords
94
    */
95
    /**
96
     * @param $keywords
97
     */
98
    public function setKeywords($keywords)
99
    {
100
        if (is_array($keywords)) {
101
            $this->keywords = $keywords;
102
        } else {
103
            $this->keywords[] = $keywords;
104
        }
105
    }
106
107
    /*
108
    *  @public function getKeywords
109
    *  @param null
110
    */
111
    /**
112
     * @return array
113
     */
114
    public function getKeywords()
115
    {
116
        return $this->keywords;
117
    }
118
119
    /**
120
     * @private function getModVersionHeaderComment
121
     *
122
     * @param $comment
123
     *
124
     * @return string
125
     */
126
    private function getModVersionHeaderComment($comment)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
127
    {
128
        $ret = <<<EOT
129
// ------------------- {$comment} ------------------- //
130
EOT;
131
132
        return $ret;
133
    }
134
    	
135
	/**
136
     * @private function getModVersionArrayElements
137
     *
138
     * @param $element
139
	 * @param $left
140
	 * @param $index
141
	 * @param $right
142
	 * @param $desc
143
	 * @param $arrayOptions
144
     *
145
     * @return string
146
     */
147
    private function getModVersionArrayElements($element = 1, $left, $index = '', $right = '', $desc = '', $arrayOptions = '')
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
148
    {
149
		$index = (is_string($index) && !empty($index)) ? "'{$index}'": $index;
150
		$right = (is_string($right) && !empty($right)) ? "'{$right}'": $right;
151
		$desc = is_string($desc) ? "'{$desc}'": $desc;
152
		if ($element == 1) {
153
			$ret = "\$modversion['{$left}'] = {$desc};\n";
154
		}
155
		if ($element == 2) {
156
			$ret = "\$modversion['{$left}'][{$index}] = {$desc};\n";
157
		}
158
		if ($element == 3) {
159
			$ret = "\$modversion['{$left}'][{$index}][{$right}] = {$desc};\n";
160
		}
161
		if (($element == 3) && !empty($arrayOptions)) {
162
			$ret = "\$modversion['{$left}'][{$index}][{$right}] = {$desc};";
163
			$ret .= "\$modversion['{$left}'][{$index}][{$right}] = {$arrayOptions};\n";
164
		}
165
166
        return $ret;
0 ignored issues
show
Bug introduced by
The variable $ret does not seem to be defined for all execution paths leading up to this point.

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

Let’s take a look at an example:

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

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

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

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

Available Fixes

  1. Check for existence of the variable explicitly:

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

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

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
167
    }
168
    
169
    /*
170
    *  @private function getXoopsVersionHeader
171
    *  @param $language
172
    */
173
    /**
174
     * @param $module
175
     * @param $table
176
     * @param $language
177
     *
178
     * @return string
179
     */
180
    private function getXoopsVersionHeader($module, $table, $language)
181
    {
182
        //$dateString = preg_replace('/[^0-9]/', '/', _DBDATESTRING);
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
183
        $date = date(_DBDATESTRING); // _DBDATESTRING
184
        $ret = <<<EOT
185
defined('XOOPS_ROOT_PATH') || die('Restricted access');
186
//
187
//\$dirname = basename(__DIR__);
188
// ------------------- Informations ------------------- //
189
\$modversion = array(
190
    'name' => {$language}NAME,
191
    'version' => {$module->getVar('mod_version')},
192
    'description' => {$language}DESC,
193
    'author' => '{$module->getVar('mod_author')}',
194
    'author_mail' => '{$module->getVar('mod_author_mail')}',
195
    'author_website_url' => '{$module->getVar('mod_author_website_url')}',
196
    'author_website_name' => '{$module->getVar('mod_author_website_name')}',
197
    'credits' => '{$module->getVar('mod_credits')}',
198
    'license' => '{$module->getVar('mod_license')}',
199
    'license_url' => 'www.gnu.org/licenses/gpl-2.0.html/',
200
	'help' => 'page=help',
201
    //
202
    'release_info' => '{$module->getVar('mod_release_info')}',
203
    'release_file' => XOOPS_URL . '/modules/{$module->getVar('mod_dirname')}/docs/{$module->getVar('mod_release_file')}',
204
    'release_date' => '{$date}',
205
    //
206
    'manual' => '{$module->getVar('mod_manual')}',
207
    'manual_file' => XOOPS_URL . '/modules/{$module->getVar('mod_dirname')}/docs/{$module->getVar('mod_manual_file')}',
208
    'min_php' => '{$module->getVar('mod_min_php')}',
209
    'min_xoops' => '{$module->getVar('mod_min_xoops')}',
210
    'min_admin' => '{$module->getVar('mod_min_admin')}',
211
    'min_db' => array('mysql' => '{$module->getVar('mod_min_mysql')}', 'mysqli' => '{$module->getVar('mod_min_mysql')}'),
212
    'image' => 'assets/images/{$module->getVar('mod_image')}',
213
    'dirname' => '{$module->getVar('mod_dirname')}',
214
    // Frameworks
215
    'dirmoduleadmin' => 'Frameworks/moduleclasses/moduleadmin',
216
    'sysicons16' => '../../Frameworks/moduleclasses/icons/16',
217
    'sysicons32' => '../../Frameworks/moduleclasses/icons/32',
218
    // Local path icons
219
    'modicons16' => 'assets/icons/16',
220
    'modicons32' => 'assets/icons/32',
221
    //About
222
    'demo_site_url' => '{$module->getVar('mod_demo_site_url')}',
223
    'demo_site_name' => '{$module->getVar('mod_demo_site_name')}',
224
    'support_url' => '{$module->getVar('mod_support_url')}',
225
    'support_name' => '{$module->getVar('mod_support_name')}',
226
    'module_website_url' => '{$module->getVar('mod_website_url')}',
227
    'module_website_name' => '{$module->getVar('mod_website_name')}',
228
    'release' => '{$module->getVar('mod_release')}',
229
    'module_status' => '{$module->getVar('mod_status')}',
230
    // Admin system menu
231
    'system_menu' => 1,
232
    // Admin things
233
    'hasAdmin' => 1,
234
    'adminindex' => 'admin/index.php',
235
    'adminmenu' => 'admin/menu.php',\n
236
EOT;
237
        if (1 == $module->getVar('mod_user')) {
238
            $ret .= <<<EOT
239
    // Main things
240
    'hasMain' => 1,\n
241
EOT;
242
        } else {
243
            $ret .= <<<EOT
244
    // Main things
245
    'hasMain' => 0,\n
246
EOT;
247
        }
248
        $ret .= <<<EOT
249
    // Install/Update
250
    'onInstall' => 'include/install.php',\n
251
EOT;
252
        if (is_object($table) && $table->getVar('table_name') != null) {
253
            $ret .= <<<EOT
254
    'onUpdate' => 'include/update.php'\n
255
EOT;
256
        }
257
        $ret .= <<<EOT
258
);\n
259
EOT;
260
261
        return $ret;
262
    }
263
264
    /*
265
    *  @private function getXoopsVersionMySQL
266
    *  @param $moduleDirname
267
    *  @param $table
268
    */
269
    /**
270
     * @param $moduleDirname
271
     * @param $table
272
     *
273
     * @return string
274
     */
275
    private function getXoopsVersionMySQL($moduleDirname, $table, $tables)
276
    {
277
        $tableName = $table->getVar('table_name');
278
        $n = 1;
279
        $ret = '';
280
        if (!empty($tableName)) {
281
            $ret .= <<<EOT
282
// ------------------- Mysql ------------------- //
283
\$modversion['sqlfile']['mysql'] = 'sql/mysql.sql';
284
// Tables\n
285
EOT;
286
            foreach (array_keys($tables) as $t) {
287
                $ret .= <<<EOT
288
\$modversion['tables'][{$n}] = '{$moduleDirname}_{$tables[$t]->getVar('table_name')}';\n
289
EOT;
290
                ++$n;
291
            }
292
            unset($n);
293
        }
294
        $ret .= <<<EOT
295
\n
296
EOT;
297
298
        return $ret;
299
    }
300
301
    /*
302
    *  @private function getXoopsVersionSearch
303
    *  @param $moduleDirname
304
    */
305
    /**
306
     * @param $moduleDirname
307
     *
308
     * @return string
309
     */
310
    private function getXoopsVersionSearch($moduleDirname)
311
    {
312
        $ret = <<<EOT
313
// ------------------- Search ------------------- //
314
\$modversion['hasSearch'] = 1;
315
\$modversion['search']['file'] = 'include/search.inc.php';
316
\$modversion['search']['func'] = '{$moduleDirname}_search';\n\n
317
EOT;
318
319
        return $ret;
320
    }
321
322
    /*
323
    *  @private function getXoopsVersionComments
324
    *  @param $moduleDirname
325
    */
326
    /**
327
     * @param $moduleDirname
328
     *
329
     * @return string
330
     */
331
    private function getXoopsVersionComments($moduleDirname)
332
    {
333
        $ret = <<<EOT
334
// ------------------- Comments ------------------- //
335
\$modversion['comments']['pageName'] = 'comments.php';
336
\$modversion['comments']['itemName'] = 'com_id';
337
// Comment callback functions
338
\$modversion['comments']['callbackFile'] = 'include/comment_functions.php';
339
\$modversion['comments']['callback']['approve'] = '{$moduleDirname}CommentsApprove';
340
\$modversion['comments']['callback']['update'] = '{$moduleDirname}CommentsUpdate';\n\n
341
EOT;
342
343
        return $ret;
344
    }
345
346
    /*
347
    *  @private function getXoopsVersionTemplatesAdmin
348
    *  @param $moduleDirname
349
    */
350
    /**
351
     * @param $moduleDirname
352
     * @param $table
353
     *
354
     * @return string
355
     */
356
    private function getXoopsVersionTemplatesAdmin($moduleDirname, $table, $tables)
0 ignored issues
show
Unused Code introduced by
The parameter $table 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...
357
    {
358
        $ret = <<<EOT
359
// ------------------- Templates ------------------- //
360
// Admin\n
361
EOT;
362
        $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, 'about', false, true);
363
        $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, 'header', false, true);
364
        $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, 'index', false, true);
365 View Code Duplication
        foreach (array_keys($tables) as $t) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
366
            $tableName = $tables[$t]->getVar('table_name');
367
            $tablePermissions[] = $tables[$t]->getVar('table_permissions');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tablePermissions was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tablePermissions = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
368
            $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, $tableName, false, true);
369
        }
370
        if (in_array(1, $tablePermissions)) {
0 ignored issues
show
Bug introduced by
The variable $tablePermissions 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...
371
            $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, 'permissions', false, true);
372
        }
373
        $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, 'footer', false, true);
374
375
        return $ret;
376
    }
377
378
    /*
379
    *  @private function getXoopsVersionTemplatesLine
380
    *  @param $moduleDirname
381
    */
382
    /**
383
     * @param $moduleDirname
384
     *
385
     * @return string
386
     */
387
    private function getXoopsVersionTemplatesLine($moduleDirname, $type, $extra = false, $isAdmin = false)
388
    {
389
        if ($isAdmin) {
390
            $ret = <<<EOT
391
\$modversion['templates'][] = array('file' => '{$moduleDirname}_admin_{$type}.tpl', 'description' => '', 'type' => 'admin');\n
392
EOT;
393
        } else {
394
            if (!$extra) {
395
                $ret = <<<EOT
396
\$modversion['templates'][] = array('file' => '{$moduleDirname}_{$type}.tpl', 'description' => '');\n
397
EOT;
398
            } else {
399
                $ret = <<<EOT
400
\$modversion['templates'][] = array('file' => '{$moduleDirname}_{$type}_{$extra}.tpl', 'description' => '');\n
401
EOT;
402
            }
403
        }
404
405
        return $ret;
406
    }
407
408
    /*
409
    *  @private function getXoopsVersionTemplatesUser
410
    *  @param $moduleDirname
411
    */
412
    /**
413
     * @param $moduleDirname
414
     *
415
     * @return string
416
     */
417
    private function getXoopsVersionTemplatesUser($moduleDirname, $tables)
418
    {
419
        $table = $this->getTable();
420
        $ret = <<<EOT
421
// User\n
422
EOT;
423
        $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, 'header');
424
        $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, 'index');
425
        foreach (array_keys($tables) as $t) {
426
            $tableName = $tables[$t]->getVar('table_name');
427
            $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, $tableName);
428
            $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, $tableName, 'list');
0 ignored issues
show
Documentation introduced by
'list' is of type string, but the function expects a boolean.

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...
429
        }
430
        $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, 'breadcrumbs');
431
        if (1 == $table->getVar('table_broken')) {
432
            $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, 'broken');
433
        }
434
        if (1 == $table->getVar('table_pdf')) {
435
            $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, 'pdf');
436
        }
437
        if (1 == $table->getVar('table_print')) {
438
            $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, 'print');
439
        }
440
        if (1 == $table->getVar('table_rate')) {
441
            $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, 'rate');
442
        }
443
        if (1 == $table->getVar('table_rss')) {
444
            $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, 'rss');
445
        }
446
        if (1 == $table->getVar('table_search')) {
447
            $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, 'search');
448
        }
449
        if (1 == $table->getVar('table_single')) {
450
            $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, 'single');
451
        }
452
        if (1 == $table->getVar('table_submit')) {
453
            $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, 'submit');
454
        }
455
        $ret .= $this->getXoopsVersionTemplatesLine($moduleDirname, 'footer');
456
457
        return $ret;
458
    }
459
460
    /*
461
    *  @private function getXoopsVersionSubmenu
462
    *  @param $language
463
    */
464
    /**
465
     * @param $language
466
     *
467
     * @return string
468
     */
469
    private function getXoopsVersionSubmenu($language, $tables)
470
    {
471
        $ret = <<<EOT
472
// ------------------- Submenu ------------------- //\n
473
EOT;
474
        $i = 1;
475
        foreach (array_keys($tables) as $t) {
476
            $tableName = $tables[$t]->getVar('table_name');
477
            $tableSubmit[] = $tables[$t]->getVar('table_submit');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tableSubmit was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tableSubmit = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
478
            if (1 == $tables[$t]->getVar('table_submenu')) {
479
                $ret .= <<<EOT
480
// Sub {$tableName}
481
\$modversion['sub'][{$i}]['name'] = {$language}SMNAME{$i};
482
\$modversion['sub'][{$i}]['url'] = '{$tableName}.php';\n
483
EOT;
484
            }
485
            ++$i;
486
        }
487
        if (in_array(1, $tableSubmit)) {
0 ignored issues
show
Bug introduced by
The variable $tableSubmit 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...
488
            $ret .= <<<EOT
489
// Sub Submit
490
\$modversion['sub'][{$i}]['name'] = {$language}SMNAME{$i};
491
\$modversion['sub'][{$i}]['url'] = 'submit.php';\n
492
EOT;
493
        }
494
        unset($i);
495
        $ret .= <<<EOT
496
\n
497
EOT;
498
499
        return $ret;
500
    }
501
502
    /*
503
    *  @private function getXoopsVersionBlocks
504
    *  @param $moduleDirname
505
    *  @param $language
506
    */
507
    /**
508
     * @param $moduleDirname
509
     * @param $language
510
     *
511
     * @return string
512
     */
513
    private function getXoopsVersionBlocks($moduleDirname, $table, $tables, $language)
0 ignored issues
show
Unused Code introduced by
The parameter $table 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...
514
    {
515
        $ret = <<<EOT
516
// ------------------- Blocks ------------------- //\n
517
EOT;
518
519
        foreach (array_keys($tables) as $i) {
520
            $tableName = $tables[$i]->getVar('table_name');
521
            $tableFieldName = $tables[$i]->getVar('table_fieldname');
522
            if (1 == $tables[$i]->getVar('table_blocks')) {
523
                if (1 == $tables[$i]->getVar('table_category')) {
524
                    $ret .= $this->getXoopsVersionTypeBlocks($moduleDirname, $tableName, $language, $tableFieldName);
525
                } else {
526
                    $ret .= $this->getXoopsVersionTypeBlocks($moduleDirname, $tableName, $language, 'last');
527
                    $ret .= $this->getXoopsVersionTypeBlocks($moduleDirname, $tableName, $language, 'new');
528
                    $ret .= $this->getXoopsVersionTypeBlocks($moduleDirname, $tableName, $language, 'hits');
529
                    $ret .= $this->getXoopsVersionTypeBlocks($moduleDirname, $tableName, $language, 'top');
530
                    $ret .= $this->getXoopsVersionTypeBlocks($moduleDirname, $tableName, $language, 'random');
531
                }
532
            }
533
        }
534
535
        return $ret;
536
    }
537
538
    /*
539
    *  @private function getXoopsVersionTypeBlocks
540
    *  @param $moduleDirname
541
    *  @param $language
542
    */
543
    /**
544
     * @param $moduleDirname
545
     * @param $language
546
     *
547
     * @return string
548
     */
549 View Code Duplication
    private function getXoopsVersionTypeBlocks($moduleDirname, $tableName, $language, $type)
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...
550
    {
551
        $stuTableName = strtoupper($tableName);
552
        $ret = <<<EOT
553
\$modversion['blocks'][] = array(
554
    'file' => '{$tableName}.php',
555
    'name' => {$language}{$stuTableName}_BLOCK,
556
    'description' => {$language}{$stuTableName}_BLOCK_DESC,
557
    'show_func' => 'b_{$moduleDirname}_{$tableName}_show',
558
    'edit_func' => 'b_{$moduleDirname}_{$tableName}_edit',
559
    'options' => '{$type}|5|25|0',
560
    'template' => '{$moduleDirname}_block_{$tableName}.tpl');\n\n
561
EOT;
562
563
        return $ret;
564
    }
565
566
    /*
567
    *  @private function getXoopsVersionConfig
568
    *  @param $moduleDirname
569
    *  @param $language
570
    */
571
    /**
572
     * @param $module
573
     * @param $table
574
     * @param $language
575
     *
576
     * @return string
577
     */
578
    private function getXoopsVersionConfig($module, $table, $language)
579
    {
580
        $moduleDirname = $module->getVar('mod_dirname');
581
        $ret = <<<EOT
582
// ------------------- Config ------------------- //\n
583
EOT;
584
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
585 View Code Duplication
        foreach (array_keys($fields) as $f) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
586
            $fieldElement = $fields[$f]->getVar('field_element');
587
            if ($fieldElement == 4) {
588
                $fieldName = $fields[$f]->getVar('field_name');
589
                $rpFieldName = $this->tdmcfile->getRightString($fieldName);
0 ignored issues
show
Bug introduced by
The method getRightString cannot be called on $this->tdmcfile (of type string).

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

Loading history...
590
                $ret .= <<<EOT
591
// Editor
592
xoops_load('xoopseditorhandler');
593
\$editorHandler = XoopsEditorHandler::getInstance();
594
\$modversion['config'][] = array(
595
    'name' => '{$moduleDirname}_editor_{$rpFieldName}',
596
    'title' => '{$language}EDITOR',
597
    'description' => '{$language}EDITOR_DESC',
598
    'formtype' => 'select',
599
    'valuetype' => 'text',
600
    'options' => array_flip(\$editorHandler->getList()),
601
    'default' => 'dhtml');\n\n
602
EOT;
603
            }
604
        }
605
        if (1 == $table->getVar('table_permissions')) {
606
            $ret .= <<<EOT
607
// Get groups
608
\$memberHandler =& xoops_gethandler('member');
609
\$xoopsgroups = \$memberHandler->getGroupList();
610
foreach (\$xoopsgroups as \$key => \$group) {
611
    \$groups[\$group] = \$key;
612
}
613
\$modversion['config'][] = array(
614
    'name' => 'groups',
615
    'title' => '{$language}GROUPS',
616
    'description' => '{$language}GROUPS_DESC',
617
    'formtype' => 'select_multi',
618
    'valuetype' => 'array',
619
    'options' => \$groups,
620
    'default' => \$groups);
621
622
// Get Admin groups
623
\$criteria = new CriteriaCompo();
624
\$criteria->add( new Criteria( 'group_type', 'Admin' ) );
625
\$memberHandler =& xoops_gethandler('member');
626
\$admin_xoopsgroups = \$memberHandler->getGroupList(\$criteria);
627
foreach (\$admin_xoopsgroups as \$key => \$admin_group) {
628
    \$admin_groups[\$admin_group] = \$key;
629
}
630
\$modversion['config'][] = array(
631
    'name' => 'admin_groups',
632
    'title' => '{$language}ADMIN_GROUPS',
633
    'description' => '{$language}ADMIN_GROUPS_DESC',
634
    'formtype' => 'select_multi',
635
    'valuetype' => 'array',
636
    'options' => \$admin_groups,
637
    'default' => \$admin_groups);\n\n
638
EOT;
639
        }
640
        $keyword = implode(', ', $this->getKeywords());
641
        $ret .= <<<EOT
642
\$modversion['config'][] = array(
643
    'name' => 'keywords',
644
    'title' => '{$language}KEYWORDS',
645
    'description' => '{$language}KEYWORDS_DESC',
646
    'formtype' => 'textbox',
647
    'valuetype' => 'text',
648
    'default' => '{$moduleDirname}, {$keyword}');\n\n
649
EOT;
650
        unset($this->keywords);
651
        if (is_object($table)) {
652
            foreach (array_keys($fields) as $f) {
653
                $fieldElement = $fields[$f]->getVar('field_element');
654
                if ((10 == $fieldElement) || (11 == $fieldElement) ||
655
                    (12 == $fieldElement) || (13 == $fieldElement) || (14 == $fieldElement)
656
                ) {
657
                    $ret .= <<<EOT
658
//Uploads : maxsize of image
659
\$modversion['config'][] = array(
660
    'name' => 'maxsize',
661
    'title' => '{$language}MAXSIZE',
662
    'description' => '{$language}MAXSIZE_DESC',
663
    'formtype' => 'textbox',
664
    'valuetype' => 'int',
665
    'default' => 5000000);
666
667
//Uploads : mimetypes of image
668
\$modversion['config'][] = array(
669
    'name' => 'mimetypes',
670
    'title' => '{$language}MIMETYPES',
671
    'description' => '{$language}MIMETYPES_DESC',
672
    'formtype' => 'select_multi',
673
    'valuetype' => 'array',
674
    'default' => array('image/gif', 'image/jpeg', 'image/png'),
675
    'options' => array('bmp' => 'image/bmp','gif' => 'image/gif','pjpeg' => 'image/pjpeg',
676
                       'jpeg' => 'image/jpeg','jpg' => 'image/jpg','jpe' => 'image/jpe',
677
                       'png' => 'image/png'));\n\n
678
EOT;
679
                }
680
            }
681
            if (1 == $table->getVar('table_admin')) {
682
                $ret .= <<<EOT
683
\$modversion['config'][] = array(
684
    'name' => 'adminpager',
685
    'title' => '{$language}ADMIN_PAGER',
686
    'description' => '{$language}ADMIN_PAGER_DESC',
687
    'formtype' => 'textbox',
688
    'valuetype' => 'int',
689
    'default' => 10);\n\n
690
EOT;
691
            }
692
            if (1 == $table->getVar('table_user')) {
693
                $ret .= <<<EOT
694
\$modversion['config'][] = array(
695
    'name' => 'userpager',
696
    'title' => '{$language}USER_PAGER',
697
    'description' => '{$language}USER_PAGER_DESC',
698
    'formtype' => 'textbox',
699
    'valuetype' => 'int',
700
    'default' => 10);\n\n
701
EOT;
702
            }
703
        }
704
        if (1 == $table->getVar('table_tag')) {
705
            $ret .= <<<EOT
706
\$modversion['config'][] = array(
707
    'name' => 'usetag',
708
    'title' => '{$language}USE_TAG',
709
    'description' => '{$language}USE_TAG_DESC',
710
    'formtype' => 'yesno',
711
    'valuetype' => 'int',
712
    'default' => 0);\n\n
713
EOT;
714
        }
715
        $ret .= <<<EOT
716
\$modversion['config'][] = array(
717
    'name' => 'numb_col',
718
    'title' => '{$language}NUMB_COL',
719
    'description' => '{$language}NUMB_COL_DESC',
720
    'formtype' => 'select',
721
    'valuetype' => 'int',
722
    'default' => 1,
723
	'options' => array(1 => '1', 2 => '2', 3 => '3', 4 => '4'));
724
725
\$modversion['config'][] = array(
726
    'name' => 'divideby',
727
    'title' => '{$language}DIVIDEBY',
728
    'description' => '{$language}DIVIDEBY_DESC',
729
    'formtype' => 'select',
730
    'valuetype' => 'int',
731
    'default' => 1,
732
	'options' => array(1 => '1', 2 => '2', 3 => '3', 4 => '4'));
733
734
\$modversion['config'][] = array(
735
    'name' => 'table_type',
736
    'title' => '{$language}TABLE_TYPE',
737
    'description' => '{$language}TABLE_TYPE_DESC',
738
    'formtype' => 'select',
739
    'valuetype' => 'text',
740
    'default' => 'bordered',
741
    'options' => array('bordered' => 'bordered', 'striped' => 'striped', 'hover' => 'hover', 'condensed' => 'condensed'));
742
743
\$modversion['config'][] = array(
744
    'name' => 'panel_type',
745
    'title' => '{$language}PANEL_TYPE',
746
    'description' => '{$language}PANEL_TYPE_DESC',
747
    'formtype' => 'select',
748
    'valuetype' => 'text',
749
    'default' => 'default',
750
    'options' => array('default' => 'default', 'primary' => 'primary', 'success' => 'success', 'info' => 'info', 'warning' => 'warning', 'danger' => 'danger'));
751
752
\$modversion['config'][] = array(
753
    'name' => 'advertise',
754
    'title' => '{$language}ADVERTISE',
755
    'description' => '{$language}ADVERTISE_DESC',
756
    'formtype' => 'textarea',
757
    'valuetype' => 'text',
758
    'default' => '');
759
760
\$modversion['config'][] = array(
761
    'name' => 'bookmarks',
762
    'title' => '{$language}BOOKMARKS',
763
    'description' => '{$language}BOOKMARKS_DESC',
764
    'formtype' => 'yesno',
765
    'valuetype' => 'int',
766
    'default' => 0);
767
768
\$modversion['config'][] = array(
769
    'name' => 'facebook_comments',
770
    'title' => '{$language}FACEBOOK_COMMENTS',
771
    'description' => '{$language}FACEBOOK_COMMENTS_DESC',
772
    'formtype' => 'yesno',
773
    'valuetype' => 'int',
774
    'default' => 0);
775
776
\$modversion['config'][] = array(
777
    'name' => 'disqus_comments',
778
    'title' => '{$language}DISQUS_COMMENTS',
779
    'description' => '{$language}DISQUS_COMMENTS_DESC',
780
    'formtype' => 'yesno',
781
    'valuetype' => 'int',
782
    'default' => 0);
783
	
784
\$modversion['config'][] = array(
785
    'name' => 'maintainedby',
786
    'title' => '{$language}MAINTAINEDBY',
787
    'description' => '{$language}MAINTAINEDBY_DESC',
788
    'formtype' => 'textbox',
789
    'valuetype' => 'text',
790
    'default' => '{$module->getVar('mod_support_url')}');\n\n
791
EOT;
792
793
        return $ret;
794
    }
795
796
    /*
797
    *  @private function getTypeNotifications
798
    *  @param $language
799
    *  @param $type
800
    *  @param $tableName
801
    *  @param $item
802
    *  @param $typeOfNotify
803
    */
804
    /**
805
     * @param $language
806
     * @param $type
807
     * @param $tableName
808
     * @param $notifyFile
809
     * @param $item
810
     * @param $typeOfNotify
811
     *
812
     * @return string
813
     */
814
    private function getTypeNotifications($language, $type = 'category', $tableName, $notifyFile, $item, $typeOfNotify)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
815
    {
816
        $stuTableName = strtoupper($tableName);
817
        $stuTypeOfNotify = strtoupper($typeOfNotify);
818
        $notifyFile = explode(', ', $notifyFile);
819
        $notifyFile = implode(', ', $notifyFile);
820
        switch ($type) {
821
            case 'category':
822
                $ret = <<<EOT
823
\$modversion['notification']['{$type}'][] = array(
824
    'name' => 'category',
825
    'title' => {$language}{$stuTableName}_NOTIFY,
826
    'description' => {$language}{$stuTableName}_NOTIFY_DESC,
827
    'subscribe_from' => array('index.php',{$notifyFile}),
828
    'item_name' => '{$item}',
829
    'allow_bookmark' => 1);\n
830
EOT;
831
                break;
832
            case 'event':
833
                $ret = <<<EOT
834
\$modversion['notification']['{$type}'][] = array(
835
    'name' => '{$typeOfNotify}',
836
    'category' => '{$tableName}',
837
    'admin_only' => 1,
838
    'title' => {$language}{$stuTableName}_{$stuTypeOfNotify}_NOTIFY,
839
    'caption' => {$language}{$stuTableName}_{$stuTypeOfNotify}_NOTIFY_CAPTION,
840
    'description' => {$language}{$stuTableName}_{$stuTypeOfNotify}_NOTIFY_DESC,
841
    'mail_template' => '{$tableName}_{$typeOfNotify}_notify',
842
    'mail_subject' => {$language}{$stuTableName}_{$stuTypeOfNotify}_NOTIFY_SUBJECT);\n
843
EOT;
844
                break;
845
        }
846
847
        return $ret;
0 ignored issues
show
Bug introduced by
The variable $ret does not seem to be defined for all execution paths leading up to this point.

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

Let’s take a look at an example:

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

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

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

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

Available Fixes

  1. Check for existence of the variable explicitly:

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

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

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
848
    }
849
850
    /*
851
    *  @private function getXoopsVersionNotifications
852
    *  @param $moduleDirname
853
    *  @param $language
854
    */
855
    /**
856
     * @param $moduleDirname
857
     * @param $language
858
     * @param $filename
859
     *
860
     * @return string
861
     */
862
    private function getXoopsVersionNotifications($module, $language, $filename)
0 ignored issues
show
Unused Code introduced by
The parameter $filename 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...
863
    {
864
        $moduleDirname = $module->getVar('mod_dirname');
865
        $ret = <<<EOT
866
// ------------------- Notifications ------------------- //
867
\$modversion['hasNotification'] = 1;
868
\$modversion['notification']['lookup_file'] = 'include/notification.inc.php';
869
\$modversion['notification']['lookup_func'] = '{$moduleDirname}_notify_iteminfo';\n\n
870
EOT;
871
        $notifyFiles = array();
872
        $single = 'single';
873
        $tables = $this->getTableTables($module->getVar('mod_id'), 'table_order');
874
        foreach (array_keys($tables) as $t) {
875
            $tableId = $tables[$t]->getVar('table_id');
876
            $tableMid = $tables[$t]->getVar('table_mid');
877
            $tableName = $tables[$t]->getVar('table_name');
878
            $tableCategory = $tables[$t]->getVar('table_category');
879
            $tableBroken = $tables[$t]->getVar('table_broken');
880
            $tableSubmit = $tables[$t]->getVar('table_submit');
881
            if (1 == $tables[$t]->getVar('table_notifications')) {
882
                if ($t <= count($tableName)) {
883
                    $notifyFiles[] = $tables[$t]->getVar('table_name');
884
                }
885
            }
886
            if (1 == $tables[$t]->getVar('table_single')) {
887
                $single = $tableName;
888
            }
889
        }
890
        $fields = $this->tdmcfile->getTableFields($tableMid, $tableId);
0 ignored issues
show
Bug introduced by
The variable $tableMid 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 $tableId does not seem to be defined for all execution paths leading up to this point.

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

Let’s take a look at an example:

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

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

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

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

Available Fixes

  1. Check for existence of the variable explicitly:

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

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

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The method getTableFields cannot be called on $this->tdmcfile (of type string).

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

Loading history...
891
        $fieldParent = null;
892
        foreach (array_keys($fields) as $f) {
893
            $fieldMid = $fields[$f]->getVar('field_mid');
0 ignored issues
show
Unused Code introduced by
$fieldMid 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...
894
            $fieldTid = $fields[$f]->getVar('field_tid');
0 ignored issues
show
Unused Code introduced by
$fieldTid 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...
895
            $fieldName = $fields[$f]->getVar('field_name');
896
            $fieldElement = $fields[$f]->getVar('field_element');
897
            if (0 == $f) {
898
                $fieldId = $fieldName;
899
            }
900
            if ($fieldElement > 15) {
901
                $fieldParent = $fieldName;
902
            }
903
        }
904
905
        $ret .= $this->getXoopsVersionNotificationGlobal($language, 'category', 'global', 'global', $notifyFiles);
906
        $ret .= $this->getXoopsVersionNotificationCategory($language, 'category', 'category', 'category', $notifyFiles, $fieldParent, '1');
907
        $ret .= $this->getXoopsVersionNotificationTableName($language, 'category', 'file', 'file', $single, $fieldId, 1);
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...
908
        if (1 == $tableCategory) {
0 ignored issues
show
Bug introduced by
The variable $tableCategory 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...
909
            $ret .= $this->getXoopsVersionNotificationCodeComplete($language, 'event', 'new_category', 'global', 0, 'global', 'newcategory', 'global_newcategory_notify');
910
        }
911
        $ret .= $this->getXoopsVersionNotificationCodeComplete($language, 'event', 'file_modify', 'global', 1, 'global', 'filemodify', 'global_filemodify_notify');
912
        if (1 == $tableBroken) {
0 ignored issues
show
Bug introduced by
The variable $tableBroken 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...
913
            $ret .= $this->getXoopsVersionNotificationCodeComplete($language, 'event', 'file_broken', 'global', 1, 'global', 'filebroken', 'global_filebroken_notify');
914
        }
915
        if (1 == $tableSubmit) {
0 ignored issues
show
Bug introduced by
The variable $tableSubmit 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...
916
            $ret .= $this->getXoopsVersionNotificationCodeComplete($language, 'event', 'file_submit', 'global', 1, 'global', 'filesubmit', 'global_filesubmit_notify');
917
        }
918
        $ret .= $this->getXoopsVersionNotificationCodeComplete($language, 'event', 'new_file', 'global', 0, 'global', 'newfile', 'global_newfile_notify');
919
        if (1 == $tableCategory) {
920
            $ret .= $this->getXoopsVersionNotificationCodeComplete($language, 'event', 'file_submit', 'category', 1, 'category', 'filesubmit', 'category_filesubmit_notify');
921
            $ret .= $this->getXoopsVersionNotificationCodeComplete($language, 'event', 'new_file', 'category', 0, 'category', 'newfile', 'category_newfile_notify');
922
        }
923
        $ret .= $this->getXoopsVersionNotificationCodeComplete($language, 'event', 'approve', 'file', 1, 'file', 'approve', 'file_approve_notify');
924
925
        return $ret;
926
    }
927
928
    /*
929
    *  @private function getXoopsVersionNotificationGlobal
930
    */
931
    /**
932
     * @param $language
933
     * @param $type
934
     * @param $name
935
     * @param $title
936
     * @param $from
937
     *
938
     * @return string
939
     */
940
    private function getXoopsVersionNotificationGlobal($language, $type, $name, $title, $from)
941
    {
942
        $title = strtoupper($title);
943
        $ret = "
944
\$modversion['notification']['{$type}'][] = array(
945
    'name' => '{$name}',
946
    'title' => {$language}{$title}_NOTIFY,
947
    'description' => {$language}{$title}_NOTIFY_DESC,
948
    'subscribe_from' => array('index.php', '".implode(".php', '", $from).".php'));\n
949
";
950
951
        return $ret;
952
    }
953
954
    /*
955
    *  @private function getXoopsVersionNotificationCategory
956
    */
957
    /**
958
     * @param $language
959
     * @param $type
960
     * @param $name
961
     * @param $title
962
     * @param $from
963
     *
964
     * @return string
965
     */
966 View Code Duplication
    private function getXoopsVersionNotificationCategory($language, $type, $name, $title, $from, $item, $allow)
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...
967
    {
968
        $title = strtoupper($title);//{$from}
969
        $ret = "
970
\$modversion['notification']['{$type}'][] = array(
971
    'name' => '{$name}',
972
    'title' => {$language}{$title}_NOTIFY,
973
    'description' => {$language}{$title}_NOTIFY_DESC,
974
    'subscribe_from' => array('".implode(".php', '", $from).".php'),
975
	'item_name' => '{$item}',
976
    'allow_bookmark' => {$allow});\n
977
";
978
979
        return $ret;
980
    }
981
982
    /*
983
    *  @private function getXoopsVersionNotificationTableName
984
    */
985
    /**
986
     * @param $language
987
     * @param $type
988
     * @param $name
989
     * @param $title
990
     * @param $from
991
     * @param $item
992
     * @param $allow
993
     *
994
     * @return string
995
     */
996 View Code Duplication
    private function getXoopsVersionNotificationTableName($language, $type, $name, $title, $from, $item = 'cid', $allow = 1)
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...
997
    {
998
        $title = strtoupper($title);
999
        $ret = <<<EOT
1000
\$modversion['notification']['{$type}'][] = array(
1001
    'name' => '{$name}',
1002
    'title' => {$language}{$title}_NOTIFY,
1003
    'description' => {$language}{$title}_NOTIFY_DESC,
1004
    'subscribe_from' => '{$from}.php',
1005
    'item_name' => '{$item}',
1006
    'allow_bookmark' => {$allow});\n\n
1007
EOT;
1008
1009
        return $ret;
1010
    }
1011
1012
    /*
1013
    *  @private function getXoopsVersionNotifications
1014
    */
1015
    /**
1016
     * @param $language
1017
     * @param $type
1018
     * @param $name
1019
     * @param $title
1020
     * @param $from
1021
     * @param $item
1022
     * @param $mail
1023
     *
1024
     * @return string
1025
     */
1026
    private function getXoopsVersionNotificationCodeComplete($language, $type, $name, $category, $admin = 1, $title, $table, $mail)
1027
    {
1028
        $title = strtoupper($title);
1029
        $table = strtoupper($table);
1030
        $ret = <<<EOT
1031
\$modversion['notification']['{$type}'][] = array(
1032
    'name' => '{$name}',
1033
    'category' => '{$category}',
1034
    'admin_only' => {$admin},
1035
    'title' => {$language}{$title}_{$table}_NOTIFY,
1036
    'caption' => {$language}{$title}_{$table}_NOTIFY_CAPTION,
1037
    'description' => {$language}{$title}_{$table}_NOTIFY_DESC,
1038
    'mail_template' => '{$mail}',
1039
    'mail_subject' => {$language}{$title}_{$table}_NOTIFY_SUBJECT);\n\n
1040
EOT;
1041
1042
        return $ret;
1043
    }
1044
1045
    /*
1046
    *  @public function render
1047
    *  @param null
1048
    */
1049
    /**
1050
     * @return bool|string
1051
     */
1052
    public function render()
1053
    {
1054
        $module = $this->getModule();
1055
        $table = $this->getTable();
1056
        $tables = $this->getTables();
1057
        $filename = $this->getFileName();
1058
        $moduleDirname = $module->getVar('mod_dirname');
1059
        $language = $this->getLanguage($moduleDirname, 'MI');
1060
        $content = $this->getHeaderFilesComments($module, $filename);
1061
        $content .= $this->getXoopsVersionHeader($module, $table, $language);
1062
        if (1 == $module->getVar('mod_admin')) {
1063
            $content .= $this->getXoopsVersionTemplatesAdmin($moduleDirname, $table, $tables);
1064
        }
1065
        if (1 == $module->getVar('mod_user')) {
1066
            $content .= $this->getXoopsVersionTemplatesUser($moduleDirname, $tables);
1067
        }
1068
        $content .= $this->getXoopsVersionMySQL($moduleDirname, $table, $tables);
1069
        if (1 == $table->getVar('table_search')) {
1070
            $content .= $this->getXoopsVersionSearch($moduleDirname);
1071
        }
1072
        if (1 == $table->getVar('table_comments')) {
1073
            $content .= $this->getXoopsVersionComments($moduleDirname);
1074
        }
1075
1076
        //if (1 == $table->getVar('table_submenu')) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
1077
            $content .= $this->getXoopsVersionSubmenu($language, $tables);
1078
        //}
1079
        if (1 == $table->getVar('table_blocks')) {
1080
            $content .= $this->getXoopsVersionBlocks($moduleDirname, $table, $tables, $language);
1081
        }
1082
        $content .= $this->getXoopsVersionConfig($module, $table, $language);
1083
        if (1 == $table->getVar('table_notifications')) {
1084
            $content .= $this->getXoopsVersionNotifications($module, $language, $filename);
1085
        }
1086
        $this->tdmcfile->create($moduleDirname, '/', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
0 ignored issues
show
Bug introduced by
The method create cannot be called on $this->tdmcfile (of type string).

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

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

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

Loading history...
1089
    }
1090
}
1091