Passed
Pull Request — master (#1323)
by Michael
05:58
created

protector_onupdate_base()   D

Complexity

Conditions 18
Paths 64

Size

Total Lines 97
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 1
Metric Value
cc 18
eloc 62
c 5
b 2
f 1
nc 64
nop 2
dl 0
loc 97
rs 4.8666

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
2
// start hack by Trabis
3
if (!class_exists('ProtectorRegistry')) {
4
    exit('Registry not found');
5
}
6
7
$registry  = ProtectorRegistry::getInstance();
8
$mydirname = $registry->getEntry('mydirname');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $mydirname is correct as $registry->getEntry('mydirname') targeting ProtectorRegistry::getEntry() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
9
$mydirpath = $registry->getEntry('mydirpath');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $mydirpath is correct as $registry->getEntry('mydirpath') targeting ProtectorRegistry::getEntry() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
10
$language  = $registry->getEntry('language');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $language is correct as $registry->getEntry('language') targeting ProtectorRegistry::getEntry() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
11
// end hack by Trabis
12
13
eval(' function xoops_module_update_' . $mydirname . '( $module ) { return protector_onupdate_base( $module , "' . $mydirname . '" ) ; } ');
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
14
15
if (!function_exists('protector_onupdate_base')) {
16
17
    /**
18
     * @param $module
19
     * @param $mydirname
20
     *
21
     * @return bool
22
     */
23
    function protector_onupdate_base($module, $mydirname)
24
    {
25
        // translations on module update
26
27
        global $msgs; // TODO :-D
28
29
        if (!is_array($msgs)) {
30
            $msgs = array();
31
        }
32
33
        /** @var XoopsMySQLDatabase $db */
34
        $db  = XoopsDatabaseFactory::getDatabaseConnection();
35
        $mid = $module->getVar('mid');
36
37
        // TABLES (write here ALTER TABLE etc. if necessary)
38
39
        // configs (Though I know it is not a recommended way...)
40
        $sql = 'SHOW COLUMNS FROM ' . $db->prefix('config') . " LIKE 'conf_title'";
41
        $result = $db->query($sql);
42
        if ($db->isResultSet($result) && ($myrow = $db->fetchArray($result)) && isset($myrow['Type']) && $myrow['Type'] === 'varchar(30)') {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        if ($db->isResultSet($result) && ($myrow = $db->fetchArray(/** @scrutinizer ignore-type */ $result)) && isset($myrow['Type']) && $myrow['Type'] === 'varchar(30)') {
Loading history...
43
            $db->queryF('ALTER TABLE ' . $db->prefix('config') . " MODIFY `conf_title` varchar(255) NOT NULL default '', MODIFY `conf_desc` varchar(255) NOT NULL default ''");
44
        }
45
46
        $sql = 'SHOW CREATE TABLE ' . $db->prefix('config');
47
        $result = $db->query($sql);
48
        if (!$db->isResultSet($result)) {
49
            throw new \RuntimeException(
50
                \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR
51
            );
52
        }
53
        list(, $create_string) = $db->fetchRow($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchRow() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        list(, $create_string) = $db->fetchRow(/** @scrutinizer ignore-type */ $result);
Loading history...
54
55
        foreach (explode('KEY', $create_string) as $line) {
56
            if (preg_match('/(\`conf\_title_\d+\`) \(\`conf\_title\`\)/', $line, $regs)) {
57
                $db->query('ALTER TABLE ' . $db->prefix('config') . ' DROP KEY ' . $regs[1]);
58
            }
59
        }
60
        $db->query('ALTER TABLE ' . $db->prefix('config') . ' ADD KEY `conf_title` (`conf_title`)');
61
62
        // 2.x -> 3.0
63
        $sql = 'SHOW CREATE TABLE ' . $db->prefix($mydirname . '_log');
64
        $result = $db->query($sql);
65
        if (!$db->isResultSet($result)) {
66
            throw new \RuntimeException(
67
                \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR
68
            );
69
        }
70
        list(, $create_string) = $db->fetchRow($result);
71
        if (preg_match('/timestamp\(/i', $create_string)) {
72
            $db->query('ALTER TABLE ' . $db->prefix($mydirname . '_log') . ' MODIFY `timestamp` DATETIME');
73
        }
74
75
        // TEMPLATES (all templates have been already removed by modulesadmin)
76
        /** @var XoopsTplfileHandler $tplfile_handler */
77
        $tplfile_handler = xoops_getHandler('tplfile');
78
        $tpl_path        = __DIR__ . '/templates';
79
        if ($handler = @opendir($tpl_path . '/')) {
80
            while (($file = readdir($handler)) !== false) {
81
                if (substr($file, 0, 1) === '.') {
82
                    continue;
83
                }
84
                $file_path = $tpl_path . '/' . $file;
85
                if (is_file($file_path) && in_array(strrchr($file, '.'), array('.html', '.css', '.js'))) {
86
                    $mtime   = (int)(@filemtime($file_path));
87
                    $tplfile = $tplfile_handler->create();
88
                    $tplfile->setVar('tpl_source', file_get_contents($file_path), true);
89
                    $tplfile->setVar('tpl_refid', $mid);
90
                    $tplfile->setVar('tpl_tplset', 'default');
91
                    $tplfile->setVar('tpl_file', $mydirname . '_' . $file);
92
                    $tplfile->setVar('tpl_desc', '', true);
93
                    $tplfile->setVar('tpl_module', $mydirname);
94
                    $tplfile->setVar('tpl_lastmodified', $mtime);
95
                    $tplfile->setVar('tpl_lastimported', 0);
96
                    $tplfile->setVar('tpl_type', 'module');
97
                    if (!$tplfile_handler->insert($tplfile)) {
98
                        $msgs[] = '<span style="color:#ff0000;">ERROR: Could not insert template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES) . '</b> to the database.</span>';
99
                    } else {
100
                        $tplid  = $tplfile->getVar('tpl_id');
101
                        $msgs[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES) . '</b> added to the database. (ID: <b>' . $tplid . '</b>)';
102
                        // generate compiled file
103
                        include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
104
                        include_once XOOPS_ROOT_PATH . '/class/template.php';
105
                        if (!xoops_template_touch((string)$tplid)) {
106
                            $msgs[] = '<span style="color:#ff0000;">ERROR: Failed compiling template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES) . '</b>.</span>';
107
                        } else {
108
                            $msgs[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES) . '</b> compiled.</span>';
109
                        }
110
                    }
111
                }
112
            }
113
            closedir($handler);
114
        }
115
        include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
116
        include_once XOOPS_ROOT_PATH . '/class/template.php';
117
        xoops_template_clear_module_cache($mid);
118
119
        return true;
120
    }
121
122
    /**
123
     * @param $module_obj
124
     * @param $log
125
     */
126
    function protector_message_append_onupdate(&$module_obj, &$log)
0 ignored issues
show
Unused Code introduced by
The parameter $module_obj is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

126
    function protector_message_append_onupdate(/** @scrutinizer ignore-unused */ &$module_obj, &$log)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
    {
128
        if (isset($GLOBALS['msgs']) && is_array($GLOBALS['msgs'])) {
129
            foreach ($GLOBALS['msgs'] as $message) {
130
                $log->add(strip_tags($message));
131
            }
132
        }
133
134
        // use mLog->addWarning() or mLog->addError() if necessary
135
    }
136
}
137