onupdate.inc.php ➔ xoops_module_update_mylinks_base()   C
last analyzed

Complexity

Conditions 13
Paths 6

Size

Total Lines 54

Duplication

Lines 16
Ratio 29.63 %

Importance

Changes 0
Metric Value
cc 13
nc 6
nop 2
dl 16
loc 54
rs 6.6166
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
2
/**
3
 * mylinks install functions.php
4
 *
5
 * LICENSE
6
 *
7
 * You may not change or alter any portion of this comment or credits
8
 * of supporting developers from this source code or any supporting source code
9
 * which is considered copyrighted (c) material of the original comment or credit authors.
10
 *
11
 * @copyright:: {@link http://xoops.org/ XOOPS Project}
12
 * @license  ::    {@link http://www.gnu.org/licenses/gpl-2.0.html GNU Public License}
13
 * @package  ::   mylinks
14
 * @author   ::    zyspec ([email protected])
15
 */
16
defined('XOOPS_ROOT_PATH') or die('Restricted access');
17
18
$moduleDirName = basename(dirname(__DIR__));
19
20
/**
21
 * @param XoopsModule $xoopsModule
22
 * @param $prev_version
23
 * @return bool
24
 */
25
function xoops_module_update_mylinks_base(XoopsModule $xoopsModule, $prev_version)
26
{
27
    $minUpgradeFrom = '0.0.0';  //minimum version of module supported for upgrade
28
    $success        = false;
29
30
    $ref = xoops_getenv('HTTP_REFERER');  //referer check
31
    if ($ref == '' || strpos($ref, XOOPS_URL . '/modules/system/admin.php') === 0) {
32
        /* module specific part */
33
        $minValueArray       = explode('.', $minUpgradeFrom);
34
        $installedVersion    = (int)$prev_version;
35
        $minSupportedVersion = ($minValueArray[0] * 100) + ($minValueArray[1] * 10) + $minValueArray[2];
36
        $modErrMsg           = "<span style='color: red; font-weight: bold;'>This module cannot be upgraded from version {$installedVersion}.</span>";
37
38
        if ($installedVersion < $minSupportedVersion) {
39
            $success = false;
40
            $xoopsModule->setErrors($modErrMsg);
41
        } else {
42
            require_once XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->getVar('dirname') . '/include/oninstall.inc.php';
43
            $success = xoops_module_pre_install_mylinks($xoopsModule);
44
        }
45
    }
46
47
    global $xoopsDB;
48
    if ($prev_version < 312) {
49
        // delete old html template files
50
        $templateDirectory = $GLOBALS['xoops']->path('modules/' . $xoopsModule->getVar('dirname', 'n') . '/templates/');
51
        $templateList      = array_diff(scandir($templateDirectory), array('..', '.'));
52 View Code Duplication
        foreach ($templateList as $k => $v) {
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...
53
            $fileInfo = new SplFileInfo($templateDirectory . $v);
54
            if ($fileInfo->getExtension() === 'html' && $fileInfo->getFilename() !== 'index.html') {
55
                if (file_exists($templateDirectory . $v)) {
56
                    unlink($templateDirectory . $v);
57
                }
58
            }
59
        }
60
        // delete old block html template files
61
        $templateDirectory = $GLOBALS['xoops']->path('modules/' . $xoopsModule->getVar('dirname', 'n') . '/templates/blocks/');
62
        $templateList      = array_diff(scandir($templateDirectory), array('..', '.'));
63 View Code Duplication
        foreach ($templateList as $k => $v) {
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...
64
            $fileInfo = new SplFileInfo($templateDirectory . $v);
65
            if ($fileInfo->getExtension() === 'html' && $fileInfo->getFilename() !== 'index.html') {
66
                if (file_exists($templateDirectory . $v)) {
67
                    unlink($templateDirectory . $v);
68
                }
69
            }
70
        }
71
72
        //delete .html entries from the tpl table
73
        $sql = 'DELETE FROM ' . $xoopsDB->prefix('tplfile') . " WHERE `tpl_module` = '" . $xoopsModule->getVar('dirname', 'n') . "' AND `tpl_file` LIKE '%.html%'";
74
        $xoopsDB->queryF($sql);
75
    }
76
77
    return $success;
78
}
79
80
/**
81
 * eval functions to support module relocation (directory renaming)
82
 */
83
eval('function xoops_module_update_' . $moduleDirName . '($module=NULL, $prev_version)
84
        {
85
        return xoops_module_update_mylinks_base($module, $prev_version);
86
        }
87
    ');
88