Passed
Push — master ( 6ae33d...022918 )
by Michael
03:45 queued 02:16
created

action.module.php ➔ xoops_module_update_about()   D

Complexity

Conditions 12
Paths 216

Size

Total Lines 43
Code Lines 23

Duplication

Lines 16
Ratio 37.21 %

Importance

Changes 0
Metric Value
cc 12
eloc 23
nc 216
nop 2
dl 16
loc 43
rs 4.4061
c 0
b 0
f 0

How to fix   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
use XoopsModules\About;
4
5
defined('XOOPS_ROOT_PATH') || die('Restricted access');
6
7
/**
8
 * Prepares system prior to attempting to install module
9
 *
10
 * @param \XoopsModule $module
11
 *
12
 * @return bool       true if ready to install, false if not
13
 */
14
function xoops_module_pre_install_about(\XoopsModule $module)
15
{
16
    require_once dirname(__DIR__) . '/preloads/autoloader.php';
17
    /** @var About\Utility $utility */
18
    $utility      = new About\Utility();
19
    $xoopsSuccess = $utility::checkVerXoops($module);
20
    $phpSuccess   = $utility::checkVerPhp($module);
21
22
    if ($xoopsSuccess && $phpSuccess) {
23
        $moduleTables = &$module->getInfo('tables');
24
        foreach ($moduleTables as $table) {
25
            $GLOBALS['xoopsDB']->queryF('DROP TABLE IF EXISTS ' . $GLOBALS['xoopsDB']->prefix($table) . ';');
26
        }
27
    }
28
29
    return $xoopsSuccess && $phpSuccess;
30
}
31
32
/**
33
 * @param  \XoopsModule $module
34
 * @return bool        true if install successful, false if not
35
 */
36
function xoops_module_install_about(\XoopsModule $module)
37
{
38
    $success   = true;
39
    $data_file = XOOPS_ROOT_PATH . '/modules/about/sql/mysql.about.sql';
40
    $GLOBALS['xoopsDB']->queryF('SET NAMES utf8');
41
    if (!$GLOBALS['xoopsDB']->queryFromFile($data_file)) {
42
        $module->setErrors('Pre-set data was not installed');
43
        // preset info not set, but don't 'fail' install because of this
44
        //$success = false;
45
    }
46
47
    // Delete files from previous version (if they exist)
48
    // this is only executed if this version copied over old version without running module update
49
    $oldFiles = [
50
        XOOPS_ROOT_PATH . '/modules/' . $module->dirname() . '/include/xoopsformloader.php',
51
        XOOPS_ROOT_PATH . '/modules/' . $module->dirname() . '/include/blockform.php',
52
        XOOPS_ROOT_PATH . '/modules/' . $module->dirname() . '/class/utilities.php',
53
    ];
54
    foreach ($oldFiles as $file) {
55
        if (is_file($file)) {
56
            $delOk = unlink($file);
57
            if (!$delOk) {
58
                $module->setErrors(sprintf(_AM_ABOUT_ERROR_BAD_REMOVE, $file));
59
            } else {
60
                $module->setErrors(sprintf(_AM_ABOUT_DELETED, $file));
61
            }
62
            $success = $success && $delOk;
63
        }
64
    }
65
    // Create uploads folder
66
    $dirOk = mkdir(XOOPS_UPLOAD_PATH . '/' . $module->dirname());
67
    if (!$dirOk) {
68
        $module->setErrors(_AM_ABOUT_ERROR_BAD_UPLOAD_DIR);
69
    }
70
71
    return $dirOk && $success;
72
}
73
74
/**
75
 * Prepares system prior to attempting to install module
76
 *
77
 * @param \XoopsModule $module
78
 *
79
 * @return bool       true if ready to install, false if not
80
 */
