Passed
Branch master (cd0d6c)
by Michael
01:35
created

xoops_module_update_waiting()   B

Complexity

Conditions 9
Paths 1

Size

Total Lines 63
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 46
nc 1
nop 2
dl 0
loc 63
rs 7.6226
c 1
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
 You may not change or alter any portion of this comment or credits of
4
 supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit
6
 authors.
7
8
 This program is distributed in the hope that it will be useful, but
9
 WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
/**
13
 * Module: Waiting
14
 *
15
 * @category        Module
16
 * @package         waiting
17
 * @author          XOOPS Module Development Team
18
 * @copyright       {@link https://xoops.org 2001-2016 XOOPS Project}
19
 * @license         {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
20
 * @link            https://xoops.org XOOPS
21
 */
22
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->IsAdmin()) {
23
    exit('Restricted Access - ' . basename($_SERVER['PHP_SELF']) . PHP_EOL);
24
}
25
/**
26
 * Waiting Block installation update
27
 * @param XoopsModule $module  {@see XoopsModule}
28
 * @param int         $version currently installed module version
29
 *
30
 * @return bool success
31
 */
32
function xoops_module_update_waiting($module, $version)
0 ignored issues
show
Unused Code introduced by
The parameter $module is not used and could be removed. ( Ignorable by Annotation )

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

32
function xoops_module_update_waiting(/** @scrutinizer ignore-unused */ $module, $version)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $version is not used and could be removed. ( Ignorable by Annotation )

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

32
function xoops_module_update_waiting($module, /** @scrutinizer ignore-unused */ $version)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
{
34
    return true;  // force return - following code is really only needed for XOOPS < 2.3
35
    // Keep Block option values when update (by nobunobu)
36
    $mid = $module->mid();
0 ignored issues
show
Unused Code introduced by
$mid = $module->mid() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
37
    if ($mid) {
38
        $configHandler       = xoops_getHandler('config');
39
        $waitingModuleConfig = $configHandler->getConfigsByCat(0, $mid);
40
41
        $count = count($waitingModuleConfig['blocks']);
42
43
        /** @var \XoopsBlockHandler $blockHandler */
44
        $blockHandler = xoops_getHandler('block');
45
        $criteria     = new \CriteriaCompo();
46
        $criteria->add(new \Criteria('mid', $mid));
47
        $criteria->add(new \Criteria('block_type', 'D'));
48
        $criteria->add(new \Criteria('func_num', $count, '>'));
49
        $blockObjs = $blockHandler->getAll($criteria);
50
51
        foreach ($blockObjs as $blockObj) {
52
            $local_msgs[] = "Non Defined Block <b>{$fblock['name']}</b> will be deleted";
53
            $success      = $blockHandler->delete($blockObj); // remove the invalid block
54
        }
55
56
        $fieldsArray = ['func_num', 'name', 'options'];
57
        $criteria    = new \CriteriaCompo();
58
        $criteria->add(new \Criteria('mid', $mid));
59
        //        $criteria->add(new \Criteria('func_num', $i));
60
        $criteria->add(new \Criteria('show_func'), addslashes($waitingModuleConfig['blocks'][$i]['show_func']));
61
        $criteria->add(new \Criteria('func_file', addslashes($waitingModuleConfig['blocks'][$i]['file'])));
62
        $fblockObjs = $blockHandler->getObjects($criteria);
63
        foreach ($fblockObjs as $fblockObj) {
64
            if (!empty($fblockObj->options())) {
65
                $old_vals = explode('|', $fblockObj->getVar('options'));
66
                $def_vals = explode('|', $modversion['blocks'][$fblockObj->getVar('func_num')]['options']);
67
                if (count($old_vals) == count($def_vals)) {
68
                    $modversion['blocks'][$fblock->getVar('func_num')]['options'] = $fblockObj->getVar('options');
69
                    $local_msgs[]                                                 = "Option's values of the block <b>" . $fblockObj->getVar('name') . '</b> will be kept. (value = <b>' . $fblockObj->getVar('options') . '</b>)';
70
                } elseif (count($old_vals) < count($def_vals)) {
71
                    $def_vals                                                        = array_merge($old_vals, $def_vals); //merges prev. values with new - older are preserved
72
                    $modversion['blocks'][$fblockObj->getVar('func_num')]['options'] = implode('|', $def_vals);
73
                    $local_msgs[]                                                    = "Option's values of the block "
74
                                                                                       . '<b>'
75
                                                                                       . $fblockObj->getVar('name')
76
                                                                                       . '</b> '
77
                                                                                       . 'will be kept and new option(s) are added. (value = '
78
                                                                                       . '<b>'
79
                                                                                       . $waitingModuleConfig['blocks'][$fblockObj->getVar('options')]
80
                                                                                       . '</b>)';
81
                } else {
82
                    $local_msgs[] = "Option's values of the block " . '<b>' . $fblockObj->getVar('name') . '</b> ' . 'will be reset to the default, because of some decrease of options. (value = ' . '<b>' . $waitingModuleConfig['blocks'][$fblockObj->getVar('func_num')]['options'] . '</b>)';
83
                }
84
            }
85
        }
86
    }
87
88
    global $msgs, $myblocksadmin_parsed_updateblock;
89
    if (!empty($msgs) && empty($myblocksadmin_parsed_updateblock)) {
90
        $msgs                             = array_merge($msgs, $local_msgs);
91
        $myblocksadmin_parsed_updateblock = true;
92
    }
93
94
    return true;  //@todo: for now force success - eventually send 'actual' results
95
}
96