Passed
Pull Request — master (#9)
by Michael
03:24
created

xoops_module_update_mylinks()   D

Complexity

Conditions 20
Paths 97

Size

Total Lines 96
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
dl 0
loc 96
rs 4.1666
c 1
b 0
f 0
cc 20
nc 97
nop 2

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
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    {@link https://xoops.org/ XOOPS Project}
14
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team
18
 */
19
20
use XoopsModules\Mylinks;
21
22
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof XoopsUser)
23
    || !$GLOBALS['xoopsUser']->isAdmin()) {
24
    exit('Restricted access' . PHP_EOL);
25
}
26
27
/**
28
 * @param string $tablename
29
 *
30
 * @return bool
31
 */
32
function tableExists($tablename)
33
{
34
    $result = $GLOBALS['xoopsDB']->queryF("SHOW TABLES LIKE '$tablename'");
35
36
    return ($GLOBALS['xoopsDB']->getRowsNum($result) > 0);
37
}
38
39
/**
40
 * Prepares system prior to attempting to install module
41
 * @param \XoopsModule $module {@link XoopsModule}
42
 * @return bool true if ready to install, false if not
43
 */
44
function xoops_module_pre_update_mylinks(\XoopsModule $module)
45
{
46
    $moduleDirName = basename(dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
47
    /** @var Mylinks\Helper $helper */
48
    /** @var Mylinks\Utility $utility */
49
    $helper  = Mylinks\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
50
    $utility = new Mylinks\Utility();
51
52
    $xoopsSuccess = $utility::checkVerXoops($module);
53
    $phpSuccess   = $utility::checkVerPhp($module);
54
55
    return $xoopsSuccess && $phpSuccess;
56
}
57
58
/**
59
 * Performs tasks required during update of the module
60
 * @param \XoopsModule $module {@link XoopsModule}
61
 * @param null         $previousVersion
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $previousVersion is correct as it would always require null to be passed?
Loading history...
62
 *
63
 * @return bool true if update successful, false if not
64
 */
65
function xoops_module_update_mylinks(\XoopsModule $module, $previousVersion = null)
66
{
67
    $moduleDirName      = basename(dirname(__DIR__));
68
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
69
70
    /** @var Mylinks\Helper $helper */ /** @var Mylinks\Utility $utility */
71
    /** @var Mylinks\Common\Configurator $configurator */
72
    $helper       = Mylinks\Helper::getInstance();
73
    $utility      = new Mylinks\Utility();
74
    $configurator = new Mylinks\Common\Configurator();
75
76
    $helper->loadLanguage('common');
77
78
    if ($previousVersion < 240) {
79
        //rename column EXAMPLE
80
        $tables     = new \Xmf\Database\Tables();
81
        $table      = 'mylinks_categories';
82
        $column     = 'ordre';
83
        $newName    = 'order';
84
        $attributes = "INT(5) NOT NULL DEFAULT '0'";
85
        if ($tables->useTable($table)) {
86
            $tables->alterColumn($table, $column, $attributes, $newName);
87
            if (!$tables->executeQueue()) {
88
                echo '<br>' . constant('CO_' . $moduleDirNameUpper . '_UPGRADEFAILED0') . ' ' . $tables->getLastError();
89
            }
90
        }
91
92
        //delete old HTML templates
93
        if (count($configurator->templateFolders) > 0) {
94
            foreach ($configurator->templateFolders as $folder) {
95
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
96
                if (is_dir($templateFolder)) {
97
                    $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']);
0 ignored issues
show
Bug introduced by
It seems like scandir($templateFolder, SCANDIR_SORT_NONE) can also be of type false; however, parameter $array1 of array_diff() does only seem to accept array, 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

97
                    $templateList = array_diff(/** @scrutinizer ignore-type */ scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']);
Loading history...
98
                    foreach ($templateList as $k => $v) {
99
                        $fileInfo = new SplFileInfo($templateFolder . $v);
100
                        if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
101
                            if (file_exists($templateFolder . $v)) {
102
                                unlink($templateFolder . $v);
103
                            }
104
                        }
105
                    }
106
                }
107
            }
108
        }
109
110
        //  ---  DELETE OLD FILES ---------------
111
        if (count($configurator->oldFiles) > 0) {
112
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
113
            foreach (array_keys($configurator->oldFiles) as $i) {
114
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
115
                if (is_file($tempFile)) {
116
                    unlink($tempFile);
117
                }
118
            }
119
        }
120
121
        //  ---  DELETE OLD FOLDERS ---------------
122
        xoops_load('XoopsFile');
123
        if (count($configurator->oldFolders) > 0) {
124
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
125
            foreach (array_keys($configurator->oldFolders) as $i) {
126
                $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]);
127
                /** @var \XoopsObjectHandler $folderHandler */
128
                $folderHandler = \XoopsFile::getHandler('folder', $tempFolder);
129
                $folderHandler->delete($tempFolder);
130
            }
131
        }
132
133
        //  ---  CREATE FOLDERS ---------------
134
        if (count($configurator->uploadFolders) > 0) {
135
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
136
            foreach (array_keys($configurator->uploadFolders) as $i) {
137
                $utility::createFolder($configurator->uploadFolders[$i]);
138
            }
139
        }
140
141
        //  ---  COPY blank.png FILES ---------------
142
        if (count($configurator->copyBlankFiles) > 0) {
143
            $file = __DIR__ . '/../assets/images/blank.png';
144
            foreach (array_keys($configurator->copyBlankFiles) as $i) {
145
                $dest = $configurator->copyBlankFiles[$i] . '/blank.png';
146
                $utility::copyFile($file, $dest);
147
            }
148
        }
149
150
        //delete .html entries from the tpl table
151
        $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . "' AND `tpl_file` LIKE '%.html%'";
152
        $GLOBALS['xoopsDB']->queryF($sql);
153
154
        /** @var \XoopsGroupPermHandler $gpermHandler */
155
        $gpermHandler = xoops_getHandler('groupperm');
156
157
        return $gpermHandler->deleteByModule($module->getVar('mid'), 'item_read');
158
    }
159
160
    return true;
161
}
162