onupdate.inc.php ➔ xoops_module_update_smartpartner()   F
last analyzed

Complexity

Conditions 18
Paths 978

Size

Total Lines 175
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 67
nc 978
nop 1
dl 0
loc 175
rs 2.3577
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
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 23 and the first side effect is on line 5.

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
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
4
5
global $modversion;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
6
if (!empty($_POST['fct']) && !empty($_POST['op']) && $_POST['fct'] === 'modulesadmin' && $_POST['op'] === 'update_ok' && $_POST['dirname'] == $modversion['dirname']) {
7
    // referer check
8
    $ref = xoops_getenv('HTTP_REFERER');
9
    if ($ref == '' || strpos($ref, XOOPS_URL . '/modules/system/admin.php') === 0) {
10
        /* module specific part */
11
12
        /* General part */
13
14
        // Keep the values of block's options when module is updated (by nobunobu)
15
        include __DIR__ . '/updateblock.inc.php';
16
    }
17
}
18
19
/**
20
 * @param $module
21
 * @return bool
22
 */
23
function xoops_module_update_smartpartner($module)
24
{
25
    include_once(XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/include/functions.php');
26
    include_once(XOOPS_ROOT_PATH . '/modules/smartobject/class/smartdbupdater.php');
27
28
    $dbupdater = new SmartobjectDbupdater();
0 ignored issues
show
Unused Code introduced by
$dbupdater 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...
29
30
    ob_start();
31
32
    $dbVersion = smartpartner_GetMeta('version');
33
34
    $dbupdater = new SmartobjectDbupdater();
35
36
    echo '<code>' . _SDU_UPDATE_UPDATING_DATABASE . '<br>';
37
38
    //smartpartner_create_upload_folders();
39
40
    // db migrate version = 3
41
    $newDbVersion = 3;
42
    if ($dbVersion < $newDbVersion) {
43
        echo 'Database migrate to version ' . $newDbVersion . '<br>';
44
45
        $table = new SmartDbTable('smartpartner_partner');
46
        $table->addNewField('email_priv', " tinyint(1) NOT NULL default '0'");
47
        $table->addNewField('phone_priv', " tinyint(1) NOT NULL default '0'");
48
        $table->addNewField('adress_priv', " tinyint(1) NOT NULL default '0'");
49
50
        if (!$dbupdater->updateTable($table)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies 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 if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

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

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
51
            /**
52
             * @todo trap the errors
53
             */
54
        }
55
        unset($table);
56
    }
57
    // db migrate version =4
58
    $newDbVersion = 4;
59
    if ($dbVersion < $newDbVersion) {
60
        echo 'Database migrate to version ' . $newDbVersion . '<br>';
61
        //create new tables
62
        // Create table smartpartner_categories
63
        $table = new SmartDbTable('smartpartner_categories');
64
        if (!$table->exists()) {
65
            $table->setStructure("
66
              `categoryid` int(11) NOT NULL auto_increment,
67
              `parentid` int(11) NOT NULL default '0',
68
              `name` varchar(100) NOT NULL default '',
69
              `description` text NOT NULL,
70
              `image` varchar(255) NOT NULL default '',
71
              `total` int(11) NOT NULL default '0',
72
              `weight` int(11) NOT NULL default '1',
73
              `created` int(11) NOT NULL default '0',
74
              PRIMARY KEY  (`categoryid`)
75
            ");
76
77
            if (!$dbupdater->updateTable($table)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies 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 if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

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

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
78
                /**
79
                 * @todo trap the errors
80
                 */
81
            }
82
        }
83
        // Create table smartpartner_partner_cat_link
84
        $table = new SmartDbTable('smartpartner_partner_cat_link');
85
        if (!$table->exists()) {
86
            $table->setStructure("
87
              `partner_cat_linkid` int(11) NOT NULL auto_increment,
88
              `categoryid` int(11) NOT NULL default '0',
89
              `partnerid` int(11) NOT NULL default '0',
90
               PRIMARY KEY  (`partner_cat_linkid`)
91
            ");
92
93
            if (!$dbupdater->updateTable($table)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies 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 if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

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

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
94
                /**
95
                 * @todo trap the errors
96
                 */
97
            }
98
        }
99
100
        // Create table smartpartner_offer
101
        $table = new SmartDbTable('smartpartner_offer');
102
        if (!$table->exists()) {
103
            $table->setStructure("
104
               `offerid` int(11) NOT NULL auto_increment,
105
              `partnerid` int(11) NOT NULL default '0',
106
              `title` varchar(255) NOT NULL default '',
107
              `description` TEXT NOT NULL,
108
              `url` varchar(150) default '',
109
              `image` varchar(150) NOT NULL default '',
110
              `date_sub` int(11) NOT NULL default '0',
111
              `date_pub` int(11) NOT NULL default '0',
112
              `date_end` int(11) NOT NULL default '0',
113
              `status` int(10) NOT NULL default '-1',
114
              `weight` int(1) NOT NULL default '0',
115
              `dohtml` int(1) NOT NULL default '1',
116
              PRIMARY KEY  (`offerid`)
117
            ");
118
119
            if (!$dbupdater->updateTable($table)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies 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 if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

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

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
120
                /**
121
                 * @todo trap the errors
122
                 */
123
            }
124
        }
125
126
        // Create table smartpartner_offer
127
        $table = new SmartDbTable('smartpartner_files');
128
        if (!$table->exists()) {
129
            $table->setStructure("
130
              `fileid` int(11) NOT NULL auto_increment,
131
              `id` int(11) NOT NULL default '0',
132
              `name` varchar(255) NOT NULL default '',
133
              `description` TEXT NOT NULL,
134
              `filename` varchar(255) NOT NULL default '',
135
              `mimetype` varchar(64) NOT NULL default '',
136
              `uid` int(6) default '0',
137
              `datesub` int(11) NOT NULL default '0',
138
              `status` int(1) NOT NULL default '-1',
139
              `notifypub` tinyint(1) NOT NULL default '1',
140
              `counter` int(8) unsigned NOT NULL default '0',
141
              PRIMARY KEY  (`fileid`)
142
            ");
143
144
            if (!$dbupdater->updateTable($table)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies 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 if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

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

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
145
                /**
146
                 * @todo trap the errors
147
                 */
148
            }
149
        }
150
        //loop in partners to insert cat_links in partner_cat_link table
151
        $smartpartnerPartnerHandler = xoops_getModuleHandler('partner', 'smartpartner');
152
        //        $smartpartnerPartnerCatLinkHandler = xoops_getModuleHandler('partner_cat_link', 'smartpartner');
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...
153
        $smartpartnerPartnerCatLinkHandler = xoops_getModuleHandler('partner_cat_link', 'smartpartner');
154
155
        $modulepermHandler = xoops_getHandler('groupperm');
156
        $moduleHandler     = xoops_getHandler('module');
157
        $module            = $moduleHandler->getByDirname('smartpartner');
158
        $groupsArray       = $modulepermHandler->getGroupIds('module_read', $module->mid(), 1);
159
160
        $sql     = 'SELECT id, categoryid from ' . $smartpartnerPartnerHandler->table;
161
        $records = $smartpartnerPartnerHandler->query($sql);
162
        foreach ($records as $record) {
163
            if ($record['categoryid'] != 0) {
164
                $new_link = $smartpartnerPartnerCatLinkHandler->create();
165
                $new_link->setVar('partnerid', $record['id']);
166
                $new_link->setVar('categoryid', $record['categoryid']);
167
                $smartpartnerPartnerCatLinkHandler->insert($new_link);
168
                unset($new_link);
169
            }
170
            foreach ($groupsArray as $group) {
171
                $modulepermHandler->addRight('full_view', $record['id'], $group, $module->mid());
172
            }
173
        }
174
        //drop cat_id in partner table
175
        $table = new SmartDbTable('smartpartner_partner');
176
        $table->addNewField('last_update', " int(11) NOT NULL default '0'");
177
        $table->addNewField('showsummary', " tinyint(1) NOT NULL default '0'");
178
        $table->addDroppedField('categoryid');
179
        if (!$dbupdater->updateTable($table)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies 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 if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

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

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
180
            /**
181
             * @todo trap the errors
182
             */
183
        }
184
        unset($table);
185
    }
186
    echo '</code>';
187
188
    $feedback = ob_get_clean();
189
    if (method_exists($module, 'setMessage')) {
190
        $module->setMessage($feedback);
191
    } else {
192
        echo $feedback;
193
    }
194
    smartpartner_SetMeta('version', isset($newDbVersion) ? $newDbVersion : 0); //Set meta version to current
195
196
    return true;
197
}
198
199
/**
200
 * @param $module
201
 * @return bool
202
 */
203
function xoops_module_install_smartpartner($module)
204
{
205
    ob_start();
206
207
    include_once(XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/include/functions.php');
208
209
    smartpartner_create_upload_folders();
210
211
    $feedback = ob_get_clean();
212
    if (method_exists($module, 'setMessage')) {
213
        $module->setMessage($feedback);
214
    } else {
215
        echo $feedback;
216
    }
217
218
    return true;
219
}
220