Completed
Push — master ( 01b1a5...81f493 )
by Michael
04:03
created

update.php ➔ update_tables_to_300()   C

Complexity

Conditions 7
Paths 48

Size

Total Lines 177
Code Lines 147

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 7
eloc 147
c 2
b 1
f 0
nc 48
nop 0
dl 0
loc 177
rs 6.4589

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 *
5
 * Module: SmartSection
6
 * Author: The SmartFactory <www.smartfactory.ca>
7
 * Licence: GNU
8
 */
9
10
include_once __DIR__ . '/admin_header.php';
11
include_once(SMARTCONTENT_ROOT_PATH . 'class/dbupdater.php');
12
13
smartcontent_xoops_cp_header();
14
15
// =========================================================================================
16
// This function updates any existing table of a 2.x version to the format used
17
// in the release of WF-Downloads 3.00
18
// =========================================================================================
19
function update_tables_to_300()
20
{
21
    $dbupdater = new WfdownloadsDbupdater();
22
23
    if (!wfdownloads_TableExists('wfdownloads_meta')) {
24
        // Create table wfdownloads_meta
25
        $table = new WfdownloadsTable('wfdownloads_meta');
26
        $table->setStructure("CREATE TABLE %s (
27
                                metakey varchar(50) NOT NULL default '',
28
                                metavalue varchar(255) NOT NULL default '',
29
                                PRIMARY KEY (metakey))
30
                                ENGINE=MyISAM;");
31
32
        $table->setData(sprintf("'version', %s", round($GLOBALS['xoopsModule']->getVar('version') / 100, 2)));
33
        if ($dbupdater->updateTable($table)) {
34
            echo 'wfdownloads_meta table created<br>';
35
        }
36
    }
37
38
    $download_fields = array(
39
        'lid'           => array('Type' => 'int(11) unsigned NOT NULL auto_increment', 'Default' => false),
40
        'cid'           => array('Type' => "int(5) unsigned NOT NULL default '0'", 'Default' => true),
41
        'title'         => array('Type' => "varchar(100) NOT NULL default ''", 'Default' => true),
42
        'url'           => array('Type' => "varchar(255) NOT NULL default ''", 'Default' => true),
43
        'filename'      => array('Type' => "varchar(150) NOT NULL default ''", 'Default' => true),
44
        'filetype'      => array('Type' => "varchar(100) NOT NULL default ''", 'Default' => true),
45
        'homepage'      => array('Type' => "varchar(100) NOT NULL default ''", 'Default' => true),
46
        'version'       => array('Type' => "varchar(20) NOT NULL default ''", 'Default' => true),
47
        'size'          => array('Type' => "int(8) NOT NULL default '0'", 'Default' => true),
48
        'platform'      => array('Type' => "varchar(50) NOT NULL default ''", 'Default' => true),
49
        'screenshot'    => array('Type' => "varchar(255) NOT NULL default ''", 'Default' => true),
50
        'submitter'     => array('Type' => "int(11) NOT NULL default '0'", 'Default' => true),
51
        'publisher'     => array('Type' => "varchar(255) NOT NULL default ''", 'Default' => true),
52
        'status'        => array('Type' => "tinyint(2) NOT NULL default '0'", 'Default' => true),
53
        'date'          => array('Type' => "int(10) NOT NULL default '0'", 'Default' => true),
54
        'hits'          => array('Type' => "int(11) unsigned NOT NULL default '0'", 'Default' => true),
55
        'rating'        => array('Type' => "double(6,4) NOT NULL default '0.0000'", 'Default' => true),
56
        'votes'         => array('Type' => "int(11) unsigned NOT NULL default '0'", 'Default' => true),
57
        'comments'      => array('Type' => "int(11) unsigned NOT NULL default '0'", 'Default' => true),
58
        'license'       => array('Type' => "varchar(255) NOT NULL default ''", 'Default' => true),
59
        'mirror'        => array('Type' => "varchar(255) NOT NULL default ''", 'Default' => true),
60
        'price'         => array('Type' => "varchar(10) NOT NULL default 'Free'", 'Default' => true),
61
        'paypalemail'   => array('Type' => "varchar(255) NOT NULL default ''", 'Default' => true),
62
        'features'      => array('Type' => 'text NOT NULL', 'Default' => false),
63
        'requirements'  => array('Type' => 'text NOT NULL', 'Default' => false),
64
        'homepagetitle' => array('Type' => "varchar(255) NOT NULL default ''", 'Default' => true),
65
        'forumid'       => array('Type' => "int(11) NOT NULL default '0'", 'Default' => true),
66
        'limitations'   => array('Type' => "varchar(255) NOT NULL default '30 day trial'", 'Default' => true),
67
        'dhistory'      => array('Type' => 'text NOT NULL', 'Default' => false),
68
        'published'     => array('Type' => "int(11) NOT NULL default '1089662528'", 'Default' => true),
69
        'expired'       => array('Type' => "int(10) NOT NULL default '0'", 'Default' => true),
70
        'updated'       => array('Type' => "int(11) NOT NULL default '0'", 'Default' => true),
71
        'offline'       => array('Type' => "tinyint(1) NOT NULL default '0'", 'Default' => true),
72
        'description'   => array('Type' => 'text NOT NULL', 'Default' => false),
73
        'ipaddress'     => array('Type' => "varchar(120) NOT NULL default '0'", 'Default' => true),
74
        'notifypub'     => array('Type' => "int(1) NOT NULL default '0'", 'Default' => true),
75
        'summary'       => array('Type' => 'text NOT NULL', 'Default' => false)
76
    );
77
78
    $renamed_fields = array(
79
        'logourl' => 'screenshot'
80
    );
81
82
    echo '<br><b>Checking Download table</b><br>';
83
    $downloadHandler = xoops_getModuleHandler('download', 'wfdownloads');
84
    $download_table  = new WfdownloadsTable('wfdownloads_downloads');
85
    $fields          = get_table_info($downloadHandler->table, $download_fields);
86
    // Check for renamed fields
87
    rename_fields($download_table, $renamed_fields, $fields, $download_fields);
88
    update_table($download_fields, $fields, $download_table);
89
    if ($dbupdater->updateTable($download_table)) {
90
        echo 'Downloads table updated<br>';
91
    }
92
    unset($fields);
93
94
    $mod_fields = array(
95
        'requestid'       => array('Type' => 'int(11) NOT NULL auto_increment', 'Default' => false),
96
        'lid'             => array('Type' => "int(11) unsigned NOT NULL default '0'", 'Default' => true),
97
        'cid'             => array('Type' => "int(5) unsigned NOT NULL default '0'", 'Default' => true),
98
        'title'           => array('Type' => "varchar(255) NOT NULL default ''", 'Default' => true),
99
        'url'             => array('Type' => "varchar(255) NOT NULL default ''", 'Default' => true),
100
        'filename'        => array('Type' => "varchar(150) NOT NULL default ''", 'Default' => true),
101
        'filetype'        => array('Type' => "varchar(100) NOT NULL default ''", 'Default' => true),
102
        'homepage'        => array('Type' => "varchar(255) NOT NULL default ''", 'Default' => true),
103
        'version'         => array('Type' => "varchar(20) NOT NULL default ''", 'Default' => true),
104
        'size'            => array('Type' => "int(8) NOT NULL default '0'", 'Default' => true),
105
        'platform'        => array('Type' => "varchar(50) NOT NULL default ''", 'Default' => true),
106
        'screenshot'      => array('Type' => "varchar(255) NOT NULL default ''", 'Default' => true),
107
        'submitter'       => array('Type' => "int(11) NOT NULL default '0'", 'Default' => true),
108
        'publisher'       => array('Type' => 'text NOT NULL', 'Default' => false),
109
        'status'          => array('Type' => "tinyint(2) NOT NULL default '0'", 'Default' => true),
110
        'date'            => array('Type' => "int(10) NOT NULL default '0'", 'Default' => true),
111
        'hits'            => array('Type' => "int(11) unsigned NOT NULL default '0'", 'Default' => true),
112
        'rating'          => array('Type' => "double(6,4) NOT NULL default '0.0000'", 'Default' => true),
113
        'votes'           => array('Type' => "int(11) unsigned NOT NULL default '0'", 'Default' => true),
114
        'comments'        => array('Type' => "int(11) unsigned NOT NULL default '0'", 'Default' => true),
115
        'license'         => array('Type' => "varchar(255) NOT NULL default ''", 'Default' => true),
116
        'mirror'          => array('Type' => "varchar(255) NOT NULL default ''", 'Default' => true),
117
        'price'           => array('Type' => "varchar(10) NOT NULL default 'Free'", 'Default' => true),
118
        'paypalemail'     => array('Type' => "varchar(255) NOT NULL default ''", 'Default' => true),
119
        'features'        => array('Type' => 'text NOT NULL', 'Default' => false),
120
        'requirements'    => array('Type' => 'text NOT NULL', 'Default' => false),
121
        'homepagetitle'   => array('Type' => "varchar(255) NOT NULL default ''", 'Default' => true),
122
        'forumid'         => array('Type' => "int(11) NOT NULL default '0'", 'Default' => true),
123
        'limitations'     => array('Type' => "varchar(255) NOT NULL default '30 day trial'", 'Default' => true),
124
        'dhistory'        => array('Type' => 'text NOT NULL', 'Default' => false),
125
        'published'       => array('Type' => "int(10) NOT NULL default '0'", 'Default' => true),
126
        'expired'         => array('Type' => "int(10) NOT NULL default '0'", 'Default' => true),
127
        'updated'         => array('Type' => "int(11) NOT NULL default '0'", 'Default' => true),
128
        'offline'         => array('Type' => "tinyint(1) NOT NULL default '0'", 'Default' => true),
129
        'summary'         => array('Type' => 'text NOT NULL', 'Default' => false),
130
        'description'     => array('Type' => 'text NOT NULL', 'Default' => false),
131
        'modifysubmitter' => array('Type' => "int(11) NOT NULL default '0'", 'Default' => true),
132
        'requestdate'     => array('Type' => "int(11) NOT NULL default '0'", 'Default' => true)
133
    );
134
135
    $renamed_fields = array(
136
        'logourl' => 'screenshot'
137
    );
138
139
    echo '<br><b>Checking Modified Downloads table</b><br>';
140
    $modHandler = xoops_getModuleHandler('modification', 'wfdownloads');
141
    $mod_table  = new WfdownloadsTable('wfdownloads_mod');
142
    $fields     = get_table_info($modHandler->table, $mod_fields);
143
    rename_fields($mod_table, $renamed_fields, $fields, $mod_fields);
144
    update_table($mod_fields, $fields, $mod_table);
145
    if ($dbupdater->updateTable($mod_table)) {
146
        echo 'Modified Downloads table updated <br>';
147
    }
148
    unset($fields);
149
150
    $cat_fields = array(
151
        'cid'          => array('Type' => 'int(5) unsigned NOT NULL auto_increment', 'Default' => false),
152
        'pid'          => array('Type' => "int(5) unsigned NOT NULL default '0'", 'Default' => true),
153
        'title'        => array('Type' => "varchar(50) NOT NULL default ''", 'Default' => true),
154
        'imgurl'       => array('Type' => "varchar(255) NOT NULL default ''", 'Default' => true),
155
        'description'  => array('Type' => "text NOT NULL default ''", 'Default' => true),
156
        'total'        => array('Type' => "int(11) NOT NULL default '0'", 'Default' => true),
157
        'summary'      => array('Type' => 'text NOT NULL', 'Default' => false),
158
        'spotlighttop' => array('Type' => "int(11) NOT NULL default '0'", 'Default' => true),
159
        'spotlighthis' => array('Type' => "int(11) NOT NULL default '0'", 'Default' => true),
160
        'dohtml'       => array('Type' => "tinyint(1) NOT NULL default '1'", 'Default' => true),
161
        'dosmiley'     => array('Type' => "tinyint(1) NOT NULL default '1'", 'Default' => true),
162
        'doxcode'      => array('Type' => "tinyint(1) NOT NULL default '1'", 'Default' => true),
163
        'doimage'      => array('Type' => "tinyint(1) NOT NULL default '1'", 'Default' => true),
164
        'dobr'         => array('Type' => "tinyint(1) NOT NULL default '1'", 'Default' => true),
165
        'weight'       => array('Type' => "int(11) NOT NULL default '0'", 'Default' => true)
166
    );
167
    echo '<br><b>Checking Category table</b><br>';
168
    $catHandler = xoops_getModuleHandler('category', 'wfdownloads');
169
    $cat_table  = new WfdownloadsTable('wfdownloads_cat');
170
    $fields     = get_table_info($catHandler->table, $cat_fields);
171
    update_table($cat_fields, $fields, $cat_table);
172
    if ($dbupdater->updateTable($cat_table)) {
173
        echo 'Category table updated<br>';
174
    }
175
    unset($fields);
176
177
    $broken_fields = array(
178
        'reportid'     => array('Type' => 'int(5) NOT NULL auto_increment', 'Default' => false),
179
        'lid'          => array('Type' => "int(11) NOT NULL default '0'", 'Default' => true),
180
        'sender'       => array('Type' => "int(11) NOT NULL default '0'", 'Default' => true),
181
        'ip'           => array('Type' => "varchar(20) NOT NULL default ''", 'Default' => true),
182
        'date'         => array('Type' => "varchar(11) NOT NULL default '0'", 'Default' => true),
183
        'confirmed'    => array('Type' => "enum('0','1') NOT NULL default '0'", 'Default' => true),
184
        'acknowledged' => array('Type' => "enum('0','1') NOT NULL default '0'", 'Default' => true)
185
    );
186
    echo '<br><b>Checking Broken Report table</b><br>';
187
    $brokenHandler = xoops_getModuleHandler('report', 'wfdownloads');
188
    $broken_table  = new WfdownloadsTable('wfdownloads_broken');
189
    $fields        = get_table_info($brokenHandler->table, $broken_fields);
190
    update_table($broken_fields, $fields, $broken_table);
191
    if ($dbupdater->updateTable($broken_table)) {
192
        echo 'Broken Reports table updated<br>';
193
    }
194
    unset($fields);
195
}
196
197
// =========================================================================================
198
// we are going to change the names for the fields like nohtml, nosmilies, noxcode, noimage, nobreak in
199
// the wfdownloads_cat table into dohtml, dosmilies and so on.  Therefore the logic will change
200
// 0=yes  1=no and the currently stored value need to be changed accordingly
201
// =========================================================================================
202
/**
203
 * @return array|bool
204
 */
