protector_onupdate_base()   F
last analyzed

Complexity

Conditions 26
Paths 132

Size

Total Lines 119
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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