update_tdmcreate_v191()   B
last analyzed

Complexity

Conditions 9
Paths 42

Size

Total Lines 52
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 9
eloc 33
nc 42
nop 1
dl 0
loc 52
rs 8.0555
c 5
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
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
/**
13
 * tdmcreate module.
14
 *
15
 * @copyright       XOOPS Project (https://xoops.org)
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 *
18
 * @since           2.5.0
19
 *
20
 * @author          Txmod Xoops http://www.txmodxoops.org
21
 *
22
 * @version         $Id: update.php 12258 2014-01-02 09:33:29Z timgno $
23
 * @param mixed $module
24
 * @param null|mixed $prev_version
25
 */
26
27
/**
28
 * @param      $module
29
 * @param null $prev_version
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $prev_version is correct as it would always require null to be passed?
Loading history...
30
 *
31
 * @return bool|null
32
 */
33
function xoops_module_update_tdmcreate(&$module, $prev_version = null)
34
{
35
    // irmtfan bug fix: solve templates duplicate issue
36
    $ret = null;
37
    if ($prev_version < 191) {
38
        $ret = update_system_v191($module);
0 ignored issues
show
Bug introduced by
The function update_system_v191 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

38
        $ret = /** @scrutinizer ignore-call */ update_system_v191($module);
Loading history...
39
    }
40
    $errors = $module->getErrors();
41
    if (!empty($errors)) {
42
        print_r($errors);
43
    }
44
45
    return $ret;
46
    // irmtfan bug fix: solve templates duplicate issue
47
}
48
49
// irmtfan bug fix: solve templates duplicate issue
50
/**
51
 * @param $module
52
 *
53
 * @return bool
54
 */
55
function update_tdmcreate_v191(&$module)
56
{
57
    global $xoopsDB;
58
    $result = $xoopsDB->query(
59
        'SELECT t1.tpl_id FROM ' . $xoopsDB->prefix('tplfile') . ' t1, ' . $xoopsDB->prefix('tplfile')
60
        . ' t2 WHERE t1.tpl_refid = t2.tpl_refid AND t1.tpl_module = t2.tpl_module AND t1.tpl_tplset=t2.tpl_tplset AND t1.tpl_file = t2.tpl_file AND t1.tpl_type = t2.tpl_type AND t1.tpl_id > t2.tpl_id'
61
    );
62
    $tplids = [];
63
    while (list($tplid) = $xoopsDB->fetchRow($result)) {
64
        $tplids[] = $tplid;
65
    }
66
    if (count($tplids) > 0) {
67
        $tplfileHandler = xoops_getHandler('tplfile');
68
        $duplicate_files = $tplfileHandler->getObjects(
0 ignored issues
show
Bug introduced by
The method getObjects() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsRankHandler or XoUserHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        /** @scrutinizer ignore-call */ 
69
        $duplicate_files = $tplfileHandler->getObjects(
Loading history...
69
            new \Criteria('tpl_id', '(' . implode(',', $tplids) . ')', 'IN')
70
        );
71
72
        if (count($duplicate_files) > 0) {
73
            foreach (array_keys($duplicate_files) as $i) {
74
                $tplfileHandler->delete($duplicate_files[$i]);
75
            }
76
        }
77
    }
78
    $sql = 'SHOW INDEX FROM ' . $xoopsDB->prefix('tplfile') . " WHERE KEY_NAME = 'tpl_refid_module_set_file_type'";
79
    if (!$result = $xoopsDB->queryF($sql)) {
80
        xoops_error($this->db->error() . '<br />' . $sql);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
81
82
        return false;
83
    }
84
    $ret = [];
85
    while (false !== ($myrow = $xoopsDB->fetchArray($result))) {
86
        $ret[] = $myrow;
87
    }
88
    if (!empty($ret)) {
89
        $module->setErrors(
90
            "'tpl_refid_module_set_file_type' unique index is exist. Note: check 'tplfile' table to be sure this index is UNIQUE because XOOPS CORE need it."
91
        );
92
93
        return true;
94
    }
95
    $sql = 'ALTER TABLE ' . $xoopsDB->prefix('tplfile')
96
        . ' ADD UNIQUE tpl_refid_module_set_file_type ( tpl_refid, tpl_module, tpl_tplset, tpl_file, tpl_type )';
97
    if (!$result = $xoopsDB->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
98
        xoops_error($xoopsDB->error() . '<br />' . $sql);
99
        $module->setErrors(
100
            "'tpl_refid_module_set_file_type' unique index is not added to 'tplfile' table. Warning: do not use XOOPS until you add this unique index."
101
        );
102
103
        return false;
104
    }
105
106
    return true;
107
}
108
// irmtfan bug fix: solve templates duplicate issue
109