Passed
Push — master ( 1bc300...987c24 )
by Michael
01:47
created

xoops_module_update_moduleinstaller()   D

Complexity

Conditions 18
Paths 33

Size

Total Lines 84
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 18
eloc 41
nc 33
nop 2
dl 0
loc 84
rs 4.837
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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 33 and the first side effect is on line 25.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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\Moduleinstaller;
21
22
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof \XoopsUser)
0 ignored issues
show
Bug introduced by
The type XoopsUser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
    || !$GLOBALS['xoopsUser']->IsAdmin()
24
) {
25
    exit('Restricted access' . PHP_EOL);
26
}
27
28
/**
29
 * @param string $tablename
30
 *
31
 * @return bool
32
 */
33
function tableExists($tablename)
34
{
35
    $result = $GLOBALS['xoopsDB']->queryF("SHOW TABLES LIKE '$tablename'");
36
37
    return $GLOBALS['xoopsDB']->getRowsNum($result) > 0;
38
}
39
40
/**
41
 *
42
 * Prepares system prior to attempting to install module
43
 * @param XoopsModule $module {@link XoopsModule}
44
 *
45
 * @return bool true if ready to install, false if not
46
 */
47
function xoops_module_pre_update_moduleinstaller(\XoopsModule $module)
48
{
49
    $moduleDirName = basename(dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
50
    /** @var Moduleinstaller\Helper $helper */
51
    /** @var Moduleinstaller\Utility $utility */
52
    $helper       = Moduleinstaller\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
53
    $utility      = new Moduleinstaller\Utility();
54
55
    $xoopsSuccess = $utility::checkVerXoops($module);
56
    $phpSuccess   = $utility::checkVerPhp($module);
57
    return $xoopsSuccess && $phpSuccess;
58
}
59
60
/**
61
 *
62
 * Performs tasks required during update of the module
63
 * @param XoopsModule $module {@link XoopsModule}
64
 * @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...
65
 *
66
 * @return bool true if update successful, false if not
67
 */
68
69
function xoops_module_update_moduleinstaller(\XoopsModule $module, $previousVersion = null)
70
{
71
    $moduleDirName = basename(dirname(__DIR__));
72
    $capsDirName   = strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $capsDirName is dead and can be removed.
Loading history...
73
74
75
    /** @var Moduleinstaller\Helper $helper */
76
    /** @var Moduleinstaller\Utility $utility */
77
    /** @var Moduleinstaller\Common\Configurator $configurator */
78
    $helper  = Moduleinstaller\Helper::getInstance();
79
    $utility = new Moduleinstaller\Utility();
80
    $configurator = new Moduleinstaller\Common\Configurator();
81
82
    $helper->loadLanguage('common');
83
84
    if ($previousVersion < 240) {
85
86
        //delete old HTML templates
87
        if (count($configurator->templateFolders) > 0) {
88
            foreach ($configurator->templateFolders as $folder) {
89
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
90
                if (is_dir($templateFolder)) {
91
                    $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']);
92
                    foreach ($templateList as $k => $v) {
93
                        $fileInfo = new \SplFileInfo($templateFolder . $v);
94
                        if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
95
                            if (file_exists($templateFolder . $v)) {
96
                                unlink($templateFolder . $v);
97
                            }
98
                        }
99
                    }
100
                }
101
            }
102
        }
103
104
        //  ---  DELETE OLD FILES ---------------
105
        if (count($configurator->oldFiles) > 0) {
106
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
107
            foreach (array_keys($configurator->oldFiles) as $i) {
108
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
109
                if (is_file($tempFile)) {
110
                    unlink($tempFile);
111
                }
112
            }
113
        }
114
115
        //  ---  DELETE OLD FOLDERS ---------------
116
        xoops_load('XoopsFile');
0 ignored issues
show
Bug introduced by
The function xoops_load was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

116
        /** @scrutinizer ignore-call */ 
117
        xoops_load('XoopsFile');
Loading history...
117
        if (count($configurator->oldFolders) > 0) {
118
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
119
            foreach (array_keys($configurator->oldFolders) as $i) {
120
                $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]);
121
                /* @var $folderHandler XoopsObjectHandler */
122
                $folderHandler = XoopsFile::getHandler('folder', $tempFolder);
0 ignored issues
show
Bug introduced by
The type XoopsFile was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
123
                $folderHandler->delete($tempFolder);
124
            }
125
        }
126
127
        //  ---  CREATE FOLDERS ---------------
128
        if (count($configurator->uploadFolders) > 0) {
129
            //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
130
            foreach (array_keys($configurator->uploadFolders) as $i) {
131
                $utility::createFolder($configurator->uploadFolders[$i]);
132
            }
133
        }
134
135
        //  ---  COPY blank.png FILES ---------------
136
        if (count($configurator->copyBlankFiles) > 0) {
137
            $file = __DIR__ . '/../assets/images/blank.png';
138
            foreach (array_keys($configurator->copyBlankFiles) as $i) {
139
                $dest = $configurator->copyBlankFiles[$i] . '/blank.png';
140
                $utility::copyFile($file, $dest);
141
            }
142
        }
143
144
        //delete .html entries from the tpl table
145
        $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . '\' AND `tpl_file` LIKE \'%.html%\'';
146
        $GLOBALS['xoopsDB']->queryF($sql);
147
148
        /** @var XoopsGroupPermHandler $gpermHandler */
149
        $gpermHandler = xoops_getHandler('groupperm');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

149
        $gpermHandler = /** @scrutinizer ignore-call */ xoops_getHandler('groupperm');
Loading history...
150
        return $gpermHandler->deleteByModule($module->getVar('mid'), 'item_read');
151
    }
152
    return true;
153
}
154