Passed
Push — master ( 838f24...dcfdf8 )
by Richard
05:49 queued 13s
created

protector_oninstall_base()   C

Complexity

Conditions 16
Paths 16

Size

Total Lines 105
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
cc 16
eloc 68
c 5
b 1
f 0
nc 16
nop 2
dl 0
loc 105
rs 5.5666

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_install_' . $mydirname . '( $module ) { return protector_oninstall_base( $module , "' . $mydirname . '" ) ; } ');
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
14
15
if (!function_exists('protector_oninstall_base')) {
16
17
    /**
18
     * @param $module
19
     * @param $mydirname
20
     *
21
     * @return bool
22
     */
23
    function protector_oninstall_base($module, $mydirname)
24
    {
25
        /** @var XoopsModule $module */
26
        // translations on module install
27
28
        global $ret; // TODO :-D
29
30
        if (!is_array($ret)) {
31
            $ret = array();
32
        }
33
34
        /** @var XoopsMySQLDatabase $db */
35
        $db  = XoopsDatabaseFactory::getDatabaseConnection();
36
        $mid = $module->getVar('mid');
37
38
        // TABLES (loading mysql.sql)
39
        $sql_file_path = __DIR__ . '/sql/mysql.sql';
40
        $prefix_mod    = $db->prefix() . '_' . $mydirname;
41
        if (file_exists($sql_file_path)) {
42
            $ret[] = 'SQL file found at <b>' . htmlspecialchars($sql_file_path, ENT_QUOTES) . '</b>.<br> Creating tables...<br>';
43
44
            include_once XOOPS_ROOT_PATH . '/class/database/sqlutility.php';
45
            $sqlutil = new SqlUtility; //old code is -> $sqlutil =& new SqlUtility ; //hack by Trabis
46
47
            $sql_query = trim(file_get_contents($sql_file_path));
48
            $sqlutil::splitMySqlFile($pieces, $sql_query);
49
            $created_tables = [];
50
            foreach ($pieces as $piece) {
51
                $prefixed_query = $sqlutil::prefixQuery($piece, $prefix_mod);
52
                if (!$prefixed_query) {
53
                    $ret[] = 'Invalid SQL <b>' . htmlspecialchars($piece, ENT_QUOTES) . '</b><br>';
54
55
                    return false;
56
                }
57
                if (!$db->query($prefixed_query[0])) {
58
                    $ret[] = '<b>' . htmlspecialchars($db->error(), ENT_QUOTES) . '</b><br>';
59
60
                    //var_dump( $db->error() ) ;
61
                    return false;
62
                } else {
63
                    if (!in_array($prefixed_query[4], $created_tables)) {
64
                        $ret[]            = 'Table <b>' . htmlspecialchars($prefix_mod . '_' . $prefixed_query[4], ENT_QUOTES) . '</b> created.<br>';
65
                        $created_tables[] = $prefixed_query[4];
66
                    } else {
67
                        $ret[] = 'Data inserted to table <b>' . htmlspecialchars($prefix_mod . '_' . $prefixed_query[4], ENT_QUOTES) . '</b>.</br />';
68
                    }
69
                }
70
            }
71
        }
72
73
        // TEMPLATES
74
        /** @var XoopsTplfileHandler $tplfile_handler */
75
        $tplfile_handler = xoops_getHandler('tplfile');
76
        $tpl_path        = __DIR__ . '/templates';
77
        // Check if the directory exists
78
        if (is_dir($tpl_path) && is_readable($tpl_path)) {
79
            // Try to open the directory
80
             if ($handler = opendir($tpl_path . '/')) {
81
                while (($file = readdir($handler)) !== false) {
82
                    if (substr($file, 0, 1) === '.') {
83
                        continue;
84
                    }
85
                    $file_path = $tpl_path . '/' . $file;
86
                    if (is_file($file_path) && in_array(strrchr($file, '.'),  array('.html', '.css', '.js'))) {
87
                        $mtime   = (int)(@filemtime($file_path));
88
                        $tplfile = $tplfile_handler->create();
89
                        $tplfile->setVar('tpl_source', file_get_contents($file_path), true);
90
                        $tplfile->setVar('tpl_refid', $mid);
91
                        $tplfile->setVar('tpl_tplset', 'default');
92
                        $tplfile->setVar('tpl_file', $mydirname . '_' . $file);
93
                        $tplfile->setVar('tpl_desc', '', true);
94
                        $tplfile->setVar('tpl_module', $mydirname);
95
                        $tplfile->setVar('tpl_lastmodified', $mtime);
96
                        $tplfile->setVar('tpl_lastimported', 0);
97
                        $tplfile->setVar('tpl_type', 'module');
98
                        if (!$tplfile_handler->insert($tplfile)) {
99
                            $ret[] = '<span style="color:#ff0000;">ERROR: Could not insert template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES) . '</b> to the database.</span><br>';
100
                        } else {
101
                            $tplid = $tplfile->getVar('tpl_id');
102
                            $ret[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES) . '</b> added to the database. (ID: <b>' . $tplid . '</b>)<br>';
103
                            // generate compiled file
104
                            include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
105
                            include_once XOOPS_ROOT_PATH . '/class/template.php';
106
                            if (!xoops_template_touch((string)$tplid)) {
107
                                $ret[] = '<span style="color:#ff0000;">ERROR: Failed compiling template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES) . '</b>.</span><br>';
108
                            } else {
109
                                $ret[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES) . '</b> compiled.</span><br>';
110
                            }
111
                        }
112
                    }
113
                }
114
                closedir($handler);
115
            } else {
116
                // Handle the error condition when opendir fails
117
                $ret[] = '<span style="color:#ff0000;">ERROR: Could not open the template directory:  <b>' . htmlspecialchars($tpl_path, ENT_QUOTES) . '</b>.</span><br>';
118
            }
119
        } else {
120
            // Directory does not exist; handle this condition
121
            $ret[] = '<span style="color:#ff0000;">ERROR: The template directory does not exist or is not readable: <b>' . htmlspecialchars($tpl_path, ENT_QUOTES) . '</b>.</span><br>';
122
        }
123
        include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
124
        include_once XOOPS_ROOT_PATH . '/class/template.php';
125
        xoops_template_clear_module_cache($mid);
126
127
        return true;
128
    }
129
130
    /**
131
     * @param $module_obj
132
     * @param $log
133
     */
134
    function protector_message_append_oninstall(&$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

134
    function protector_message_append_oninstall(/** @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...
135
    {
136
        if (isset($GLOBALS['ret']) && is_array($GLOBALS['ret'])) {
137
            foreach ($GLOBALS['ret'] as $message) {
138
                $log->add(strip_tags($message));
139
            }
140
        }
141
142
        // use mLog->addWarning() or mLog->addError() if necessary
143
    }
144
}
145