Passed
Push — master ( dcbd4a...4bb7b0 )
by Michael
02:37
created

onupdate.php ➔ xoops_module_update_marquee()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 96
Code Lines 24

Duplication

Lines 13
Ratio 13.54 %

Importance

Changes 0
Metric Value
cc 6
eloc 24
nc 3
nop 2
dl 13
loc 96
rs 8.1954
c 0
b 0
f 0

How to fix   Long Method   

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\Marquee;
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_marquee(\XoopsModule $module)
48
{
49
    /** @var Marquee\Utility $utility */
50
    $utility      = new Marquee\Utility();
51
52
    $xoopsSuccess = $utility::checkVerXoops($module);
53
    $phpSuccess   = $utility::checkVerPhp($module);
54
    return $xoopsSuccess && $phpSuccess;
55
}
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
66
function xoops_module_update_marquee(\XoopsModule $module, $previousVersion = null)
67
{
68
    $moduleDirName = basename(dirname(__DIR__));
69
    $capsDirName   = strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $capsDirName is dead and can be removed.
Loading history...
70
71
    /** @var Marquee\Helper $helper */
72
    /** @var Marquee\Utility $utility */
73
    /** @var Marquee\Common\Configurator $configurator */
74
    $helper  = Marquee\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 Marquee\Utility();
76
    $configurator = new Marquee\Common\Configurator();
77
78
    if ($previousVersion < 260) {
79
80
        //rename column EXAMPLE
81
        $tables     = new Xmf\Database\Tables();
0 ignored issues
show
Bug introduced by
The type Xmf\Database\Tables 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...
82
        $table      = 'marqueex_categories';
83
        $column     = 'ordre';
84
        $newName    = 'order';
85
        $attributes = "INT(5) NOT NULL DEFAULT '0'";
86
        if ($tables->useTable($table)) {
87
            $tables->alterColumn($table, $column, $attributes, $newName);
88
            if (!$tables->executeQueue()) {
89
                echo '<br>' . _AM_MARQUEE_UPGRADEFAILED0 . ' ' . $migrate->getLastError();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $migrate seems to be never defined.
Loading history...
90
            }
91
        }
92
93
        //delete old HTML templates
94
        if (count($configurator->templateFolders) > 0) {
95
            foreach ($configurator->templateFolders as $folder) {
96
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
97
                if (is_dir($templateFolder)) {
98
                    $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']);
99
                    foreach ($templateList as $k => $v) {
100
                        $fileInfo = new \SplFileInfo($templateFolder . $v);
101
                        if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
102
                            if (file_exists($templateFolder . $v)) {
103
                                unlink($templateFolder . $v);
104
                            }
105
                        }
106
                    }
107
                }
108
            }
109
        }
110
111
        //  ---  DELETE OLD FILES ---------------
112
        if (count($configurator->oldFiles) > 0) {
113
            //    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...
114
            foreach (array_keys($configurator->oldFiles) as $i) {
115
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
116
                if (is_file($tempFile)) {
117
                    unlink($tempFile);
118
                }
119
            }
120
        }
121
122
        //  ---  DELETE OLD FOLDERS ---------------
123
        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

123
        /** @scrutinizer ignore-call */ 
124
        xoops_load('XoopsFile');
Loading history...
124
        if (count($configurator->oldFolders) > 0) {
125
            //    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...
126
            foreach (array_keys($configurator->oldFolders) as $i) {
127
                $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]);
128
                /* @var $folderHandler XoopsObjectHandler */
129
                $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...
130
                $folderHandler->delete($tempFolder);
131
            }
132
        }
133
134
        //  ---  CREATE FOLDERS ---------------
135
        if (count($configurator->uploadFolders) > 0) {
136
            //    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...
137
            foreach (array_keys($configurator->uploadFolders) as $i) {
138
                $utility::createFolder($configurator->uploadFolders[$i]);
139
            }
140
        }
141
142
        //  ---  COPY blank.png FILES ---------------
143
        if (count($configurator->copyBlankFiles) > 0) {
144
            $file = __DIR__ . '/../assets/images/blank.png';
145
            foreach (array_keys($configurator->copyBlankFiles) as $i) {
146
                $dest = $configurator->copyBlankFiles[$i] . '/blank.png';
147
                $utility::copyFile($file, $dest);
148
            }
149
        }
150
151
        //delete .html entries from the tpl table
152
        $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . '\' AND `tpl_file` LIKE \'%.html%\'';
153
        $GLOBALS['xoopsDB']->queryF($sql);
154
155
        /** @var \XoopsGroupPermHandler $gpermHandler */
156
        $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

156
        $gpermHandler = /** @scrutinizer ignore-call */ xoops_getHandler('groupperm');
Loading history...
157
        return $gpermHandler->deleteByModule($module->getVar('mid'), 'item_read');
158
    }
159
    return true;
160
}
161