Completed
Push — master ( 937117...931dfe )
by Michael
05:45 queued 02:42
created

onupdate.php ➔ xoops_module_update_soapbox()   F

Complexity

Conditions 21
Paths 260

Size

Total Lines 89
Code Lines 46

Duplication

Lines 13
Ratio 14.61 %

Importance

Changes 0
Metric Value
cc 21
eloc 46
nc 260
nop 2
dl 13
loc 89
rs 3.6155
c 0
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 32 and the first side effect is on line 24.

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 http://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 Xmf\Language;
21
22
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof XoopsUser)
0 ignored issues
show
Bug introduced by
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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)
0 ignored issues
show
Coding Style introduced by
tableExists uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
33
{
34
    $result = $GLOBALS['xoopsDB']->queryF("SHOW TABLES LIKE '$tablename'");
35
36
    return $GLOBALS['xoopsDB']->getRowsNum($result) > 0;
37
}
38
39
/**
40
 *
41
 * Prepares system prior to attempting to install module
42
 * @param XoopsModule $module {@link XoopsModule}
43
 *
44
 * @return bool true if ready to install, false if not
45
 */
46
function xoops_module_pre_update_soapbox(XoopsModule $module)
47
{
48
    $moduleDirName = basename(dirname(__DIR__));
49
    $className     = ucfirst($moduleDirName) . 'Utility';
50
    if (!class_exists($className)) {
51
        xoops_load('utility', $moduleDirName);
52
    }
53
    //check for minimum XOOPS version
54
    if (!$className::checkVerXoops($module)) {
55
        return false;
56
    }
57
58
    // check for minimum PHP version
59
    if (!$className::checkVerPhp($module)) {
60
        return false;
61
    }
62
63
    return true;
64
}
65
66
/**
67
 *
68
 * Performs tasks required during update of the module
69
 * @param XoopsModule $module {@link XoopsModule}
70
 * @param null        $previousVersion
71
 *
72
 * @return bool true if update successful, false if not
73
 */
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
74
75
function xoops_module_update_soapbox(XoopsModule $module, $previousVersion = null)
0 ignored issues
show
Coding Style introduced by
xoops_module_update_soapbox uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
76
{
77
    //    global $xoopsDB;
78
    require_once __DIR__ . '/../../../mainfile.php';
79
    require_once __DIR__ . '/../include/config.php';
80
81
    if (!isset($moduleDirName)) {
0 ignored issues
show
Bug introduced by
The variable $moduleDirName seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
82
        $moduleDirName = basename(dirname(__DIR__));
83
    }
84
85
    if (false !== ($moduleHelper = Xmf\Module\Helper::getHelper($moduleDirName))) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
86
    } else {
87
        $moduleHelper = Xmf\Module\Helper::getHelper('system');
88
    }
89
90
    // Load language files
91
    $moduleHelper->loadLanguage('admin');
92
    $moduleHelper->loadLanguage('modinfo');
93
94
    if ($previousVersion < 240) {
95
        $configurator = new ModuleConfigurator();
96
        $classUtility = ucfirst($moduleDirName) . 'Utility';
97
        if (!class_exists($classUtility)) {
98
            xoops_load('utility', $moduleDirName);
99
        }
100
101
        //delete old HTML templates
102
        if (count($configurator['templateFolders']) > 0) {
103
            foreach ($configurator['templateFolders'] as $folder) {
104
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
105
                if (is_dir($templateFolder)) {
106
                    $templateList = array_diff(scandir($templateFolder), array('..', '.'));
107
                    foreach ($templateList as $k => $v) {
108
                        $fileInfo = new SplFileInfo($templateFolder . $v);
109
                        if ($fileInfo->getExtension() === 'html' && $fileInfo->getFilename() !== 'index.html') {
110
                            if (file_exists($templateFolder . $v)) {
111
                                unlink($templateFolder . $v);
112
                            }
113
                        }
114
                    }
115
                }
116
            }
117
        }
118
119
        //  ---  DELETE OLD FILES ---------------
120
        if (count($configurator->oldFiles) > 0) {
121
            //    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...
122
            foreach (array_keys($configurator->oldFiles) as $i) {
123
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
124
                if (is_file($tempFile)) {
125
                    unlink($tempFile);
126
                }
127
            }
128
        }
129
130
        //  ---  DELETE OLD FOLDERS ---------------
131
        xoops_load('XoopsFile');
132
        if (count($configurator->oldFolders) > 0) {
133
            //    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...
134
            foreach (array_keys($configurator->oldFolders) as $i) {
135
                $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]);
136
                /** @var XoopsObjectHandler $folderHandler */
137
                $folderHandler = XoopsFile::getHandler('folder', $tempFolder);
138
                $folderHandler->delete($tempFolder);
139
            }
140
        }
141
142
        //  ---  CREATE FOLDERS ---------------
143 View Code Duplication
        if (count($configurator->uploadFolders) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
            //    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...
145
            foreach (array_keys($configurator->uploadFolders) as $i) {
146
                $classUtility::createFolder($configurator->uploadFolders[$i]);
147
            }
148
        }
149
150
        //  ---  COPY blank.png FILES ---------------
151 View Code Duplication
        if (count($configurator->blankFiles) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
            $file = __DIR__ . '/../assets/images/blank.png';
153
            foreach (array_keys($configurator->blankFiles) as $i) {
154
                $dest = $configurator->blankFiles[$i] . '/blank.png';
155
                $classUtility::copyFile($file, $dest);
156
            }
157
        }
158
159
        //delete .html entries from the tpl table
160
        $sql = 'DELETE FROM ' . $xoopsDB->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . "' AND `tpl_file` LIKE '%.html%'";
0 ignored issues
show
Bug introduced by
The variable $xoopsDB does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
161
        $xoopsDB->queryF($sql);
162
    }
163
}
164