81
function xoops_module_pre_update_about(\XoopsModule $module)
82
{
83
    /** @var About\Helper $helper */
84
    /** @var About\Utility $utility */
85
    $moduleDirName = basename(dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
86
    /** @var \XoopsModules\About\Helper $helper */
87
    $helper  = \XoopsModules\About\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
88
    $utility = new About\Utility();
89
90
    $xoopsSuccess = $utility::checkVerXoops($module);
91
    $phpSuccess   = $utility::checkVerPhp($module);
92
93
    return $xoopsSuccess && $phpSuccess;
94
}
95
96
/**
97
 * @param  XoopsModule $module
98
 * @param  null        $prev_version
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $prev_version is correct as it would always require null to be passed?
Loading history...
99
 * @return bool        true if update successful, false if not
100
 */
101
function xoops_module_update_about(\XoopsModule $module, $prev_version = null)
0 ignored issues
show
Unused Code introduced by
The parameter $prev_version 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

101
function xoops_module_update_about(\XoopsModule $module, /** @scrutinizer ignore-unused */ $prev_version = null)

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...
102
{
103
    $success = true;
104
    // Delete files from previous version (if they exist)
105
    $oldFiles = [
106
        XOOPS_ROOT_PATH . '/modules/' . $module->dirname() . '/include/xoopsformloader.php',
107
        XOOPS_ROOT_PATH . '/modules/' . $module->dirname() . '/include/blockform.php',
108
        XOOPS_ROOT_PATH . '/modules/' . $module->dirname() . '/class/utilities.php',
109
    ];
110
    foreach ($oldFiles as $file) {
111
        if (is_file($file)) {
112
            if (!$delOk = unlink($file)) {
113
                $module->setErrors(sprintf(_AM_ABOUT_ERROR_BAD_REMOVE, $file));
114
            }
115
            $success = $success && $delOk;
116
        }
117
    }
118
119
    // Delete files from previous version (if they exist)
120
    // this is only executed if this version copied over old version without running module update
121
    $oldFiles = [
122
        XOOPS_PATH . '/modules/' . $module->dirname() . '/include/xoopsformloader.php',
123
        XOOPS_PATH . '/modules/' . $module->dirname() . '/include/blockform.php',
124
    ];
125
    foreach ($oldFiles as $file) {
126
        if (is_file($file)) {
127
            if (!$delOk = unlink($file)) {
128
                $module->setErrors(sprintf(_AM_ABOUT_ERROR_BAD_REMOVE, $file));
129
            }
130
            $success = $success && $delOk;
131
        }
132
    }
133
134
    // Create uploads folder if it doesn't exist
135
    $dirOk = true;
136
    if (!file_exists(XOOPS_UPLOAD_PATH . '/' . $module->dirname())) {
137
        // File doesn't exist so try and create it
138
        $dirOk = mkdir(XOOPS_UPLOAD_PATH . '/' . $module->dirname());
139
        if (!$dirOk) {
140
            $module->setErrors(_AM_ABOUT_ERROR_BAD_UPLOAD_DIR);
141
        }
142
    }
143
144
    return $dirOk && $success;
145
}
146
147
/**
148
 * Function to complete upon module uninstall
149
 *
150
 * @param \XoopsModule $module
151
 *
152
 * @return bool       true if successfully executed uninstall of module, false if not
153
 */
154
function xoops_module_uninstall_about(\XoopsModule $module)
155
{
156
    $moduleDirName = $module->dirname();
157
    /** @var \XoopsModules\About\Helper $helper */
158
    $helper  = \XoopsModules\About\Helper::getInstance();
159
    $utility = new \XoopsModules\About\Utility();
160
161
    $success = true;
162
    $helper->loadLanguage('admin');
163
164
    //------------------------------------------------------------------
165
    // Remove module uploads folder (and all subfolders) if they exist
166
    //------------------------------------------------------------------
167
168
    $old_directories = [XOOPS_UPLOAD_PATH . "/{$moduleDirName}"];
169
    foreach ($old_directories as $old_dir) {
170
        $dirInfo = new \SplFileInfo($old_dir);
171
        if ($dirInfo->isDir()) {
172
            // The directory exists so delete it
173
            if (!$utility::rrmdir($old_dir)) {
174
                $module->setErrors(sprintf(_AM_ABOUT_ERROR_BAD_DEL_PATH, $old_dir));
175
                $success = false;
176
            }
177
        }
178
        unset($dirInfo);
179
    }
180
181
    return $success;
182
}
183