Passed
Push — master ( e3d02e...b8a1dc )
by Michael
03:24 queued 12s
created

xoops_module_update_xoopsheadline()   D

Complexity

Conditions 20
Paths 97

Size

Total Lines 92
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 49
c 2
b 0
f 0
dl 0
loc 92
rs 4.1666
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    XOOPS Project https://xoops.org/
14
 * @license      GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team
18
 */
19
20
use Xmf\Database\Tables;
21
use XoopsModules\Xoopsheadline\{
22
    Common\Configurator,
23
    Helper,
24
    Utility
25
};
26
/** @var Helper $helper */
27
/** @var Utility $utility */
28
/** @var Configurator $configurator */
29
30
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof \XoopsUser)
31
    || !$GLOBALS['xoopsUser']->isAdmin()) {
32
    exit('Restricted access' . PHP_EOL);
33
}
34
35
/**
36
 * @param string $tablename
37
 *
38
 * @return bool
39
 */
40
function tableExists($tablename)
41
{
42
    $result = $GLOBALS['xoopsDB']->queryF("SHOW TABLES LIKE '$tablename'");
43
44
    return ($GLOBALS['xoopsDB']->getRowsNum($result) > 0) ? true : false;
45
}
46
47
/**
48
 * Prepares system prior to attempting to install module
49
 * @param \XoopsModule $module {@link XoopsModule}
50
 *
51
 * @return bool true if ready to install, false if not
52
 */
53
function xoops_module_pre_update_xoopsheadline(\XoopsModule $module)
54
{
55
    $moduleDirName = basename(dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
56
    $helper  = Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
57
    $utility = new Utility();
58
59
    $xoopsSuccess = $utility::checkVerXoops($module);
60
    $phpSuccess   = $utility::checkVerPhp($module);
61
62
    return $xoopsSuccess && $phpSuccess;
63
}
64
65
/**
66
 * Performs tasks required during update of the module
67
 * @param \XoopsModule $module {@link XoopsModule}
68
 * @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...
69
 *
70
 * @return bool true if update successful, false if not
71
 */
72
function xoops_module_update_xoopsheadline(\XoopsModule $module, $previousVersion = null)
73
{
74
    $moduleDirName      = basename(dirname(__DIR__));
75
    $moduleDirNameUpper = mb_strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirNameUpper is dead and can be removed.
Loading history...
76
77
    $helper       = Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
78
    $utility      = new Utility();
79
    $configurator = new Configurator();
80
81
    if ($previousVersion < 240) {
82
        //rename column EXAMPLE
83
        $tables     = new Tables();
84
        $table      = 'xoopsheadlinex_categories';
85
        $column     = 'ordre';
86
        $newName    = 'order';
87
        $attributes = "INT(5) NOT NULL DEFAULT '0'";
88
        if ($tables->useTable($table)) {
89
            $tables->alterColumn($table, $column, $attributes, $newName);
90
            if (!$tables->executeQueue()) {
91
                echo '<br>' . _AM_XOOPSHEADLINE_UPGRADEFAILED0 . ' ' . $tables->getLastError();
0 ignored issues
show
Bug introduced by
The constant _AM_XOOPSHEADLINE_UPGRADEFAILED0 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
92
            }
93
        }
94
95
        //delete old HTML templates
96
        if (count($configurator->templateFolders) > 0) {
97
            foreach ($configurator->templateFolders as $folder) {
98
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
99
                if (is_dir($templateFolder)) {
100
                    $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

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