Passed
Branch master (4e30dc)
by Michael
02:20
created

onupdate.inc.php ➔ xoops_module_update_smartfaq()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 92
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 36
nc 64
nop 1
dl 0
loc 92
rs 6.4983
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') || die('Restricted access');
4
5
/**
6
 * @param XoopsModule $module
7
 * @return bool
8
 */
9
10
use XoopsModules\Smartfaq;
11
12
/**
13
 * @param $module
14
 * @return bool
15
 */
16
function xoops_module_update_smartfaq($module)
17
{
18
    /*
19
    // Load SmartDbUpdater from the SmartObject Framework if present
20
    $smartdbupdater = XOOPS_ROOT_PATH . '/modules/smartobject/class/smartdbupdater.php';
21
    if (!file_exists($smartdbupdater)) {
22
        $smartdbupdater = XOOPS_ROOT_PATH . '/modules/smartfaq/class/SmartobjectDbupdater.php';
23
    }
24
    require_once $smartdbupdater;
25
*/
26
    $dbupdater = new \XoopsModules\Smartfaq\SmartobjectDbupdater();
27
28
    ob_start();
29
30
    echo '<code>' . _SDU_UPDATE_UPDATING_DATABASE . '<br>';
31
32
    // Adding partialview field
33
    $table = new Smartfaq\SmartDbTable('smartfaq_faq');
34
    if (!$table->fieldExists('partialview')) {
35
        $table->addNewField('partialview', "tinyint(1) NOT NULL default '0'");
36
    }
37
38
    // Changing categoryid type to int(11)
39
    $table->addAlteredField('categoryid', "int(11) NOT NULL default '0'", false);
40
41
    if (!$dbupdater->updateTable($table)) {
42
        /**
43
         * @todo trap the errors
44
         */
45
    }
46
    unset($table);
47
48
    // Editing smartfaq_categories table
49
    $table = new Smartfaq\SmartDbTable('smartfaq_categories');
50
    // Changing categoryid type to int(11)
51
    $table->addAlteredField('categoryid', "int(11) NOT NULL default '0'", false);
52
53
    // Changing parentid type to int(11)
54
    $table->addAlteredField('parentid', "int(11) NOT NULL default '0'", false);
55
56
    if (!$dbupdater->updateTable($table)) {
57
        /**
58
         * @todo trap the errors
59
         */
60
    }
61
    unset($table);
62
63
    // Editing smartfaq_answers table
64
    $table = new Smartfaq\SmartDbTable('smartfaq_answers');
65
    // Changing categoryid type to int(11)
66
    $table->addAlteredField('answerid', "int(11) NOT NULL default '0'", false);
67
68
    // Changing parentid type to int(11)
69
    $table->addAlteredField('faqid', "int(11) NOT NULL default '0'", false);
70
71
    if (!$dbupdater->updateTable($table)) {
72
        /**
73
         * @todo trap the errors
74
         */
75
    }
76
    unset($table);
77
78
    /**
79
     * Check for items with categoryid=0
80
     */
81
//    require_once XOOPS_ROOT_PATH . '/modules/smartfaq/include/functions.php';
82
    /** @var \XoopsModules\Smartfaq\FaqHandler $faqHandler */
83
    $smartfaq_faqHandler      = $answerHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Faq');
0 ignored issues
show
Unused Code introduced by
The assignment to $answerHandler is dead and can be removed.
Loading history...
84
    /** @var \XoopsModules\Smartfaq\CategoryHandler $smartfaq_categoryHandler */
85
    $smartfaq_categoryHandler = $answerHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Category');
86
87
    //find a valid categoryid
88
    $categoriesObj = $smartfaq_categoryHandler->getCategories(1, 0, 0, 'weight', 'ASC', false);
89
    if (count($categoriesObj) > 0) {
90
        $categoryid = $categoriesObj[0]->getVar('categoryid');
91
        $criteria   = new \CriteriaCompo();
0 ignored issues
show
Bug introduced by
The type CriteriaCompo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
92
        $criteria->add(new \Criteria('categoryid', 0));
0 ignored issues
show
Bug introduced by
The type Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
93
        $smartfaq_faqHandler->updateAll('categoryid', $categoryid, $criteria);
94
        echo '&nbsp;&nbsp;Cleaning up questions with categoryid=0<br>';
95
    }
96
97
    echo '</code>';
98
99
    $feedback = ob_get_clean();
100
    if (method_exists($module, 'setMessage')) {
101
        $module->setMessage($feedback);
102
    } else {
103
        echo $feedback;
104
    }
105
106
    return true;
107
}
108
109
/**
110
 * @param XoopsModule $module
111
 * @return bool
112
 */
113
function xoops_module_install_smartfaq($module)
114
{
115
    ob_start();
116
117
//    require_once XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/include/functions.php';
118
119
    $feedback = ob_get_clean();
120
    if (method_exists($module, 'setMessage')) {
121
        $module->setMessage($feedback);
122
    } else {
123
        echo $feedback;
124
    }
125
126
    return true;
127
}
128