Completed
Branch master (c921ab)
by Michael
109:50 queued 98:29
created

onupdate.php ➔ xoops_module_update_extcal()   C

Complexity

Conditions 7
Paths 17

Size

Total Lines 113
Code Lines 34

Duplication

Lines 19
Ratio 16.81 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 34
c 1
b 0
f 0
nc 17
nop 2
dl 19
loc 113
rs 6.4589

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 30 and the first side effect is on line 22.

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
 * @author       XOOPS Development Team
17
 */
18
19
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...
20
    || !$GLOBALS['xoopsUser']->IsAdmin()
21
) {
22
    exit('Restricted access' . PHP_EOL);
23
}
24
25
/**
26
 * @param string $tablename
27
 *
28
 * @return bool
29
 */
30
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...
31
{
32
    $result = $GLOBALS['xoopsDB']->queryF("SHOW TABLES LIKE '$tablename'");
33
34
    return ($GLOBALS['xoopsDB']->getRowsNum($result) > 0) ? true : false;
35
}
36
37
/**
38
 *
39
 * Prepares system prior to attempting to install module
40
 * @param XoopsModule $module {@link XoopsModule}
41
 *
42
 * @return bool true if ready to install, false if not
43
 */
44
function xoops_module_pre_update_extcal(XoopsModule $module)
45
{
46
    $moduleDirName  = basename(dirname(__DIR__));
47
    $classUtilities = ucfirst($moduleDirName) . 'Utilities';
48
    if (!class_exists($classUtilities)) {
49
        xoops_load('utilities', $moduleDirName);
50
    }
51
    //check for minimum XOOPS version
52
    if (!$classUtilities::checkXoopsVer($module)) {
53
        return false;
54
    }
55
56
    // check for minimum PHP version
57
    if (!$classUtilities::checkPHPVer($module)) {
58
        return false;
59
    }
60
61
    return true;
62
}
63
64
/**
65
 *
66
 * Performs tasks required during update of the module
67
 * @param XoopsModule $module {@link XoopsModule}
68
 * @param null        $previousVersion
69
 *
70
 * @return bool true if update successful, false if not
71
 */
72
73
function xoops_module_update_extcal(XoopsModule $module, $previousVersion = null)
0 ignored issues
show
Coding Style introduced by
xoops_module_update_extcal 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...
74
{
75
//    global $xoopsDB;
76
77
    $moduleDirName = basename(dirname(__DIR__));
78
79
    $newVersion = $xoopsModule->getVar('version') * 100;
0 ignored issues
show
Bug introduced by
The variable $xoopsModule does not exist. Did you mean $module?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
80
    if ($newVersion == $previousVersion) {
81
        return true;
82
    }
83
84
85
    $fld = XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->getVar('dirname') . '/versions/';
0 ignored issues
show
Bug introduced by
The variable $xoopsModule does not exist. Did you mean $module?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
86
    $cls = 'extcal_%1$s';
87
88
    $version = array(
89
        '2_04' => 204,
90
        '2_15' => 215,
91
        '2_21' => 221,
92
        '2_28' => 228,
93
        '2_29' => 229,
94
        '2_33' => 233,
95
        '2_34' => 234,
96
        '2_35' => 235,
97
        '2_37' => 237,
98
    );
99
100 View Code Duplication
    while (list($key, $val) = each($version)) {
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...
101
        if ($previousVersion < $val) {
102
            $name = sprintf($cls, $key);
103
            $f    = $fld . $name . '.php';
104
            //ext_echo ("<hr>{$f}<hr>");
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...
105
            if (is_readable($f)) {
106
                echo "mise à jour version : {$key} = {$val}<br>";
107
                include_once $f;
108
                $cl = new $name($xoopsModule, array('previousVersion' => $previousVersion));
0 ignored issues
show
Bug introduced by
The variable $xoopsModule does not exist. Did you mean $module?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
109
            }
110
        }
111
    }
112
113
114
115
    if ($previousVersion < 240) {
116
117
118
        $configurator = include __DIR__ . '/config.php';
119
        $classUtilities = ucfirst($moduleDirName) . 'Utilities';
120
        if (!class_exists($classUtilities)) {
121
            xoops_load('utilities', $moduleDirName);
122
        }
123
124
125
        //delete old HTML templates
126
        if (count($configurator['templateFolders']) > 0) {
127
            foreach ($configurator['templateFolders'] as $folder) {
128
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
129
                if (is_dir($templateFolder)) {
130
                    $templateList = array_diff(scandir($templateFolder), array('..', '.'));
131
                    foreach ($templateList as $k => $v) {
132
                        $fileInfo = new SplFileInfo($templateFolder . $v);
133
                        if ($fileInfo->getExtension() === 'html' && $fileInfo->getFilename() !== 'index.html') {
134
                            if (file_exists($templateFolder . $v)) {
135
                                unlink($templateFolder . $v);
136
                            }
137
                        }
138
                    }
139
                }
140
            }
141
        }
142
143
144
145
        //  ---  COPY blank.png FILES ---------------
146 View Code Duplication
        if (count($configurator['copyFiles']) > 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...
147
            $file = __DIR__ . '/../assets/images/blank.png';
148
            foreach (array_keys($configurator['copyFiles']) as $i) {
149
                $dest = $configurator['copyFiles'][$i] . '/blank.png';
150
                $classUtilities::copyFile($file, $dest);
151
            }
152
        }
153
154
        //  ---  DELETE OLD FILES ---------------
155
        if (count($configurator['oldFiles']) > 0) {
156
            //    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...
157
            foreach (array_keys($configurator['oldFiles']) as $i) {
158
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator['oldFiles'][$i]);
159
                if (is_file($tempFile)) {
160
                    unlink($tempFile);
161
                }
162
            }
163
        }
164
165
        //---------------------
166
167
        //delete .html entries from the tpl table
168
        $sql = 'DELETE FROM ' . $xoopsDB->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname',
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...
169
                                                                                                          'n')
170
               . "' AND `tpl_file` LIKE '%.html%'";
171
        $xoopsDB->queryF($sql);
172
173
        // Load class XoopsFile ====================
174
        xoops_load('XoopsFile');
175
176
        //delete /images directory ============
177
        $imagesDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/images/');
178
        $folderHandler   = XoopsFile::getHandler('folder', $imagesDirectory);
179
        $folderHandler->delete($imagesDirectory);
180
    }
181
182
    $gpermHandler = xoops_getHandler('groupperm');
183
184
    return $gpermHandler->deleteByModule($module->getVar('mid'), 'item_read');
185
}
186