Passed
Pull Request — master (#18)
by Michael
04:31
created

xoops_module_update_xoopspoll()   F

Complexity

Conditions 18
Paths 1301

Size

Total Lines 69
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 40
c 0
b 0
f 0
nc 1301
nop 2
dl 0
loc 69
rs 0.7

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
 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
 * Xoopspoll install functions.php
14
 *
15
 * @copyright:: {@link https://xoops.org/ XOOPS Project}
16
 * @license  ::   {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
17
 * @package  ::   xoopspoll
18
 * @since    ::     1.40
19
 * @author   ::    zyspec <[email protected]>
20
 */
21
22
use XoopsModules\Xoopspoll;
23
24
25
//xoops_load('pollUtility', 'xoopspoll');
26
27
/**
28
 * @param \XoopsDatabase|null $db
29
 * @param                     $fromTable
30
 * @param                     $toTable
31
 * @return bool
32
 */
33
function xoopspollChangeTableName(\XoopsDatabase $db, $fromTable, $toTable)
34
{
35
    $fromTable = addslashes($fromTable);
36
    $toTable   = addslashes($toTable);
37
    /*
38
        $fromThisTable = $db->prefix("{$fromTable}");
39
        $toThisTable = $db->prefix("{$toTable}");
40
    */
41
    $success = false;
42
    if (Xoopspoll\Utility::dbTableExists($db, $fromTable) && !Xoopspoll\Utility::dbTableExists($db, $toTable)) {
43
        $sql     = sprintf('ALTER TABLE ' . $db->prefix((string)$fromTable) . ' RENAME ' . $db->prefix('{$toTable}'));
44
        $success = $db->queryF($sql);
45
        if (false === $success) {
46
            $moduleHandler   = $helper->getHandler('Module');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $helper seems to be never defined.
Loading history...
47
            $xoopspollModule = $moduleHandler->getByDirname('xoopspoll');
48
            $xoopspollModule->setErrors(sprintf(_AM_XOOPSPOLL_UPGRADE_FAILED, $fromTable));
49
        }
50
    }
51
52
    return $success;
53
}
54
55
/**
56
 * @param XoopsModule  $module
57
 * @param              $prev_version
58
 * @return bool
59
 */
60
function xoops_module_update_xoopspoll(\XoopsModule $module, $prev_version)
61
{
62
    // referer check
63
    $success = false;
64
    $ref     = xoops_getenv('HTTP_REFERER');
65
    if (('' === $ref) || 0 === mb_strpos($ref, $GLOBALS['xoops']->url('modules/system/admin.php'))) {
66
        /* module specific part */
67
        require_once $GLOBALS['xoops']->path('modules/xoopspoll/include/oninstall.php');
68
69
        $installedVersion = (int)$prev_version;
70
        xoops_loadLanguage('admin', 'xoopspoll');
71
        $db      = \XoopsDatabaseFactory::getDatabaseConnection();
72
        $success = true;
73
        if ($installedVersion < 140) {
74
            /* add column for poll anonymous which was created in versions prior
75
             * to 1.40 of xoopspoll but not automatically created
76
             */
77
            $result    = $db->queryF('SHOW COLUMNS FROM ' . $db->prefix('xoopspoll_desc') . " LIKE 'anonymous'");
78
            $foundAnon = $db->getRowsNum($result);
79
            if (empty($foundAnon)) {
80
                // column doesn't exist, so try and add it
81
                $success = $db->queryF('ALTER TABLE ' . $db->prefix('xoopspoll_desc') . ' ADD anonymous TINYINT( 1 ) DEFAULT 0 NOT NULL AFTER multiple');
82
                if (false === $success) {
83
                    $module->setErrors(_AM_XOOPSPOLL_ERROR_COLUMN . 'anonymous');
84
                }
85
            }
86
            /* change description to TINYTEXT */
87
            if ($success) {
88
                $success = $db->queryF('ALTER TABLE ' . $db->prefix('xoopspoll_desc') . ' MODIFY description TINYTEXT NOT NULL');
89
                if (false === $success) {
90
                    $module->setErrors(_AM_XOOPSPOLL_ERROR_COLUMN . 'description');
91
                }
92
            }
93
94
            if ($success) {
95
                $success = $db->queryF('ALTER TABLE ' . $db->prefix('xoopspoll_desc') . " ADD multilimit TINYINT( 63 ) UNSIGNED DEFAULT '0' NOT NULL AFTER multiple");
96
                if (false === $success) {
97
                    $module->setErrors(_AM_XOOPSPOLL_ERROR_COLUMN . 'multilimit');
98
                }
99
            }
100
            if ($success) {
101
                $success = $db->queryF('ALTER TABLE ' . $db->prefix('xoopspoll_desc') . " ADD mail_voter TINYINT( 1 ) UNSIGNED DEFAULT '0' NOT NULL AFTER mail_status");
102
                if (false === $success) {
103
                    $module->setErrors(_AM_XOOPSPOLL_ERROR_COLUMN . 'mail_voter');
104
                }
105
            }
106
            if ($success) {
107
                $result   = $db->queryF('SHOW COLUMNS FROM ' . $db->prefix('xoopspoll_desc') . " LIKE 'visibility'");
108
                $foundCol = $db->getRowsNum($result);
109
                if (empty($foundCol)) {
110
                    // column doesn't exist, so try and add it
111
                    $success = $db->queryF('ALTER TABLE ' . $db->prefix('xoopspoll_desc') . " ADD visibility INT( 3 ) DEFAULT '0' NOT NULL AFTER display");
112
                    if (false === $success) {
113
                        $module->setErrors(_AM_XOOPSPOLL_ERROR_COLUMN . 'visibility');
114
                    }
115
                }
116
            }
117
        }
118
119
        if ($success) {
120
            /* now reverse table names changes from 1.40 Beta  */
121
            $s1      = xoopspollChangeTableName($db, 'mod_xoopspoll_option', 'xoopspoll_option');
122
            $s2      = xoopspollChangeTableName($db, 'mod_xoopspoll_desc', 'xoopspoll_desc');
123
            $s3      = xoopspollChangeTableName($db, 'mod_xoopspoll_log', 'xoopspoll_log');
124
            $success = ($s1 && $s2 && $s3);
125
        }
126
    }
127
128
    return $success;
129
}
130