205
function invert_nohtm_dohtml_values()
206
{
207
    $ret = array();
208
    global $xoopsDB;
209
    $catHandler = xoops_getModuleHandler('category', 'wfdownloads');
210
    $result     = $xoopsDB->query('SHOW COLUMNS FROM ' . $catHandler->table);
211
    while ($existing_field = $xoopsDB->fetchArray($result)) {
212
        $fields[$existing_field['field']] = $existing_field['type'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$fields was never initialized. Although not strictly required by PHP, it is generally a good practice to add $fields = 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...
213
    }
214
    if (in_array('nohtml', array_keys($fields))) {
0 ignored issues
show
Bug introduced by
The variable $fields 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...
215
        $dbupdater = new WfdownloadsDbupdater();
216
        //Invert column values
217
        // alter options in wfdownloads_cat
218
        $table = new WfdownloadsTable('wfdownloads_cat');
219
        $table->addAlteredField('nohtml', "dohtml tinyint(1) NOT NULL DEFAULT '1'");
220
        $table->addAlteredField('nosmiley', "dosmiley tinyint(1) NOT NULL DEFAULT '1'");
221
        $table->addAlteredField('noxcodes', "doxcode tinyint(1) NOT NULL DEFAULT '1'");
222
        $table->addAlteredField('noimages', "doimage tinyint(1) NOT NULL DEFAULT '1'");
223
        $table->addAlteredField('nobreak', "dobr tinyint(1) NOT NULL DEFAULT '1'");
224
225
        //inverting values no=1 <=> do=0
226
        // have to store teporarly as value = 2 to
227
        // avoid putting everithing to same value
228
        // if you change 1 to 0, then 0 to one,
229
        // every value will be 1, follow me?
230
        $table->addUpdatedWhere('dohtml', 2, '=1');
231
        $table->addUpdatedWhere('dohtml', 1, '=0');
232
        $table->addUpdatedWhere('dohtml', 0, '=2');
233
234
        $table->addUpdatedWhere('dosmiley', 2, '=1');
235
        $table->addUpdatedWhere('dosmiley', 1, '=0');
236
        $table->addUpdatedWhere('dosmiley', 0, '=2');
237
238
        $table->addUpdatedWhere('doxcode', 2, '=1');
239
        $table->addUpdatedWhere('doxcode', 1, '=0');
240
        $table->addUpdatedWhere('doxcode', 0, '=2');
241
242
        $table->addUpdatedWhere('doimage', 2, '=1');
243
        $table->addUpdatedWhere('doimage', 1, '=0');
244
        $table->addUpdatedWhere('doimage', 0, '=2');
245
        $ret = $dbupdater->updateTable($table);
246
    }
247
248
    return $ret;
249
}
250
251
/**
252
 * Updates a table by comparing correct fields with existing ones
253
 *
254
 * @param  array            $new_fields
255
 * @param  array            $existing_fields
256
 * @param  WfDownloadsTable $table
257
 * @return void
258
 */
259
function update_table($new_fields, $existing_fields, &$table)
260
{
261
    foreach ($new_fields as $field => $fieldinfo) {
262
        $type = $fieldinfo['Type'];
263
        if (!in_array($field, array_keys($existing_fields))) {
264
            //Add field as it is missing
265
            $table->addNewField($field, $type);
266
            //$xoopsDB->query("ALTER TABLE ".$table." ADD ".$field." ".$type);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
267
            //echo $field."(".$type.") <FONT COLOR='##22DD51'>Added</FONT><br>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
268
        } elseif ($existing_fields[$field] != $type) {
269
            $table->addAlteredField($field, $field . ' ' . $type);
270
            // check $fields[$field]['type'] for things like "int(10) unsigned"
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...
271
            //$xoopsDB->query("ALTER TABLE ".$table." CHANGE ".$field." ".$field." ".$type);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
272
            //echo $field." <FONT COLOR='#FF6600'>Changed to</FONT> ".$type."<br>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
273
        } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
274
            //echo $field." <FONT COLOR='#0033FF'>Uptodate</FONT><br>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
275
        }
276
    }
277
}
278
279
/**
280
 * Get column information for a table - we'll need to send along an array of fields to determine
281
 * whether the "Default" index value should be appended
282
 *
283
 * @param  string $table
284
 * @param  array  $default_fields
285
 * @return array
286
 */
