protector_onupdate_base()   F
last analyzed

Complexity

Conditions 26
Paths 132

Size

Total Lines 117
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 3 Features 2
Metric Value
cc 26
eloc 70
c 9
b 3
f 2
nc 132
nop 2
dl 0
loc 117
rs 3.9

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
        /** @var XoopsModule $module */
26
        // translations on module update
27
28
        global $msgs; // TODO :-D
29
30
        if (!is_array($msgs)) {
31
            $msgs = array();
32
        }
33
34
        /** @var XoopsMySQLDatabase $db */
35
        $db  = XoopsDatabaseFactory::getDatabaseConnection();
36
        $mid = $module->getVar('mid');
37
38
        // TABLES (write here ALTER TABLE etc. if necessary)
39
40
        // configs (Though I know it is not a recommended way...)
41
        $sql = 'SHOW COLUMNS FROM ' . $db->prefix('config') . " LIKE 'conf_title'";
42
        $result = $db->query($sql);
43
        if ($result !== false && $db->isResultSet($result)) {
44
            if ($result instanceof mysqli_result && ($myrow = $db->fetchArray($result)) && isset($myrow['Type']) && $myrow['Type'] === 'varchar(30)') {
45
                $db->queryF('ALTER TABLE ' . $db->prefix('config') . " MODIFY `conf_title` varchar(255) NOT NULL default '', MODIFY `conf_desc` varchar(255) NOT NULL default ''");
46
            }
47
        }
48
49
        $sql = 'SHOW CREATE TABLE ' . $db->prefix('config');
50
        $result = $db->query($sql);
51
        if (false === $result || !($result instanceof mysqli_result) || !$db->isResultSet($result)) {
52
            throw new \RuntimeException(
53
                \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR
54
            );
55
        } else {
56
            list(, $create_string) = $db->fetchRow($result);
57
        }
58
59
60
        foreach (explode('KEY', $create_string) as $line) {
61
            if (preg_match('/(\`conf\_title_\d+\`) \(\`conf\_title\`\)/', $line, $regs)) {
62
                $db->query('ALTER TABLE ' . $db->prefix('config') . ' DROP KEY ' . $regs[1]);
63
            }
64
        }
65
        $db->query('ALTER TABLE ' . $db->prefix('config') . ' ADD KEY `conf_title` (`conf_title`)');
66
67
        // 2.x -> 3.0
68
        $sql = 'SHOW CREATE TABLE ' . $db->prefix($mydirname . '_log');
69
        $result = $db->query($sql);
70
71
        if (false === $result || !($result instanceof mysqli_result) || !$db->isResultSet($result)) {
72
            throw new \RuntimeException(
73
                \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR
74
            );
75
        } else {
76
            list(, $create_string) = $db->fetchRow($result);
77
        }
78
79
80
        if (preg_match('/timestamp\(/i', $create_string)) {
81
            $db->query('ALTER TABLE ' . $db->prefix($mydirname . '_log') . ' MODIFY `timestamp` DATETIME');
82
        }
83
84
        // TEMPLATES (all templates have been already removed by modulesadmin)
85
        /** @var XoopsTplfileHandler $tplfile_handler */
86
        $tplfile_handler = xoops_getHandler('tplfile');
87
        $tpl_path = __DIR__ . '/templates';
88
        // Check if the directory exists
89
        if (is_dir($tpl_path) && is_readable($tpl_path)) {
90
            // Try to open the directory
91
            if ($handler = opendir($tpl_path . '/')) {
92
                while (($file = readdir($handler)) !== false) {
93
                    if (substr($file, 0, 1) === '.') {
94
                        continue;
95
                    }
96
                    $file_path = $tpl_path . '/' . $file;
97
                if (is_file($file_path) && in_array(strrchr($file, '.'), array('.html', '.css', '.js'))) {
98
                        $mtime   = (int)(@filemtime($file_path));
99
                        $tplfile = $tplfile_handler->create();
100
                        $tplfile->setVar('tpl_source', file_get_contents($file_path), true);
101
                        $tplfile->setVar('tpl_refid', $mid);
102
                        $tplfile->setVar('tpl_tplset', 'default');
103
                        $tplfile->setVar('tpl_file', $mydirname . '_' . $file);
104
                        $tplfile->setVar('tpl_desc', '', true);
105
                        $tplfile->setVar('tpl_module', $mydirname);
106
                        $tplfile->setVar('tpl_lastmodified', $mtime);
107
                        $tplfile->setVar('tpl_lastimported', 0);
108
                        $tplfile->setVar('tpl_type', 'module');
109
                        if (!$tplfile_handler->insert($tplfile)) {
110
                        $ret[] = '<span style="color:#ff0000;">ERROR: Could not insert template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES | ENT_HTML5) . '</b> to the database.</span><br>';
111
                        } else {
112
                            $tplid  = $tplfile->getVar('tpl_id');
113
                            $msgs[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES | ENT_HTML5) . '</b> added to the database. (ID: <b>' . $tplid . '</b>)<br>';
114
                            // generate compiled file
115
                            include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
116
                            include_once XOOPS_ROOT_PATH . '/class/template.php';
117
                            if (!xoops_template_touch((string)$tplid)) {
118
                                $msgs[] = '<span style="color:#ff0000;">ERROR: Failed compiling template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES | ENT_HTML5) . '</b>.</span><br>';
119
                            } else {
120
                                $msgs[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES | ENT_HTML5) . '</b> compiled.</span><br>';
121
                            }
122
                        }
123
                    }
124
                }
125
                closedir($handler);
126
            } else {
127
                // Handle the error condition when opendir fails
128
                $msgs[] = '<span style="color:#ff0000;">ERROR: Could not open the template directory:  <b>' . htmlspecialchars($tpl_path) . '</b>.</span>';
129
            }
130
        } else {
131
            // Directory does not exist; handle this condition
132
            $msgs[] = '<span style="color:#ff0000;">ERROR: The template directory does not exist or is not readable: <b>' . htmlspecialchars($tpl_path) . '</b>.</span><br>';
133
        }
134
135
        include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
136
        include_once XOOPS_ROOT_PATH . '/class/template.php';
137
        xoops_template_clear_module_cache($mid);
138
139
        return true;
140
    }
141
142
    /**
143
     * @param $module_obj
144
     * @param $log
145
     */
146
    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

146
    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...
147
    {
148
        if (isset($GLOBALS['msgs']) && is_array($GLOBALS['msgs'])) {
149
            foreach ($GLOBALS['msgs'] as $message) {
150
                $log->add(strip_tags($message));
151
            }
152
        }
153
154
        // use mLog->addWarning() or mLog->addError() if necessary
155
    }
156
}
157