Passed
Pull Request — master (#1323)
by Michael
04:53
created

protector_onupdate_base()   D

Complexity

Conditions 18
Paths 64

Size

Total Lines 96
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
cc 18
eloc 62
c 4
b 1
f 1
nc 64
nop 2
dl 0
loc 96
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
        $db  = XoopsDatabaseFactory::getDatabaseConnection();
34
        $mid = $module->getVar('mid');
35
36
        // TABLES (write here ALTER TABLE etc. if necessary)
37
38
        // configs (Though I know it is not a recommended way...)
39
        $sql = 'SHOW COLUMNS FROM ' . $db->prefix('config') . " LIKE 'conf_title'";
40
        $result = $db->query($sql);
0 ignored issues
show
Bug introduced by
The method query() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

40
        /** @scrutinizer ignore-call */ 
41
        $result = $db->query($sql);
Loading history...
41
        if ($db->isResultSet($result) && ($myrow = $db->fetchArray($result)) && isset($myrow['Type']) && $myrow['Type'] === 'varchar(30)') {
0 ignored issues
show
Bug introduced by
The method fetchArray() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

41
        if ($db->isResultSet($result) && ($myrow = $db->/** @scrutinizer ignore-call */ fetchArray($result)) && isset($myrow['Type']) && $myrow['Type'] === 'varchar(30)') {
Loading history...
42
            $db->queryF('ALTER TABLE ' . $db->prefix('config') . " MODIFY `conf_title` varchar(255) NOT NULL default '', MODIFY `conf_desc` varchar(255) NOT NULL default ''");
0 ignored issues
show
Bug introduced by
The method queryF() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

42
            $db->/** @scrutinizer ignore-call */ 
43
                 queryF('ALTER TABLE ' . $db->prefix('config') . " MODIFY `conf_title` varchar(255) NOT NULL default '', MODIFY `conf_desc` varchar(255) NOT NULL default ''");
Loading history...
43
        }
44
45
        $sql = 'SHOW CREATE TABLE ' . $db->prefix('config');
46
        $result = $db->query($sql);
47
        if (!$db->isResultSet($result)) {
48
            throw new \RuntimeException(
49
                \sprintf(_DB_QUERY_ERROR, $sql) . $db->error(), E_USER_ERROR
0 ignored issues
show
Bug introduced by
The method error() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

49
                \sprintf(_DB_QUERY_ERROR, $sql) . $db->/** @scrutinizer ignore-call */ error(), E_USER_ERROR
Loading history...
50
            );
51
        }
52
        list(, $create_string) = $db->fetchRow($result);
0 ignored issues
show
Bug introduced by
The method fetchRow() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

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

125
    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...
126
    {
127
        if (isset($GLOBALS['msgs']) && is_array($GLOBALS['msgs'])) {
128
            foreach ($GLOBALS['msgs'] as $message) {
129
                $log->add(strip_tags($message));
130
            }
131
        }
132
133
        // use mLog->addWarning() or mLog->addError() if necessary
134
    }
135
}
136