287
function get_table_info($table, $default_fields)
288
{
289
    global $xoopsDB;
290
    $result = $xoopsDB->query('SHOW COLUMNS FROM ' . $table);
291
    while ($existing_field = $xoopsDB->fetchArray($result)) {
292
        $fields[$existing_field['Field']] = $existing_field['Type'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$fields was never initialized. Although not strictly required by PHP, it is generally a good practice to add $fields = 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...
293
        if ($existing_field['Null'] !== 'YES') {
294
            $fields[$existing_field['Field']] .= ' NOT NULL';
295
        }
296
        if ($existing_field['Extra']) {
297
            $fields[$existing_field['Field']] .= ' ' . $existing_field['Extra'];
298
        }
299
        if ($default_fields[$existing_field['Field']]['Default']) {
300
            $fields[$existing_field['Field']] .= " default '" . $existing_field['Default'] . "'";
301
        }
302
    }
303
304
    return $fields;
0 ignored issues
show
Bug introduced by
The variable $fields 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...
305
}
306
307
/**
308
 * Renames fields in a table and updates the existing fields array to reflect it.
309
 *
310
 * @param  WfDownloadsTable $table
311
 * @param  array            $renamed_fields
312
 * @param  array            $fields
313
 * @param  array            $new_fields
314
 * @return array
315
 */
316
function rename_fields(&$table, $renamed_fields, &$fields, $new_fields)
317
{
318
    foreach (array_keys($fields) as $field) {
319
        if (in_array($field, array_keys($renamed_fields))) {
320
            $new_field_name = $renamed_fields[$field];
321
            $new_field_type = $new_fields[$new_field_name]['Type'];
322
            $table->addAltered($field, $new_field_name . ' ' . $new_field_type);
323
            //$xoopsDB->query("ALTER TABLE ".$table." CHANGE ".$field." ".$new_field_name." ".$new_field_type);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
324
            //echo $field." Renamed to ".$new_field_name."<br>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
325
            $fields[$new_field_name] = $new_field_type;
326
        }
327
    }
328
    //return $fields;
329
}
330
331
$op = isset($_REQUEST['op']) ? (int)$_REQUEST['op'] : 0;
332
switch ($op) {
333
    case 1:
334
        // Make sure that nohtml is properly changed to dohtml
335
        invert_nohtm_dohtml_values();
336
        // Ensure that the proper tables are present
337
        update_tables_to_300();
338
        // Import data from MyDownloads
339
        import_mydownloads_to_wfdownloads();
340
        break;
341
342
    case 2:
343
        // Update WF-Downloads
344
        $log = invert_nohtm_dohtml_values();
345
        update_tables_to_300();
346
        break;
347
348
    default:
349
        //ask what to do
350
        include XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
351
        $form = new XoopsThemeForm('Upgrade WF-Downloads', 'form', $_SERVER['REQUEST_URI']);
352
353
        //Is MyDownloads installed?
354
        $moduleHandler     = xoops_getHandler('module');
355
        $mydownloadsModule = $moduleHandler->getByDirname('mydownloads');
356
        if (is_object($mydownloadsModule)) {
357
            $mydownloadsButton = new XoopsFormButton('Import data from MyDownloads', 'myd_button', 'Import', 'submit');
358
            $mydownloadsButton->setExtra("onclick='document.forms.form.op.value=\"1\"'");
359
            $form->addElement($mydownloadsButton);
360
        }
361
362
        if (!wfdownloads_TableExists('wfdownloads_meta')) {
363
            $updateButton = new XoopsFormButton('Update WF-Downloads', 'upd_button', 'Update', 'submit');
364
            $updateButton->setExtra("onclick='document.forms.form.op.value=\"2\"'");
365
            $form->addElement($updateButton);
366
        }
367
368
        $form->addElement(new XoopsFormHidden('op', 0));
369
        $form->display();
370
        break;
371
}
372
//wfdownloads_modFooter();
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
373
//xoops_cp_footer();
374
include_once __DIR__ . '/admin_footer.php';
375