Completed
Push — master ( 6c5294...348995 )
by Michael
02:27
created

onupdate.inc.php ➔ xoops_module_update_smartfaq()   D

Complexity

Conditions 8
Paths 128

Size

Total Lines 89
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 41
nc 128
nop 1
dl 0
loc 89
rs 4.9555
c 0
b 0
f 0

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
// 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
/**
6
 * @param $module
7
 * @return bool
8
 */
9
function xoops_module_update_smartfaq($module)
10
{
11
    // Load SmartDbUpdater from the SmartObject Framework if present
12
    $smartdbupdater = XOOPS_ROOT_PATH . '/modules/smartobject/class/smartdbupdater.php';
13
    if (!file_exists($smartdbupdater)) {
14
        $smartdbupdater = XOOPS_ROOT_PATH . '/modules/smartfaq/class/smartdbupdater.php';
15
    }
16
    include_once($smartdbupdater);
17
18
    $dbupdater = new SmartobjectDbupdater();
19
20
    ob_start();
21
22
    echo '<code>' . _SDU_UPDATE_UPDATING_DATABASE . '<br />';
23
24
    // Adding partialview field
25
    $table = new SmartDbTable('smartfaq_faq');
26
    if (!$table->fieldExists('partialview')) {
27
        $table->addNewField('partialview', "tinyint(1) NOT NULL default '0'");
28
    }
29
30
    // Changing categoryid type to int(11)
31
    $table->addAlteredField('categoryid', "int(11) NOT NULL default '0'", false);
32
33
    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...
34
        /**
35
         * @todo trap the errors
36
         */
37
    }
38
    unset($table);
39
40
    // Editing smartfaq_categories table
41
    $table = new SmartDbTable('smartfaq_categories');
42
    // Changing categoryid type to int(11)
43
    $table->addAlteredField('categoryid', "int(11) NOT NULL default '0'", false);
44
45
    // Changing parentid type to int(11)
46
    $table->addAlteredField('parentid', "int(11) NOT NULL default '0'", false);
47
48
    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...
49
        /**
50
         * @todo trap the errors
51
         */
52
    }
53
    unset($table);
54
55
    // Editing smartfaq_answers table
56
    $table = new SmartDbTable('smartfaq_answers');
57
    // Changing categoryid type to int(11)
58
    $table->addAlteredField('answerid', "int(11) NOT NULL default '0'", false);
59
60
    // Changing parentid type to int(11)
61
    $table->addAlteredField('faqid', "int(11) NOT NULL default '0'", false);
62
63
    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...
64
        /**
65
         * @todo trap the errors
66
         */
67
    }
68
    unset($table);
69
70
    /**
71
     * Check for items with categoryid=0
72
     */
73
    include_once(XOOPS_ROOT_PATH . '/modules/smartfaq/include/functions.php');
74
    $smartfaq_faqHandler      = $answerHandler = sf_gethandler('faq');
0 ignored issues
show
Unused Code introduced by
$answerHandler 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...
75
    $smartfaq_categoryHandler = $answerHandler = sf_gethandler('category');
0 ignored issues
show
Unused Code introduced by
$answerHandler 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...
76
77
    //find a valid categoryid
78
    $categoriesObj = $smartfaq_categoryHandler->getCategories(1, 0, 0, 'weight', 'ASC', false);
79
    if (count($categoriesObj) > 0) {
80
        $categoryid = $categoriesObj[0]->getVar('categoryid');
81
        $criteria   = new CriteriaCompo();
82
        $criteria->add(new Criteria('categoryid', 0));
83
        $smartfaq_faqHandler->updateAll('categoryid', $categoryid, $criteria);
84
        echo '&nbsp;&nbsp;Cleaning up questions with categoryid=0<br />';
85
    }
86
87
    echo '</code>';
88
89
    $feedback = ob_get_clean();
90
    if (method_exists($module, 'setMessage')) {
91
        $module->setMessage($feedback);
92
    } else {
93
        echo $feedback;
94
    }
95
96
    return true;
97
}
98
99
/**
100
 * @param $module
101
 * @return bool
102
 */
103
function xoops_module_install_smartfaq($module)
104
{
105
    ob_start();
106
107
    include_once(XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/include/functions.php');
108
109
    $feedback = ob_get_clean();
110
    if (method_exists($module, 'setMessage')) {
111
        $module->setMessage($feedback);
112
    } else {
113
        echo $feedback;
114
    }
115
116
    return true;
117
}
118