Passed
Pull Request — master (#1399)
by Richard
04:56
created

protector_oninstall_base()   C

Complexity

Conditions 16
Paths 32

Size

Total Lines 108
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 16
eloc 72
c 3
b 1
f 0
nc 32
nop 2
dl 0
loc 108
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
        $db  = XoopsDatabaseFactory::getDatabaseConnection();
35
        $mid = $module->getVar('mid');
36
        if (!is_array($ret)) {
0 ignored issues
show
introduced by
The condition is_array($ret) is always true.
Loading history...
37
            $ret = array();
38
        }
39
40
        // TABLES (loading mysql.sql)
41
        $sql_file_path = __DIR__ . '/sql/mysql.sql';
42
        $prefix_mod    = $db->prefix() . '_' . $mydirname;
43
        if (file_exists($sql_file_path)) {
44
            $ret[] = 'SQL file found at <b>' . htmlspecialchars($sql_file_path, ENT_QUOTES) . '</b>.<br> Creating tables...';
45
46
                include_once XOOPS_ROOT_PATH . '/class/database/sqlutility.php';
47
                $sqlutil = new SqlUtility; //old code is -> $sqlutil =& new SqlUtility ; //hack by Trabis
48
49
            $sql_query = trim(file_get_contents($sql_file_path));
50
            $sqlutil->splitMySqlFile($pieces, $sql_query);
51
            $created_tables = array();
52
            foreach ($pieces as $piece) {
53
                $prefixed_query = $sqlutil->prefixQuery($piece, $prefix_mod);
54
                if (!$prefixed_query) {
55
                    $ret[] = 'Invalid SQL <b>' . htmlspecialchars($piece, ENT_QUOTES) . '</b><br>';
56
57
                    return false;
58
                }
59
                if (!$db->query($prefixed_query[0])) {
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

59
                if (!$db->/** @scrutinizer ignore-call */ query($prefixed_query[0])) {
Loading history...
60
                    $ret[] = '<b>' . htmlspecialchars($db->error(), ENT_QUOTES) . '</b><br>';
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

60
                    $ret[] = '<b>' . htmlspecialchars($db->/** @scrutinizer ignore-call */ error(), ENT_QUOTES) . '</b><br>';
Loading history...
61
62
                    //var_dump( $db->error() ) ;
63
                    return false;
64
                } else {
65
                    if (!in_array($prefixed_query[4], $created_tables)) {
66
                        $ret[]            = 'Table <b>' . htmlspecialchars($prefix_mod . '_' . $prefixed_query[4], ENT_QUOTES) . '</b> created.<br>';
67
                        $created_tables[] = $prefixed_query[4];
68
                    } else {
69
                        $ret[] = 'Data inserted to table <b>' . htmlspecialchars($prefix_mod . '_' . $prefixed_query[4], ENT_QUOTES) . '</b>.</br />';
70
                    }
71
                }
72
            }
73
        }
74
75
        // TEMPLATES
76
        $tplfile_handler = xoops_getHandler('tplfile');
77
        $tpl_path        = __DIR__ . '/templates';
78
        // Check if the directory exists
79
        if (is_dir($tpl_path)) {
80
            // Try to open the directory
81
            $handler = opendir($tpl_path . '/');
82
            if (false !== $handler) {
83
            while (($file = readdir($handler)) !== false) {
84
                if (substr($file, 0, 1) === '.') {
85
                    continue;
86
                }
87
                $file_path = $tpl_path . '/' . $file;
88
                if (is_file($file_path) && in_array(strrchr($file, '.'), array('.html', '.css', '.js'))) {
89
                    $mtime   = (int)(@filemtime($file_path));
90
                    $tplfile = $tplfile_handler->create();
91
                    $tplfile->setVar('tpl_source', file_get_contents($file_path), true);
92
                    $tplfile->setVar('tpl_refid', $mid);
93
                    $tplfile->setVar('tpl_tplset', 'default');
94
                    $tplfile->setVar('tpl_file', $mydirname . '_' . $file);
95
                    $tplfile->setVar('tpl_desc', '', true);
96
                    $tplfile->setVar('tpl_module', $mydirname);
97
                    $tplfile->setVar('tpl_lastmodified', $mtime);
98
                    $tplfile->setVar('tpl_lastimported', 0);
99
                    $tplfile->setVar('tpl_type', 'module');
100
                    if (!$tplfile_handler->insert($tplfile)) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of $tplfile_handler->insert($tplfile) targeting XoopsObjectHandler::insert() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
101
                        $ret[] = '<span style="color:#ff0000;">ERROR: Could not insert template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES) . '</b> to the database.</span><br>';
102
                    } else {
103
                        $tplid = $tplfile->getVar('tpl_id');
104
                        $ret[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES) . '</b> added to the database. (ID: <b>' . $tplid . '</b>)<br>';
105
                        // generate compiled file
106
                        include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
107
                        include_once XOOPS_ROOT_PATH . '/class/template.php';
108
                        if (!xoops_template_touch($tplid)) {
0 ignored issues
show
Bug introduced by
It seems like $tplid can also be of type array and array; however, parameter $tpl_id of xoops_template_touch() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

108
                        if (!xoops_template_touch(/** @scrutinizer ignore-type */ $tplid)) {
Loading history...
109
                            $ret[] = '<span style="color:#ff0000;">ERROR: Failed compiling template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES) . '</b>.</span><br>';
110
                        } else {
111
                            $ret[] = 'Template <b>' . htmlspecialchars($mydirname . '_' . $file, ENT_QUOTES) . '</b> compiled.</span><br>';
112
                        }
113
                    }
114
                }
115
            }
116
            closedir($handler);
117
            } else {
118
                // Handle the error condition when opendir fails
119
                $ret[] = '<span style="color:#ff0000;">ERROR: Could not open the directory:  <b>' . htmlspecialchars($tpl_path) . '</b>.</span><br>';
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 directory:  <b>' . htmlspecialchars($tpl_path) . '</b>.</span><br>';
125
            }
126
        include_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
127
        include_once XOOPS_ROOT_PATH . '/class/template.php';
128
        xoops_template_clear_module_cache($mid);
129
130
        return true;
131
    }
132
133
    /**
134
     * @param $module_obj
135
     * @param $log
136
     */
137
    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

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