protector_oninstall_base()   C
last analyzed

Complexity

Conditions 17
Paths 24

Size

Total Lines 111
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
cc 17
eloc 71
c 6
b 1
f 0
nc 24
nop 2
dl 0
loc 111
rs 5.2166

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

141
    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...
142
    {
143
        if (isset($GLOBALS['ret']) && is_array($GLOBALS['ret'])) {
144
            foreach ($GLOBALS['ret'] as $message) {
145
                $log->add(strip_tags($message));
146
            }
147
        }
148
149
        // use mLog->addWarning() or mLog->addError() if necessary
150
    }
151
}
152