xoops_module_update_mastopgo2()   D
last analyzed

Complexity

Conditions 18
Paths 33

Size

Total Lines 82
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 18
eloc 40
nc 33
nop 2
dl 0
loc 82
rs 4.8666
c 2
b 0
f 0

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    XOOPS Project https://xoops.org/
14
 * @license      GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team
18
 */
19
20
use XoopsModules\Mastopgo2;
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
 *
43
 * @return bool true if ready to install, false if not
44
 */
45
function xoops_module_pre_update_mastopgo2(\XoopsModule $module)
46
{
47
    $moduleDirName = basename(dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
48
    /** @var Mastopgo2\Helper $helper */
49
    /** @var Mastopgo2\Utility $utility */
50
    $helper  = Mastopgo2\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
51
    $utility = new Mastopgo2\Utility();
52
53
    $xoopsSuccess = $utility::checkVerXoops($module);
54
    $phpSuccess   = $utility::checkVerPhp($module);
55
56
    return $xoopsSuccess && $phpSuccess;
57
}
58
59
/**
60
 * Performs tasks required during update of the module
61
 * @param \XoopsModule $module {@link XoopsModule}
62
 * @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...
63
 *
64
 * @return bool true if update successful, false if not
65
 */
66
function xoops_module_update_mastopgo2(\XoopsModule $module, $previousVersion = null)
67
{
68
    $moduleDirName      = basename(dirname(__DIR__));
69
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
70
71
    /** @var Mastopgo2\Helper $helper */
72
    /** @var Mastopgo2\Utility $utility */
73
    /** @var Mastopgo2\Common\Configurator $configurator */
74
    $helper       = Mastopgo2\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
75
    $utility      = new Mastopgo2\Utility();
76
    $configurator = new Mastopgo2\Common\Configurator();
77
78
    if ($previousVersion < 240) {
79
        //delete old HTML templates
80
        if (count($configurator->templateFolders) > 0) {
81
            foreach ($configurator->templateFolders as $folder) {
82
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
83
                if (is_dir($templateFolder)) {
84
                    $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

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